3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\InvalidConfigException;
12
use yii\caching\CacheInterface;
17
use yii\helpers\ArrayHelper;
20
* DbMessageSource extends [[MessageSource]] and represents a message source that stores translated
21
* messages in database.
23
* The database must contain the following two tables: source_message and message.
25
* The `source_message` table stores the messages to be translated, and the `message` table stores
26
* the translated messages. The name of these two tables can be customized by setting [[sourceMessageTable]]
27
* and [[messageTable]], respectively.
29
* The database connection is specified by [[db]]. Database schema could be initialized by applying migration:
32
* yii migrate --migrationPath=@yii/i18n/migrations/
35
* If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.
37
* @author resurtm <resurtm@gmail.com>
40
class DbMessageSource extends MessageSource
43
* Prefix which would be used when generating cache key.
44
* @deprecated This constant has never been used and will be removed in 2.1.0.
46
const CACHE_KEY_PREFIX = 'DbMessageSource';
49
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
51
* After the DbMessageSource object is created, if you want to change this property, you should only assign
52
* it with a DB connection object.
54
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
58
* @var CacheInterface|array|string the cache object or the application component ID of the cache object.
59
* The messages data will be cached using this cache object.
60
* Note, that to enable caching you have to set [[enableCaching]] to `true`, otherwise setting this property has no effect.
62
* After the DbMessageSource object is created, if you want to change this property, you should only assign
63
* it with a cache object.
65
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
66
* @see cachingDuration
69
public $cache = 'cache';
71
* @var string the name of the source message table.
73
public $sourceMessageTable = '{{%source_message}}';
75
* @var string the name of the translated message table.
77
public $messageTable = '{{%message}}';
79
* @var int the time in seconds that the messages can remain valid in cache.
80
* Use 0 to indicate that the cached data will never expire.
83
public $cachingDuration = 0;
85
* @var bool whether to enable caching translated messages
87
public $enableCaching = false;
91
* Initializes the DbMessageSource component.
92
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
93
* Configured [[cache]] component would also be initialized.
94
* @throws InvalidConfigException if [[db]] is invalid or [[cache]] is invalid.
96
public function init()
99
$this->db = Instance::ensure($this->db, Connection::className());
100
if ($this->enableCaching) {
101
$this->cache = Instance::ensure($this->cache, 'yii\caching\CacheInterface');
106
* Loads the message translation for the specified language and category.
107
* If translation for specific locale code such as `en-US` isn't found it
108
* tries more generic `en`.
110
* @param string $category the message category
111
* @param string $language the target language
112
* @return array the loaded messages. The keys are original messages, and the values
113
* are translated messages.
115
protected function loadMessages($category, $language)
117
if ($this->enableCaching) {
123
$messages = $this->cache->get($key);
124
if ($messages === false) {
125
$messages = $this->loadMessagesFromDb($category, $language);
126
$this->cache->set($key, $messages, $this->cachingDuration);
132
return $this->loadMessagesFromDb($category, $language);
136
* Loads the messages from database.
137
* You may override this method to customize the message storage in the database.
138
* @param string $category the message category.
139
* @param string $language the target language.
140
* @return array the messages loaded from database.
142
protected function loadMessagesFromDb($category, $language)
144
$mainQuery = (new Query())->select(['message' => 't1.message', 'translation' => 't2.translation'])
145
->from(['t1' => $this->sourceMessageTable, 't2' => $this->messageTable])
147
't1.id' => new Expression('[[t2.id]]'),
148
't1.category' => $category,
149
't2.language' => $language,
152
$fallbackLanguage = substr($language, 0, 2);
153
$fallbackSourceLanguage = substr($this->sourceLanguage, 0, 2);
155
if ($fallbackLanguage !== $language) {
156
$mainQuery->union($this->createFallbackQuery($category, $language, $fallbackLanguage), true);
157
} elseif ($language === $fallbackSourceLanguage) {
158
$mainQuery->union($this->createFallbackQuery($category, $language, $fallbackSourceLanguage), true);
161
$messages = $mainQuery->createCommand($this->db)->queryAll();
163
return ArrayHelper::map($messages, 'message', 'translation');
167
* The method builds the [[Query]] object for the fallback language messages search.
168
* Normally is called from [[loadMessagesFromDb]].
170
* @param string $category the message category
171
* @param string $language the originally requested language
172
* @param string $fallbackLanguage the target fallback language
174
* @see loadMessagesFromDb
177
protected function createFallbackQuery($category, $language, $fallbackLanguage)
179
return (new Query())->select(['message' => 't1.message', 'translation' => 't2.translation'])
180
->from(['t1' => $this->sourceMessageTable, 't2' => $this->messageTable])
182
't1.id' => new Expression('[[t2.id]]'),
183
't1.category' => $category,
184
't2.language' => $fallbackLanguage,
186
'NOT IN', 't2.id', (new Query())->select('[[id]]')->from($this->messageTable)->where(['language' => $language]),