Class yii\mongodb\i18n\MongoDbMessageSource

Inheritanceyii\mongodb\i18n\MongoDbMessageSource » yii\i18n\MessageSource
Available since version2.0.5

MongoDbMessageSource extends \yii\i18n\MessageSource and represents a message source that stores translated messages in MongoDB collection.

This message source uses single collection for the message translations storage, defined via $collection. each entry in this collection should have 3 fields:

  • language: string, translation language
  • category: string, name translation category
  • messages: array, list of actual message translations, in each element: the key is raw message name value - message translation.

For example:

{
    "category": "app",
    "language": "de",
    "messages": [
        {
            "Hello world!": "Hallo Welt!",
            ...
        },
        ...
    ],
}

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. yii\mongodb\i18n\MongoDbMessageSource
$cachingDuration integer The time in seconds that the messages can remain valid in cache. yii\mongodb\i18n\MongoDbMessageSource
$collection string|array The name of the MongoDB collection, which stores translated messages. yii\mongodb\i18n\MongoDbMessageSource
$db yii\mongodb\Connection|array|string The MongoDB connection object or the application component ID of the MongoDB connection. yii\mongodb\i18n\MongoDbMessageSource
$enableCaching boolean Whether to enable caching translated messages yii\mongodb\i18n\MongoDbMessageSource

Public Methods

Hide inherited methods

MethodDescriptionDefined By
init() Initializes the DbMessageSource component. yii\mongodb\i18n\MongoDbMessageSource

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
loadMessages() Loads the message translation for the specified language and category. yii\mongodb\i18n\MongoDbMessageSource
loadMessagesFromDb() Loads the messages from MongoDB. yii\mongodb\i18n\MongoDbMessageSource

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, that to enable caching you have to set $enableCaching to true, otherwise setting this property has no effect.

After the MongoDbMessageSource object is created, if you want to change this property, you should only assign it with a cache object.

This can also be a configuration array for creating the object.

See also:

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.

$collection public property

The name of the MongoDB collection, which stores translated messages. This collection is better to be pre-created with fields 'category' and 'language' indexed.

public string|array $collection 'message'
$db public property

The MongoDB connection object or the application component ID of the MongoDB connection.

After the MongoDbMessageSource object is created, if you want to change this property, you should only assign it with a MongoDB connection object.

This can also be a configuration array for creating the object.

$enableCaching public property

Whether to enable caching translated messages

public boolean $enableCaching false

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 MongoDB.

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

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

The message category.

$language string

The target language.

return array

The messages loaded from database.