1 | <?php |
||
23 | class MessageSource extends Component |
||
24 | { |
||
25 | /** |
||
26 | * @event MissingTranslationEvent an event that is triggered when a message translation is not found. |
||
27 | */ |
||
28 | const EVENT_MISSING_TRANSLATION = 'missingTranslation'; |
||
29 | |||
30 | /** |
||
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. |
||
33 | */ |
||
34 | public $forceTranslation = false; |
||
35 | /** |
||
36 | * @var string the language that the original messages are in. If not set, it will use the value of |
||
37 | * [[\yii\base\Application::sourceLanguage]]. |
||
38 | */ |
||
39 | public $sourceLanguage; |
||
40 | |||
41 | private $_messages = []; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Initializes this component. |
||
46 | */ |
||
47 | 561 | public function init() |
|
54 | |||
55 | /** |
||
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`. |
||
59 | * |
||
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. |
||
64 | */ |
||
65 | protected function loadMessages($category, $language) |
||
69 | |||
70 | /** |
||
71 | * Translates a message to the specified language. |
||
72 | * |
||
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. |
||
76 | * |
||
77 | * If a translation is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered. |
||
78 | * |
||
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 |
||
83 | */ |
||
84 | 561 | public function translate($category, $message, $language) |
|
92 | |||
93 | /** |
||
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. |
||
102 | */ |
||
103 | 173 | protected function translateMessage($category, $message, $language) |
|
127 | } |
||
128 |