Class yii\i18n\I18N

继承yii\i18n\I18N » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/i18n/I18N.php

I18N provides features related with internationalization (I18N) and localization (L10N).

I18N is configured as an application component in yii\base\Application by default. You can access that instance via Yii::$app->i18n.

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$messageFormatter yii\i18n\MessageFormatter The message formatter to be used to format message via ICU message format. Note that the type of this property differs in getter and setter. See getMessageFormatter() and setMessageFormatter() for details. yii\i18n\I18N
$translations array List of yii\i18n\MessageSource configurations or objects. yii\i18n\I18N

公共方法

隐藏继承的方法

方法描述定义在
__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 using yii\i18n\MessageFormatter. yii\i18n\I18N
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getMessageFormatter() Returns the message formatter instance. yii\i18n\I18N
getMessageSource() Returns the message source for the given category. yii\i18n\I18N
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() Initializes the component by configuring the default message categories. yii\i18n\I18N
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
setMessageFormatter() yii\i18n\I18N
translate() Translates a message to the specified language. yii\i18n\I18N
trigger() 触发事件. yii\base\Component

属性详情

$messageFormatter public 属性

The message formatter to be used to format message via ICU message format. Note that the type of this property differs in getter and setter. See getMessageFormatter() and setMessageFormatter() for details.

$translations public 属性

List of yii\i18n\MessageSource configurations or objects. The array keys are message category patterns, and the array values are the corresponding yii\i18n\MessageSource objects or the configurations for creating the yii\i18n\MessageSource objects.

The message category patterns can contain the wildcard * at the end to match multiple categories with the same prefix. For example, app/* matches both app/cat1 and app/cat2.

The * category pattern will match all categories that do not match any other category patterns.

This property may be modified on the fly by extensions who want to have their own message sources registered under their own namespaces.

The category yii and app are always defined. The former refers to the messages used in the Yii core framework code, while the latter refers to the default message category for custom application code. By default, both of these categories use yii\i18n\PhpMessageSource and the corresponding message files are stored under @yii/messages and @app/messages, respectively.

You may override the configuration of both categories.

public array $translations null

方法详情

format() public 方法

Formats a message using yii\i18n\MessageFormatter.

public string format ( $message, $params, $language )
$message string

The message to be formatted.

$params array

The parameters that will be used to replace the corresponding placeholders in the message.

$language string

The language code (e.g. en-US, en).

return string

The formatted message.

getMessageFormatter() public 方法

Returns the message formatter instance.

public yii\i18n\MessageFormatter getMessageFormatter ( )
return yii\i18n\MessageFormatter

The message formatter to be used to format message via ICU message format.

getMessageSource() public 方法

Returns the message source for the given category.

public yii\i18n\MessageSource getMessageSource ( $category )
$category string

The category name.

return yii\i18n\MessageSource

The message source for the given category.

throws yii\base\InvalidConfigException

if there is no message source available for the specified category.

init() public 方法

Initializes the component by configuring the default message categories.

public void init ( )
setMessageFormatter() public 方法

public void setMessageFormatter ( $value )
$value string|array|yii\i18n\MessageFormatter

The message formatter to be used to format message via ICU message format. Can be given as array or string configuration that will be given to Yii::createObject() to create an instance or a yii\i18n\MessageFormatter instance.

translate() public 方法

Translates a message to the specified language.

After translation the message will be formatted using yii\i18n\MessageFormatter if it contains ICU message format and $params are not empty.

public string translate ( $category, $message, $params, $language )
$category string

The message category.

$message string

The message to be translated.

$params array

The parameters that will be used to replace the corresponding placeholders in the message.

$language string

The language code (e.g. en-US, en).

return string

The translated and formatted message.