Class yii\i18n\I18N
| 继承 | yii\i18n\I18N » yii\base\Component » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable |
| 可用版本自 | 2.0 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/i18n/I18N.php |
I18N 提供与国际化(I18N)和本地化(L10N)相关的功能。
默认情况下,I18N 在 yii\base\Application 中配置为应用程序组件。
您可以通过 Yii::$app->i18n 访问该实例。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $messageFormatter | yii\i18n\MessageFormatter | 消息格式化程序实例,用于通过 ICU 消息格式格式化消息。 | yii\i18n\I18N |
| $translations | array | yii\i18n\MessageSource 配置或对象的列表。
数组键是消息类别模式,
数组值是对应的 yii\i18n\MessageSource 对象或用于创建 yii\i18n\MessageSource 对象的配置。
消息类别模式可以在末尾包含通配符 *,以匹配具有相同前缀的多个类别。
例如,app/* 匹配 app/cat1 和 app/cat2。
类别模式 * 将匹配与其他类别模式不匹配的所有类别。
如果希望在自己的命名空间中扩展注册自己的消息源,
则可以动态修改此属性。
始终定义有类别 yii 和 app。
前者指的是 Yii 核心框架代码中使用的消息类别,而后者指的是自定义应用程序代码的默认消息类别。
默认情况下,这两个类别都使用 yii\i18n\PhpMessageSource,
相应的消息文件分别存储在 @yii/messages 和 @app/messages 下。
您可以重写两个类别的配置。 |
yii\i18n\I18N |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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() | 使用 yii\i18n\MessageFormatter 格式化消息。 | yii\i18n\I18N |
| getBehavior() | Returns the named behavior object. | yii\base\Component |
| getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
| getMessageFormatter() | 返回消息格式化程序实例。 | yii\i18n\I18N |
| getMessageSource() | 返回给定类别的消息源。 | yii\i18n\I18N |
| 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() | 通过配置默认消息类别来初始化组件。 | yii\i18n\I18N |
| off() | Detaches an existing event handler from this component. | yii\base\Component |
| on() | Attaches an event handler to an event. | yii\base\Component |
| setMessageFormatter() | yii\i18n\I18N | |
| translate() | 翻译消息到指定的语言。 | yii\i18n\I18N |
| trigger() | Triggers an event. | yii\base\Component |
属性详情
消息格式化程序实例,用于通过 ICU 消息格式格式化消息。
yii\i18n\MessageSource 配置或对象的列表。 数组键是消息类别模式, 数组值是对应的 yii\i18n\MessageSource 对象或用于创建 yii\i18n\MessageSource 对象的配置。
消息类别模式可以在末尾包含通配符 *,以匹配具有相同前缀的多个类别。
例如,app/* 匹配 app/cat1 和 app/cat2。
类别模式 * 将匹配与其他类别模式不匹配的所有类别。
如果希望在自己的命名空间中扩展注册自己的消息源, 则可以动态修改此属性。
始终定义有类别 yii 和 app。
前者指的是 Yii 核心框架代码中使用的消息类别,而后者指的是自定义应用程序代码的默认消息类别。
默认情况下,这两个类别都使用 yii\i18n\PhpMessageSource,
相应的消息文件分别存储在 @yii/messages 和 @app/messages 下。
您可以重写两个类别的配置。
方法详情
使用 yii\i18n\MessageFormatter 格式化消息。
| public string format($message, $params, $language) | ||
| $message | string | 要格式化的消息。 |
| $params | array | 将用于替换消息中相应占位符的参数。 |
| $language | string | 语言代码(例如 |
| return | string | 格式化的消息。 |
|---|---|---|
返回消息格式化程序实例。
| public yii\i18n\MessageFormatter getMessageFormatter() | ||
| return | yii\i18n\MessageFormatter | 消息格式化程序实例,用于通过 ICU 消息格式格式化消息。 |
|---|---|---|
返回给定类别的消息源。
| public yii\i18n\MessageSource getMessageSource($category) | ||
| $category | string | 类别名称。 |
| return | yii\i18n\MessageSource | 给定类别的消息源。 |
|---|---|---|
| throws | yii\base\InvalidConfigException | 如果没有可用于指定类别的消息源。 |
通过配置默认消息类别来初始化组件。
| public void init() |
| public void setMessageFormatter($value) | ||
| $value | string|array|yii\i18n\MessageFormatter | 消息格式化程序,用于通过 ICU 消息格式格式化消息。 可以配置为数组或字符串, 它们将被赋予 Yii::createObject() 以创建实例或 yii\i18n\MessageFormatter 实例。 |
翻译消息到指定的语言。
翻译后,如果消息包含 ICU 消息格式且 $params 不为空,
则使用 yii\i18n\MessageFormatter 格式化消息。
| public string translate($category, $message, $params, $language) | ||
| $category | string | 消息类别。 |
| $message | string | 要翻译的信息。 |
| $params | array | 将用于替换消息中相应占位符的参数。 |
| $language | string | 语言代码(例如 |
| return | string | 已经翻译和格式化的消息。 |
|---|---|---|