1 | <?php |
||
2 | |||
3 | namespace Yiisoft\I18n\Translator; |
||
4 | |||
5 | use Psr\EventDispatcher\EventDispatcherInterface; |
||
6 | use Yiisoft\I18n\Event\MissingTranslationEvent; |
||
7 | use Yiisoft\I18n\Locale; |
||
8 | use Yiisoft\I18n\MessageReaderInterface; |
||
9 | use Yiisoft\I18n\TranslatorInterface; |
||
10 | |||
11 | class Translator implements TranslatorInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var \Yiisoft\I18n\MessageReaderInterface |
||
15 | */ |
||
16 | private $messageReader; |
||
17 | /** |
||
18 | * @var \Psr\EventDispatcher\EventDispatcherInterface |
||
19 | */ |
||
20 | private $eventDispatcher; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $messages = []; |
||
26 | |||
27 | 4 | public function __construct( |
|
28 | EventDispatcherInterface $eventDispatcher, |
||
29 | MessageReaderInterface $messageReader |
||
30 | ) { |
||
31 | 4 | $this->messageReader = $messageReader; |
|
32 | 4 | $this->eventDispatcher = $eventDispatcher; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * Translates a message to the specified language. |
||
37 | * If a translation is not found, a {{@see \Yiisoft\I18n\Event\MissingTranslationEvent} event will be triggered. |
||
38 | * |
||
39 | * @param string $message the message to be translated |
||
40 | * @param string $category the message category |
||
41 | * @param string $localeString the target locale |
||
42 | * @return string|null the translated message or false if translation wasn't found or isn't required |
||
43 | */ |
||
44 | 4 | public function translate(?string $message, string $category = null, string $localeString = null): ?string |
|
45 | { |
||
46 | 4 | if ($localeString === null) { |
|
47 | 3 | $localeString = $this->getDefaultLocale(); |
|
48 | } |
||
49 | |||
50 | 4 | if ($category === null) { |
|
51 | 3 | $category = $this->getDefaultCategory(); |
|
52 | } |
||
53 | |||
54 | 4 | $messages = $this->getMessages($category, $localeString); |
|
55 | |||
56 | 4 | if (array_key_exists($message, $messages)) { |
|
57 | 3 | return $messages[$message]; |
|
58 | } |
||
59 | |||
60 | 1 | $missingTranslation = new MissingTranslationEvent($category, $localeString, $message); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
61 | 1 | $this->eventDispatcher->dispatch($missingTranslation); |
|
62 | |||
63 | 1 | $locale = new Locale($localeString); |
|
64 | 1 | $fallback = $locale->fallbackLocale(); |
|
65 | |||
66 | 1 | if ($fallback->asString() !== $locale->asString()) { |
|
67 | return $messages[$message] = $this->translate($message, $category, $fallback->asString()); |
||
68 | } |
||
69 | |||
70 | 1 | return $messages[$message] = $message; |
|
71 | } |
||
72 | |||
73 | 4 | private function getMessages(string $category, string $language): array |
|
74 | { |
||
75 | 4 | $key = $language . '/' . $category; |
|
76 | |||
77 | 4 | if (!array_key_exists($key, $this->messages)) { |
|
78 | 4 | $this->messages[$key] = $this->messageReader->all($key); |
|
79 | } |
||
80 | |||
81 | 4 | return $this->messages[$key]; |
|
82 | } |
||
83 | |||
84 | 3 | protected function getDefaultCategory(): string |
|
85 | { |
||
86 | 3 | return 'default'; |
|
87 | } |
||
88 | |||
89 | 3 | protected function getDefaultLocale(): string |
|
90 | { |
||
91 | 3 | return \Locale::getDefault(); |
|
92 | } |
||
93 | } |
||
94 |