MessageFormatter
API reference › yii › i18n › MessageFormatter
MessageFormatter allows formatting messages via ICU message format.
This class enhances the message formatter class provided by the PHP intl extension.
The following enhancements are provided:
-
It accepts named arguments and mixed numeric and named arguments.
-
Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be substituted.
-
Fixes PHP 5.5 weird placeholder replacement in case no arguments are provided at all (https://bugs.php.net/bug.php?id=65920).
-
Offers limited support for message formatting in case PHP intl extension is not installed. However it is highly recommended that you install PHP intl extension if you want to use MessageFormatter features.
The fallback implementation only supports the following message formats:
- plural formatting for english (‘one’ and ‘other’ selectors)
- select format
- simple parameters
- integer number parameters
The fallback implementation does NOT support the ‘apostrophe-friendly’ syntax. Also messages that are working with the fallback implementation are not necessarily compatible with the PHP intl MessageFormatter so do not rely on the fallback if you are able to install intl extension somehow.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$errorCode read-only | string | Code of the last error. | MessageFormatter |
$errorMessage read-only | string | Description of the last error. | MessageFormatter |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
format() | Formats a message via ICU message format. | MessageFormatter |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getErrorCode() | Get the error code from the last operation. | MessageFormatter |
getErrorMessage() | Get the error text from the last operation. | MessageFormatter |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
parse() | Parses an input string according to an ICU message format pattern. | MessageFormatter |
trigger() | Triggers an event. | yii\ |
fallbackFormat() protected | Fallback implementation for MessageFormatter::formatMessage. | MessageFormatter |
Method details
Section titled “Method details”format()
Section titled “format() ”public function format(string $pattern, array $params, string $language): string|falseFormats a message via ICU message format.
It uses the PHP intl extension’s MessageFormatter and works around some issues. If PHP intl is not installed a fallback will be used that supports a subset of the ICU message format.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The pattern string to insert parameters into. |
$params | array | The array of name value pairs to insert into the format string. |
$language | string | The locale to use for formatting locale-dependent parts |
Returns
string|false: The formatted pattern string or false if an error occurred
getErrorCode()
Section titled “getErrorCode() ”public function getErrorCode(): stringGet the error code from the last operation.
Returns
string: Code of the last error.
getErrorMessage()
Section titled “getErrorMessage() ”public function getErrorMessage(): stringGet the error text from the last operation.
Returns
string: Description of the last error.
parse()
Section titled “parse() ”public function parse(string $pattern, string $message, string $language): array|boolParses an input string according to an ICU message format pattern.
It uses the PHP intl extension’s MessageFormatter::parse() and adds support for named arguments. Usage of this method requires PHP intl extension to be installed.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The pattern to use for parsing the message. |
$message | string | The message to parse, conforming to the pattern. |
$language | string | The locale to use for formatting locale-dependent parts |
Returns
array|bool: An array containing items extracted, or FALSE on error.
Throws
yii\base\NotSupportedException: when PHP intl extension is not installed.
fallbackFormat()
Section titled “fallbackFormat() ”protected function fallbackFormat(string $pattern, array $args, string $locale): false|stringFallback implementation for MessageFormatter::formatMessage.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The pattern string to insert things into. |
$args | array | The array of values to insert into the format string |
$locale | string | The locale to use for formatting locale-dependent parts |
Returns
false|string: The formatted pattern string or false if an error occurred