Class yii\i18n\GettextMessageSource
GettextMessageSource 表示基于 GNU Gettext 的消息源。
每个 GettextMessageSource 实例表示单个域的消息翻译。 每个消息类别表示 Gettext 中的消息上下文。 翻译后的消息存储为 MO 或 PO 文件, 具体取决于 $useMoFile 属性值。
所有翻译都保存在 $basePath 目录下。
一种语言的翻译将以 MO 或 PO 文件保存在一个单独的子目录下, 该子目录的名称是语言 ID。 文件名通过 $catalog 属性指定,默认为 'messages'。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $basePath | string | yii\i18n\GettextMessageSource | |
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $catalog | string | yii\i18n\GettextMessageSource | |
| $forceTranslation | boolean | 是否在源语言和目标语言相同时强制进行消息转换。 默认为 false,表示仅在源语言和目标语言不同时执行转换。 | yii\i18n\MessageSource |
| $sourceLanguage | string | 原始消息的语言。 如果没有设置,将使用 yii\base\Application::$sourceLanguage 的值。 | yii\i18n\MessageSource |
| $useBigEndian | boolean | yii\i18n\GettextMessageSource | |
| $useMoFile | boolean | yii\i18n\GettextMessageSource |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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 |
| getBehavior() | Returns the named behavior object. | yii\base\Component |
| getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
| 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\MessageSource |
| off() | Detaches an existing event handler from this component. | yii\base\Component |
| on() | Attaches an event handler to an event. | yii\base\Component |
| translate() | 翻译消息到指定的语言。 | yii\i18n\MessageSource |
| trigger() | Triggers an event. | yii\base\Component |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| getMessageFilePath() | 返回指定语言和类别的消息文件路径。 | yii\i18n\GettextMessageSource |
| loadFallbackMessages() | 该方法由 loadMessages() 调用来为语言加载后备消息。 方法尝试为 $fallbackLanguage 加载 $category 消息,并将它们添加到 $messages 数组中。 | yii\i18n\GettextMessageSource |
| loadMessages() | 加载指定 $language 和 $category 的消息翻译。
如果找不到特定区域代码(例如 en-US)的翻译,则会尝试更通用的 en。
当两者都存在时,en-US 消息将合并在 en 上。
有关详细信息,请参见 loadFallbackMessages()。
如果 $language 不如 $sourceLanguage 更具体,
则该方法将尝试加载 $sourceLanguage 的消息。例如:$sourceLanguage 是 en-GB,
$language 是 en。该方法将加载 en 的消息并将它们合并到 en-GB。 |
yii\i18n\GettextMessageSource |
| loadMessagesFromFile() | 加载指定语言和类别的信息翻译,如果文件不存在返回 null。 | yii\i18n\GettextMessageSource |
| translateMessage() | 翻译指定的消息。 如果未找到该消息,将触发 missingTranslation 事件。 如果有事件处理程序,它可能会提供 fallback translation. | yii\i18n\MessageSource |
Events
| 事件 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| EVENT_MISSING_TRANSLATION | yii\i18n\MissingTranslationEvent | 当找不到一条消息翻译时触发的事件。 | yii\i18n\MessageSource |
常量
| 常量 | 值 | 描述 | 被定义在 |
|---|---|---|---|
| MO_FILE_EXT | '.mo' | yii\i18n\GettextMessageSource | |
| PO_FILE_EXT | '.po' | yii\i18n\GettextMessageSource |
属性详情
方法详情
返回指定语言和类别的消息文件路径。
| protected string getMessageFilePath($language) | ||
| $language | string | 目标语言 |
| return | string | 消息文件的路径 |
|---|---|---|
该方法由 loadMessages() 调用来为语言加载后备消息。 方法尝试为 $fallbackLanguage 加载 $category 消息,并将它们添加到 $messages 数组中。
| protected array loadFallbackMessages($category, $fallbackLanguage, $messages, $originalMessageFile) | ||
| $category | string | 消息类别 |
| $fallbackLanguage | string | 目标后备语言 |
| $messages | array | 先前加载的翻译消息的数组。 键是源消息,值是翻译的消息。 |
| $originalMessageFile | string | 带有消息文件的路径。 用于在未找到任何翻译时记录错误信息。 |
| return | array | 加载的消息。键是源消息,值是翻译的消息。 |
|---|---|---|
加载指定 $language 和 $category 的消息翻译。
如果找不到特定区域代码(例如 en-US)的翻译,则会尝试更通用的 en。
当两者都存在时,en-US 消息将合并在 en 上。
有关详细信息,请参见 loadFallbackMessages()。
如果 $language 不如 $sourceLanguage 更具体,
则该方法将尝试加载 $sourceLanguage 的消息。例如:$sourceLanguage 是 en-GB,
$language 是 en。该方法将加载 en 的消息并将它们合并到 en-GB。
参见:
| protected array loadMessages($category, $language) | ||
| $category | string | 消息类别 |
| $language | string | 目标语言 |
| return | array | 加载的消息。键是源消息,值是翻译的消息。 |
|---|---|---|
加载指定语言和类别的信息翻译,如果文件不存在返回 null。
| protected array|null loadMessagesFromFile($messageFile, $category) | ||
| $messageFile | string | 消息文件的路径 |
| $category | string | 消息类别 |
| return | array|null | 消息数组,如果找不到文件,则返回 null |
|---|---|---|