没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\i18n\DbMessageSource

继承yii\i18n\DbMessageSource » yii\i18n\MessageSource » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/i18n/DbMessageSource.php

DbMessageSource 继承自 yii\i18n\MessageSource, 并表示将翻译后的消息存储在数据库中作为消息源。

数据库必须包含以下两个表:source_message 和 message。

source_message 存储要翻译的消息, 表 message 储存翻译后的消息。 可以通过设置 $sourceMessageTable$messageTable 来自定义这两个表的名称。

数据库连接由 $db 指定。可以通过应用迁移来初始化数据库模式:

yii migrate --migrationPath=@yii/i18n/migrations/

如果你不想使用迁移,而是需要 SQL,那么所有数据库的文件都在 migrations 目录中。

公共属性

隐藏继承的属性

属性类型描述被定义在
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$cache yii\caching\CacheInterface|array|string 缓存对象或缓存对象的应用程序组件 ID。 消息数据将使用此缓存对象进行缓存。 注意,要启用缓存,必须将 $enableCaching 设置为 true,否则设置此属性没有效果。 在创建 DbMessageSource 对象之后,如果您想更改此属性, 您应该只使用缓存对象分配它。 从 2. yii\i18n\DbMessageSource
$cachingDuration integer 消息在缓存中保持有效的时间,以秒为单位。 使用 0 表示缓存的数据永远不会过期。 yii\i18n\DbMessageSource
$db yii\db\Connection|array|string 数据库连接对象或数据库连接的应用程序组件 ID。 在创建 DbMessageSource 对象之后, 如果您希望更改此属性,则应该仅使用 DB 连接对象对其进行分配。 从 2. yii\i18n\DbMessageSource
$enableCaching boolean 是否要缓存翻译后的消息 yii\i18n\DbMessageSource
$forceTranslation boolean 是否在源语言和目标语言相同时强制进行消息转换。 默认为 false,表示仅在源语言和目标语言不同时执行转换。 yii\i18n\MessageSource
$messageTable string 翻译后的消息表的名称。 yii\i18n\DbMessageSource
$sourceLanguage string 原始消息的语言。 如果没有设置,将使用 yii\base\Application::$sourceLanguage 的值。 yii\i18n\MessageSource
$sourceMessageTable string 源消息表的名称。 yii\i18n\DbMessageSource

公共方法

隐藏继承的方法

方法描述被定义在
__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() 初始化 DbMessageSource 组件。 这个方法将初始化 $db 属性,以确保它引用一个有效的 DB 连接。 配置的 $cache 组件也将被初始化。 yii\i18n\DbMessageSource
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

受保护的方法

隐藏继承的方法

方法描述被定义在
createFallbackQuery() 该方法为后备语言消息搜索构建 yii\db\Query 对象。 通常由 loadMessagesFromDb() 调用。 yii\i18n\DbMessageSource
loadMessages() 加载指定语言和类别的消息翻译。 如果没有找到 en-US 等特定语言环境代码的翻译, 它会尝试更通用的 en yii\i18n\DbMessageSource
loadMessagesFromDb() 从数据库加载消息。 您可以重写此方法来自定义数据库中的消息存储。 yii\i18n\DbMessageSource
translateMessage() 翻译指定的消息。 如果未找到该消息,将触发 missingTranslation 事件。 如果有事件处理程序,它可能会提供 fallback translation. yii\i18n\MessageSource

Events

隐藏继承的事件

事件类型描述被定义在
EVENT_MISSING_TRANSLATION yii\i18n\MissingTranslationEvent 当找不到一条消息翻译时触发的事件。 yii\i18n\MessageSource

常量

隐藏继承的常量

常量描述被定义在
CACHE_KEY_PREFIX 'DbMessageSource' 在生成缓存键时将使用的前缀。 Deprecated 此常量从未使用过,将在 2.1.0 中删除。 yii\i18n\DbMessageSource

属性详情

$cache 公共 属性

缓存对象或缓存对象的应用程序组件 ID。 消息数据将使用此缓存对象进行缓存。 注意,要启用缓存,必须将 $enableCaching 设置为 true,否则设置此属性没有效果。

在创建 DbMessageSource 对象之后,如果您想更改此属性, 您应该只使用缓存对象分配它。

从 2.0.2 版本开始,它也可以是用于创建对象的配置数组。

参见:

$cachingDuration 公共 属性

消息在缓存中保持有效的时间,以秒为单位。 使用 0 表示缓存的数据永远不会过期。

参见 $enableCaching.

$db 公共 属性

数据库连接对象或数据库连接的应用程序组件 ID。

在创建 DbMessageSource 对象之后, 如果您希望更改此属性,则应该仅使用 DB 连接对象对其进行分配。

从 2.0.2 版本开始,它也可以是用于创建对象的配置数组。

$enableCaching 公共 属性

是否要缓存翻译后的消息

public boolean $enableCaching false
$messageTable 公共 属性

翻译后的消息表的名称。

public string $messageTable '{{%message}}'
$sourceMessageTable 公共 属性

源消息表的名称。

public string $sourceMessageTable '{{%source_message}}'

方法详情

createFallbackQuery() 受保护 方法 (自版本 2.0.7 可用)

该方法为后备语言消息搜索构建 yii\db\Query 对象。 通常由 loadMessagesFromDb() 调用。

参见 loadMessagesFromDb().

protected yii\db\Query createFallbackQuery($category, $language, $fallbackLanguage)
$category string

消息类别

$language string

最初请求的语言

$fallbackLanguage string

目标后备语言

init() 公共 方法

初始化 DbMessageSource 组件。 这个方法将初始化 $db 属性,以确保它引用一个有效的 DB 连接。 配置的 $cache 组件也将被初始化。

public void init()
throws yii\base\InvalidConfigException

如果 $db 无效或 $cache 无效。

loadMessages() 受保护 方法

加载指定语言和类别的消息翻译。 如果没有找到 en-US 等特定语言环境代码的翻译, 它会尝试更通用的 en

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

消息的分类

$language string

目标语言

return array

加载消息。键是原始消息, 值是经过翻译的消息。

loadMessagesFromDb() 受保护 方法

从数据库加载消息。 您可以重写此方法来自定义数据库中的消息存储。

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

消息类别。

$language string

目标语言。

return array

从数据库加载的消息。