Skip to content
PHP Ecosystem
Explore

MessageFormatter

API reference › yii › i18n › MessageFormatter

classyii\i18n\MessageFormatter
Extendsyii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/i18n/MessageFormatter.php

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.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$errorCode read-onlystringCode of the last error.MessageFormatter
$errorMessage read-onlystringDescription of the last error.MessageFormatter
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
format()Formats a message via ICU message format.MessageFormatter
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
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\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.yii\base\BaseObject
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
parse()Parses an input string according to an ICU message format pattern.MessageFormatter
trigger()Triggers an event.yii\base\Component
fallbackFormat() protectedFallback implementation for MessageFormatter::formatMessage.MessageFormatter
public function format(string $pattern, array $params, string $language): string|false

Formats 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

ParameterTypeDescription
$patternstringThe pattern string to insert parameters into.
$paramsarrayThe array of name value pairs to insert into the format string.
$languagestringThe locale to use for formatting locale-dependent parts

Returns

string|false: The formatted pattern string or false if an error occurred

public function getErrorCode(): string

Get the error code from the last operation.

Returns

string: Code of the last error.

public function getErrorMessage(): string

Get the error text from the last operation.

Returns

string: Description of the last error.

public function parse(string $pattern, string $message, string $language): array|bool

Parses 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

ParameterTypeDescription
$patternstringThe pattern to use for parsing the message.
$messagestringThe message to parse, conforming to the pattern.
$languagestringThe locale to use for formatting locale-dependent parts

Returns

array|bool: An array containing items extracted, or FALSE on error.

Throws

protected function fallbackFormat(string $pattern, array $args, string $locale): false|string

Fallback implementation for MessageFormatter::formatMessage.

Parameters

ParameterTypeDescription
$patternstringThe pattern string to insert things into.
$argsarrayThe array of values to insert into the format string
$localestringThe locale to use for formatting locale-dependent parts

Returns

false|string: The formatted pattern string or false if an error occurred