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