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\Component;
14
* MessageSource is the base class for message translation repository classes.
16
* A message source stores message translations in some persistent storage.
18
* Child classes should override [[loadMessages()]] to provide translated messages.
20
* @author Qiang Xue <qiang.xue@gmail.com>
23
class MessageSource extends Component
26
* @event MissingTranslationEvent an event that is triggered when a message translation is not found.
28
const EVENT_MISSING_TRANSLATION = 'missingTranslation';
31
* @var bool whether to force message translation when the source and target languages are the same.
32
* Defaults to false, meaning translation is only performed when source and target languages are different.
34
public $forceTranslation = false;
36
* @var string|null the language that the original messages are in. If not set, it will use the value of
37
* [[\yii\base\Application::sourceLanguage]].
39
public $sourceLanguage;
41
private $_messages = [];
45
* Initializes this component.
47
public function init()
50
if ($this->sourceLanguage === null) {
51
$this->sourceLanguage = Yii::$app->sourceLanguage;
56
* Loads the message translation for the specified language and category.
57
* If translation for specific locale code such as `en-US` isn't found it
58
* tries more generic `en`.
60
* @param string $category the message category
61
* @param string $language the target language
62
* @return array the loaded messages. The keys are original messages, and the values
63
* are translated messages.
65
protected function loadMessages($category, $language)
71
* Translates a message to the specified language.
73
* Note that unless [[forceTranslation]] is true, if the target language
74
* is the same as the [[sourceLanguage|source language]], the message
75
* will NOT be translated.
77
* If a translation is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
79
* @param string $category the message category
80
* @param string $message the message to be translated
81
* @param string $language the target language
82
* @return string|bool the translated message or false if translation wasn't found or isn't required
84
public function translate($category, $message, $language)
86
if ($this->forceTranslation || $language !== $this->sourceLanguage) {
87
return $this->translateMessage($category, $message, $language);
94
* Translates the specified message.
95
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
96
* If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
97
* If no fallback translation is provided this method will return `false`.
98
* @param string $category the category that the message belongs to.
99
* @param string $message the message to be translated.
100
* @param string $language the target language.
101
* @return string|bool the translated message or false if translation wasn't found.
103
protected function translateMessage($category, $message, $language)
105
$key = $language . '/' . $category;
106
if (!isset($this->_messages[$key])) {
107
$this->_messages[$key] = $this->loadMessages($category, $language);
109
if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '') {
110
return $this->_messages[$key][$message];
111
} elseif ($this->hasEventHandlers(self::EVENT_MISSING_TRANSLATION)) {
112
$event = new MissingTranslationEvent([
113
'category' => $category,
114
'message' => $message,
115
'language' => $language,
117
$this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
118
if ($event->translatedMessage !== null) {
119
return $this->_messages[$key][$message] = $event->translatedMessage;
123
return $this->_messages[$key][$message] = false;