-
Notifications
You must be signed in to change notification settings - Fork 3
/
MessageSource.php
131 lines (123 loc) · 5.19 KB
/
MessageSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace conquer\i18n;
use yii\di\Instance;
use conquer\i18n\models\I18nMessage;
use yii\i18n\MissingTranslationEvent;
use yii\caching\Cache;
use conquer\i18n\models\I18nTranslator;
use conquer\i18n\models\I18nTranslation;
class MessageSource extends \yii\i18n\MessageSource
{
private $_messages;
/**
* @var Cache|array|string 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.
*/
public $cache = 'cache';
/**
* @var integer the time in seconds that the messages can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
* @see enableCaching
*/
public $cachingDuration = 0;
/**
* @var boolean whether to enable caching translated messages
*/
public $enableCaching = false;
/**
*
* @var TranslatorInterface
*/
public $translator;
/**
*
* @var boolean
*/
public $async = false;
/**
* 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.
* @throws InvalidConfigException if [[db]] is invalid or [[cache]] is invalid.
*/
public function init()
{
parent::init();
$this->translator = Instance::ensure($this->translator, 'conquer\i18n\TranslatorInterface');
if ($this->enableCaching) {
$this->cache = Instance::ensure($this->cache, Cache::className());
}
}
/**
* 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`.
*
* @param string $category the message category
* @param string $language the target language
* @return array the loaded messages. The keys are original messages, and the values
* are translated messages.
*/
protected function loadMessages($category, $language)
{
if ($this->enableCaching) {
$key = [
__CLASS__,
$category,
$language,
];
$messages = $this->cache->get($key);
if ($messages === false) {
$messages = I18nMessage::loadMessagesFromDb($category, $language);
$this->cache->set($key, $messages, $this->cachingDuration);
}
return $messages;
} else {
return I18nMessage::loadMessagesFromDb($category, $language);
}
}
/**
* Translates the specified message.
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
* If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
* If no fallback translation is provided this method will return `false`.
* @param string $category the category that the message belongs to.
* @param string $message the message to be translated.
* @param string $language the target language.
* @return string|boolean the translated message or false if translation wasn't found.
*/
protected function translateMessage($category, $message, $language)
{
$key = $language . '/' . $category;
if (!isset($this->_messages[$key])) {
$this->_messages[$key] = $this->loadMessages($category, $language);
}
if (isset($this->_messages[$key][$message])) {
return $this->_messages[$key][$message];
} else {
$i18nMessage = I18nMessage::getMessage($category, $message);
if ($this->async) {
$i18nMessage->getTranslation($language);
} else {
$i18nTranslator = I18nTranslator::getTranslator(get_class($this->translator));
$i18nTranslation = $i18nMessage->getTranslation($language, $i18nTranslator);
$i18nTranslation->translator_id = $i18nTranslator->translator_id;
$sourceLang = explode('-', $this->sourceLanguage)[0];
$targetLang = explode('-', $language)[0];
$translation = $this->translator->translate($message, $sourceLang, $targetLang);
if ($translation) {
$i18nTranslation->translation = $translation;
$i18nTranslation->status = I18nTranslation::STATUS_DONE;
$i18nTranslation->save(false);
return $this->_messages[$key][$message] = $translation;
} else {
$i18nTranslation->status = I18nTranslation::STATUS_ERROR;
$i18nTranslation->error_message = $this->translator->getError();
$i18nTranslation->save(false);
}
}
}
return $this->_messages[$key][$message] = false;
}
}