yii\i18n\MessageFormatter - Yii2 类参考手册

Class yii\i18n\MessageFormatter

Inheritanceyii\i18n\MessageFormatter » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0

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

属性Property类型 Type简介 Description定义在 Defined 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

方法 Method简介 Description定义在 Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__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 an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
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\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
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

方法 Method简介 Description定义在 Defined By
fallbackFormat() Fallback implementation for MessageFormatter::formatMessage. yii\i18n\MessageFormatter

属性详情 Property Details

$errorCode public read-only property

Code of the last error.

public string getErrorCode ( )
$errorMessage public read-only property

Description of the last error.

方法详情 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.