Class yii\i18n\MessageFormatter

Inheritanceyii\i18n\MessageFormatter » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/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.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component. yii\base\Component
$errorCode string Code of the last error. yii\i18n\MessageFormatter
$errorMessage string Description of the last error. yii\i18n\MessageFormatter

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__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
__construct() Constructor. yii\base\BaseObject
__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() Returns 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. yii\i18n\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. yii\i18n\MessageFormatter
getErrorMessage() Get the error text from the last operation. yii\i18n\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. yii\i18n\MessageFormatter
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
fallbackFormat() Fallback implementation for MessageFormatter::formatMessage. yii\i18n\MessageFormatter

Property Details

$errorCode public property

Code of the last error.

public string $errorCode null
$errorMessage public property

Description of the last error.

public string $errorMessage null

Method Details

fallbackFormat() protected method

Fallback implementation for MessageFormatter::formatMessage.

protected false|string fallbackFormat ( $pattern, $args, $locale )
$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

return false|string

The formatted pattern string or false if an error occurred

format() public method

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.

public string|false format ( $pattern, $params, $language )
$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

return string|false

The formatted pattern string or false if an error occurred

getErrorCode() public method

Get the error code from the last operation.

public string getErrorCode ( )
return string

Code of the last error.

getErrorMessage() public method

Get the error text from the last operation.

public string getErrorMessage ( )
return string

Description of the last error.

parse() public method

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.

public array|boolean parse ( $pattern, $message, $language )
$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

return array|boolean

An array containing items extracted, or FALSE on error.

throws yii\base\NotSupportedException

when PHP intl extension is not installed.