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