Class yii\i18n\MessageFormatter

继承yii\i18n\MessageFormatter » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用自版本2.0
源代码 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.

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$errorCode string Code of the last error. This property is read-only. yii\i18n\MessageFormatter
$errorMessage string Description of the last error. This property is read-only. yii\i18n\MessageFormatter

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
format() Formats a message via ICU message format. yii\i18n\MessageFormatter
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. 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() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\base\BaseObject
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
parse() Parses an input string according to an ICU message format pattern. yii\i18n\MessageFormatter
trigger() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
fallbackFormat() Fallback implementation for MessageFormatter::formatMessage. yii\i18n\MessageFormatter

属性详情

$errorCode public 属性

Code of the last error. This property is read-only.

public string $errorCode null
$errorMessage public 属性

Description of the last error. This property is read-only.

public string $errorMessage null

方法详情

fallbackFormat() protected 方法

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 方法

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 方法

Get the error code from the last operation.

public string getErrorCode ( )
return string

Code of the last error.

getErrorMessage() public 方法

Get the error text from the last operation.

public string getErrorMessage ( )
return string

Description of the last error.

parse() public 方法

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.