Class yii\i18n\MessageFormatter
| 继承 | yii\i18n\MessageFormatter » yii\base\Component » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable |
| 可用版本自 | 2.0 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/i18n/MessageFormatter.php |
MessageFormatter 允许通过 ICU message format 格式化消息。
此类增强了 PHP intl 扩展提供的消息格式化程序类。
提供以下增强功能:
- 它接受命名参数和混合的数值参数和命名参数。
- 当提供的参数数量不足时,不会发出错误。 相反,占位符不会被替换。
- 修复 PHP 5.5 奇怪的占位符替换,以防根本没有提供任何参数 (https://bugs.php.net/bug.php?id=65920)。
如果未安装 PHP intl 扩展,则对消息格式化提供有限支持。 但是,如果要使用 MessageFormatter 功能, 强烈建议您安装 PHP intl extension。
回退实现仅支持以下消息格式:
- 英语的复数格式('one' 和 'other' 选择器)
- 选择格式
- 简单的参数
- 整数参数
回退实现不支持 'apostrophe-friendly' syntax。 使用回退实现的消息也不一定与 PHP intl MessageFormatter 兼容, 因此如果能够安装 intl 扩展,则不要依赖回退。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $errorCode | string | 最后一个错误的代码。 | yii\i18n\MessageFormatter |
| $errorMessage | string | 最后一个错误的描述。 | yii\i18n\MessageFormatter |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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() | 通过 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() | 获得最后一个操作的错误代码。 | yii\i18n\MessageFormatter |
| getErrorMessage() | 获得最后一个操作的错误信息。 | 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() | 根据 ICU message format 模式解析输入字符串。 | yii\i18n\MessageFormatter |
| trigger() | Triggers an event. | yii\base\Component |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| fallbackFormat() | MessageFormatter::formatMessage 的回退实现。 | yii\i18n\MessageFormatter |
属性详情
最后一个错误的代码。
最后一个错误的描述。
方法详情
MessageFormatter::formatMessage 的回退实现。
| protected false|string fallbackFormat($pattern, $args, $locale) | ||
| $pattern | string | 要插入内容的模式字符串。 |
| $args | array | 要插入格式字符串的值数组 |
| $locale | string | 用于格式化与区域设置相关的部件的区域设置 |
| return | false|string | 格式化的模式字符串,如果发生错误,则为 |
|---|---|---|
通过 ICU message format 格式化一个消息。
它使用 PHP intl 扩展的 MessageFormatter 并解决了一些问题。 如果未安装 PHP intl,将使用支持 ICU 消息格式子集的回退。
| public string|false format($pattern, $params, $language) | ||
| $pattern | string | 要插入参数的模式字符串。 |
| $params | array | 要插入到格式字符串中的键值对数组。 |
| $language | string | 用于格式化与区域设置相关的部件的区域设置 |
| return | string|false | 格式化的模式字符串,如果发生错误,则为 |
|---|---|---|
获得最后一个操作的错误代码。
| public string getErrorCode() | ||
| return | string | 最后一个错误的代码。 |
|---|---|---|
获得最后一个操作的错误信息。
| public string getErrorMessage() | ||
| return | string | 最后一个错误的描述。 |
|---|---|---|
根据 ICU message format 模式解析输入字符串。
它使用 PHP intl 扩展的 MessageFormatter::parse() 并添加对命名参数的支持。 使用此方法需要安装 PHP intl 扩展。
| public array|boolean parse($pattern, $message, $language) | ||
| $pattern | string | 用于解析消息的模式。 |
| $message | string | 符合模式的要解析的消息。 |
| $language | string | 用于格式化与区域设置相关的部件的区域设置 |
| return | array|boolean | 包含提取的项的数组,出错时为 |
|---|---|---|
| throws | yii\base\NotSupportedException | 如果 PHP intl 扩展未安装。 |