# DbMessageSource

> DbMessageSource extends yii\i18n\MessageSource and represents a message source that stores translated messages in database.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [i18n](/docs/api/yii/i18n/index.md) › DbMessageSource

| class | `yii\i18n\DbMessageSource` |
| --- | --- |
| Extends | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/i18n/DbMessageSource.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/i18n/DbMessageSource.php#L40) |

DbMessageSource extends [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) and represents a message source that stores translated
messages in database.

The database must contain the following two tables: source_message and message.

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`](/docs/api/yii/i18n/dbmessagesource/index.md#property-sourceMessageTable)
and [`$messageTable`](/docs/api/yii/i18n/dbmessagesource/index.md#property-messageTable), respectively.

The database connection is specified by [`$db`](/docs/api/yii/i18n/dbmessagesource/index.md#property-db). Database schema could be initialized by applying migration:

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

If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$cache`](#property-cache) | [`yii\caching\CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md)`\|array\|string` | The cache object or the application component ID of the cache object. Default: `'cache'`. | `DbMessageSource` |
| [`$cachingDuration`](#property-cachingDuration) | `int` | The time in seconds that the messages can remain valid in cache. Default: `0`. | `DbMessageSource` |
| [`$db`](#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection. Default: `'db'`. | `DbMessageSource` |
| <span id="property-enableCaching"></span>`$enableCaching` | `bool` | Whether to enable caching translated messages Default: `false`. | `DbMessageSource` |
| <span id="property-forceTranslation"></span>[`$forceTranslation`](/docs/api/yii/i18n/messagesource/index.md#property-forceTranslation) | `bool` | Whether to force message translation when the source and target languages are the same. Default: `false`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <span id="property-messageTable"></span>`$messageTable` | `string` | The name of the translated message table. Default: `'{{%message}}'`. | `DbMessageSource` |
| <span id="property-sourceLanguage"></span>[`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage) | `string\|null` | The language that the original messages are in. Default: `null`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <span id="property-sourceMessageTable"></span>`$sourceMessageTable` | `string` | The name of the source message table. Default: `'{{%source_message}}'`. | `DbMessageSource` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the DbMessageSource component. | `DbMessageSource` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-translate"></span>[`translate()`](/docs/api/yii/i18n/messagesource/index.md#method-translate) | Translates a message to the specified language. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`createFallbackQuery()`](#method-createFallbackQuery) protected | The method builds the [`Query`](/docs/api/yii/db/query/index.md) object for the fallback language messages search. | `DbMessageSource` |
| [`loadMessages()`](#method-loadMessages) protected | Loads the message translation for the specified language and category. | `DbMessageSource` |
| [`loadMessagesFromDb()`](#method-loadMessagesFromDb) protected | Loads the messages from database. | `DbMessageSource` |
| <span id="method-translateMessage"></span>[`translateMessage()`](/docs/api/yii/i18n/messagesource/index.md#method-translateMessage) protected | Translates the specified message. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_MISSING_TRANSLATION"></span>[`EVENT_MISSING_TRANSLATION`](/docs/api/yii/i18n/messagesource/index.md#event-EVENT_MISSING_TRANSLATION) | [`MissingTranslationEvent`](/docs/api/yii/i18n/missingtranslationevent/index.md) | An event that is triggered when a message translation is not found. Event name: `'missingTranslation'`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |

## Property details

### $cache <span id="property-cache"></span>

```php
public yii\caching\CacheInterface|array|string $cache = 'cache'
```

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`](/docs/api/yii/i18n/dbmessagesource/index.md#property-enableCaching) to `true`, otherwise setting this property has no effect.

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.

### $cachingDuration <span id="property-cachingDuration"></span>

```php
public int $cachingDuration = 0
```

The time in seconds that the messages can remain valid in cache.

Use 0 to indicate that the cached data will never expire.

### $db <span id="property-db"></span>

```php
public yii\db\Connection|array|string $db = 'db'
```

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.

## Method details

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the DbMessageSource component.

This method will initialize the [`$db`](/docs/api/yii/i18n/dbmessagesource/index.md#property-db) property to make sure it refers to a valid DB connection.
Configured [`$cache`](/docs/api/yii/i18n/dbmessagesource/index.md#property-cache) component would also be initialized.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if [`$db`](/docs/api/yii/i18n/dbmessagesource/index.md#property-db) is invalid or [`$cache`](/docs/api/yii/i18n/dbmessagesource/index.md#property-cache) is invalid.

### createFallbackQuery() <span id="method-createFallbackQuery"></span>

```php
protected function createFallbackQuery(string $category, string $language, string $fallbackLanguage): yii\db\Query
```

The method builds the [`Query`](/docs/api/yii/db/query/index.md) object for the fallback language messages search.

Normally is called from [`loadMessagesFromDb()`](/docs/api/yii/i18n/dbmessagesource/index.md#method-loadMessagesFromDb).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category |
| `$language` | `string` | The originally requested language |
| `$fallbackLanguage` | `string` | The target fallback language |

**Returns**

[`yii\db\Query`](/docs/api/yii/db/query/index.md)

**Since**

2.0.7

**See also**

- [`loadMessagesFromDb()`](/docs/api/yii/i18n/dbmessagesource/index.md#method-loadMessagesFromDb)

### loadMessages() <span id="method-loadMessages"></span>

```php
protected function loadMessages(string $category, string $language): array
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category |
| `$language` | `string` | The target language |

**Returns**

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

### loadMessagesFromDb() <span id="method-loadMessagesFromDb"></span>

```php
protected function loadMessagesFromDb(string $category, string $language): array
```

Loads the messages from database.

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category. |
| `$language` | `string` | The target language. |

**Returns**

`array`: The messages loaded from database.