1 | <?php |
||
24 | abstract class MessageSource extends Component |
||
25 | { |
||
26 | private $eventDispatcher; |
||
27 | |||
28 | /** |
||
29 | * @var bool whether to force message translation when the source and target languages are the same. |
||
30 | * Defaults to false, meaning translation is only performed when source and target languages are different. |
||
31 | */ |
||
32 | public $forceTranslation = false; |
||
33 | /** |
||
34 | * @var string the language that the original messages are in. If not set, it will use the value of |
||
35 | * [[\yii\base\Application::sourceLanguage]]. |
||
36 | */ |
||
37 | public $sourceLanguage = 'en-US'; |
||
38 | |||
39 | private $messages = []; |
||
40 | |||
41 | public function __construct(EventDispatcherInterface $eventDispatcher) |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Loads the message translation for the specified language and category. |
||
49 | * If translation for specific locale code such as `en-US` isn't found it |
||
50 | * tries more generic `en`. |
||
51 | * |
||
52 | * @param string $category the message category |
||
53 | * @param string $language the target language |
||
54 | * @return array the loaded messages. The keys are original messages, and the values |
||
55 | * are translated messages. |
||
56 | */ |
||
57 | abstract protected function loadMessages($category, $language): array; |
||
58 | |||
59 | /** |
||
60 | * Returns all messages for a given category in a given language. |
||
61 | * Returned value is a result of {@see loadMessages()} |
||
62 | * |
||
63 | * @param string $category |
||
64 | * @param string $language |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getMessages($category, $language) |
||
77 | |||
78 | /** |
||
79 | * Translates a message to the specified language. |
||
80 | * |
||
81 | * Note that unless [[forceTranslation]] is true, if the target language |
||
82 | * is the same as the [[sourceLanguage|source language]], the message |
||
83 | * will NOT be translated. |
||
84 | * |
||
85 | * If a translation is not found, a [[TranslationEvent::MISSING|missingTranslation]] event will be triggered. |
||
86 | * |
||
87 | * @param string $category the message category |
||
88 | * @param string $message the message to be translated |
||
89 | * @param string $language the target language |
||
90 | * @return string|null the translated message or false if translation wasn't found or isn't required |
||
91 | */ |
||
92 | public function translate($category, $message, $language): ?string |
||
100 | |||
101 | /** |
||
102 | * Translates the specified message. |
||
103 | * If the message is not found, a [[TranslationEvent::MISSING|missingTranslation]] event will be triggered. |
||
104 | * If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]]. |
||
105 | * If no fallback translation is provided this method will return `false`. |
||
106 | * @param string $category the category that the message belongs to. |
||
107 | * @param string $message the message to be translated. |
||
108 | * @param string $language the target language. |
||
109 | * @return string|null the translated message or null if translation wasn't found. |
||
110 | */ |
||
111 | protected function translateMessage($category, $message, $language): ?string |
||
129 | } |
||
130 |