Class yii\i18n\MessageSource

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

MessageSource is the base class for message translation repository classes.

A message source stores message translations in some persistent storage.

Child classes should override loadMessages() to provide translated messages.

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. yii\base\Component
$forceTranslation boolean Whether to force message translation when the source and target languages are the same. yii\i18n\MessageSource
$sourceLanguage string The language that the original messages are in. yii\i18n\MessageSource

公共方法

隐藏继承的方法

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

受保护的方法

隐藏继承的方法

方法描述定义在
loadMessages() Loads the message translation for the specified language and category. yii\i18n\MessageSource
translateMessage() Translates the specified message. yii\i18n\MessageSource

事件

隐藏继承的事件

事件类型描述定义在
EVENT_MISSING_TRANSLATION yii\i18n\MissingTranslationEvent An event that is triggered when a message translation is not found. yii\i18n\MessageSource

属性详情

$forceTranslation public 属性

Whether to force message translation when the source and target languages are the same. Defaults to false, meaning translation is only performed when source and target languages are different.

public boolean $forceTranslation false
$sourceLanguage public 属性

The language that the original messages are in. If not set, it will use the value of yii\base\Application::$sourceLanguage.

public string $sourceLanguage null

方法详情

init() public 方法

Initializes this component.

public void init ( )
loadMessages() protected 方法

Loads the message translation for the specified language and category.

If translation for specific locale code such as en-US isn't found it tries more generic en.

protected array loadMessages ( $category, $language )
$category string

The message category

$language string

The target language

return array

The loaded messages. The keys are original messages, and the values are translated messages.

translate() public 方法

Translates a message to the specified language.

Note that unless $forceTranslation is true, if the target language is the same as the source language, the message will NOT be translated.

If a translation is not found, a missingTranslation event will be triggered.

public string|boolean translate ( $category, $message, $language )
$category string

The message category

$message string

The message to be translated

$language string

The target language

return string|boolean

The translated message or false if translation wasn't found or isn't required

translateMessage() protected 方法

Translates the specified message.

If the message is not found, a missingTranslation event will be triggered. If there is an event handler, it may provide a fallback translation. If no fallback translation is provided this method will return false.

protected string|boolean translateMessage ( $category, $message, $language )
$category string

The category that the message belongs to.

$message string

The message to be translated.

$language string

The target language.

return string|boolean

The translated message or false if translation wasn't found.

事件详情

EVENT_MISSING_TRANSLATION 事件类型 yii\i18n\MissingTranslationEvent

An event that is triggered when a message translation is not found.