Class yeesoft\db\DbMessageSource

Inheritanceyeesoft\db\DbMessageSource » yii\i18n\MessageSource

DbMessageSource extends \yeesoft\db\MessageSource and represents a message source that stores translated messages in database.

The database must contain the following two tables:

CREATE TABLE source_message (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    category VARCHAR(32),
    message TEXT
);

CREATE TABLE message (
    id INTEGER,
    language VARCHAR(16),
    translation TEXT,
    PRIMARY KEY (id, language),
    CONSTRAINT fk_message_source_message FOREIGN KEY (id)
        REFERENCES source_message (id) ON DELETE CASCADE ON UPDATE RESTRICT
);

The source_message table stores the messages to be translated, and the message table stores the translated messages. The name of these two tables can be customized by setting $sourceMessageTable and $messageTable, respectively.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$cache \yii\caching\Cache|array|string The cache object or the application component ID of the cache object. yeesoft\db\DbMessageSource
$cachingDuration integer The time in seconds that the messages can remain valid in cache. yeesoft\db\DbMessageSource
$db \yii\db\Connection|array|string The DB connection object or the application component ID of the DB connection. yeesoft\db\DbMessageSource
$enableCaching boolean Whether to enable caching translated messages yeesoft\db\DbMessageSource
$messageTable string The name of the translated message table. yeesoft\db\DbMessageSource
$sourceMessageTable string The name of the source message table. yeesoft\db\DbMessageSource

Public Methods

Hide inherited methods

MethodDescriptionDefined By
init() Initializes the DbMessageSource component. yeesoft\db\DbMessageSource

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
loadMessages() Loads the message translation for the specified language and category. yeesoft\db\DbMessageSource
loadMessagesFromDb() Loads the messages from database. yeesoft\db\DbMessageSource

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
CACHE_KEY_PREFIX 'DbMessageSource' Prefix which would be used when generating cache key. yeesoft\db\DbMessageSource

Property Details

$cache public property

The cache object or the application component ID of the cache object. The messages data will be cached using this cache object. Note, this property has meaning only in case $cachingDuration set to non-zero value. After the DbMessageSource object is created, if you want to change this property, you should only assign it with a cache object. Starting from version 2.0.2, this can also be a configuration array for creating the object.

public \yii\caching\Cache|array|string $cache 'cache'
$cachingDuration public property

The time in seconds that the messages can remain valid in cache. Use 0 to indicate that the cached data will never expire.

See also $enableCaching.

$db public property

The DB connection object or the application component ID of the DB connection. After the DbMessageSource object is created, if you want to change this property, you should only assign it with a DB connection object. Starting from version 2.0.2, this can also be a configuration array for creating the object.

public \yii\db\Connection|array|string $db 'db'
$enableCaching public property

Whether to enable caching translated messages

public boolean $enableCaching false
$messageTable public property

The name of the translated message table.

public string $messageTable '{{%message}}'
$sourceMessageTable public property

The name of the source message table.

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

Method Details

init() public method

Initializes the DbMessageSource component.

This method will initialize the $db property to make sure it refers to a valid DB connection. Configured $cache component would also be initialized.

public void init ( )
throws \yii\base\InvalidConfigException

if $db is invalid or $cache is invalid.

loadMessages() protected method

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.

loadMessagesFromDb() protected method

Loads the messages from database.

You may override this method to customize the message storage in the database.

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

The message category.

$language string

The target language.

return array

The messages loaded from database.