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
|
|
|
public function __construct( |
28
|
|
|
EventDispatcherInterface $eventDispatcher, |
29
|
|
|
MessageReaderInterface $messageReader |
30
|
|
|
) { |
31
|
|
|
$this->messageReader = $messageReader; |
32
|
|
|
$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
|
|
|
public function translate(?string $message, string $category = null, string $localeString = null): ?string |
45
|
|
|
{ |
46
|
|
|
if ($localeString === null) { |
47
|
|
|
$localeString = $this->getDefaultLocale(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($category === null) { |
51
|
|
|
$category = $this->getDefaultCategory(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$messages = $this->getMessages($category, $localeString); |
55
|
|
|
|
56
|
|
|
if (array_key_exists($message, $messages)) { |
57
|
|
|
return $messages[$message]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$missingTranslation = new MissingTranslationEvent($category, $localeString, $message); |
|
|
|
|
61
|
|
|
$this->eventDispatcher->dispatch($missingTranslation); |
62
|
|
|
|
63
|
|
|
$locale = new Locale($localeString); |
64
|
|
|
$fallback = $locale->fallbackLocale(); |
65
|
|
|
|
66
|
|
|
if ($fallback->asString() !== $locale->asString()) { |
67
|
|
|
return $messages[$message] = $this->translate($message, $category, $fallback->asString()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $messages[$message] = $message; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getMessages(string $category, string $language): array |
74
|
|
|
{ |
75
|
|
|
$key = $language . '/' . $category; |
76
|
|
|
|
77
|
|
|
if (!array_key_exists($key, $this->messages)) { |
78
|
|
|
$this->messages[$key] = $this->messageReader->all($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->messages[$key]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getDefaultCategory(): string |
85
|
|
|
{ |
86
|
|
|
return 'default'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getDefaultLocale(): string |
90
|
|
|
{ |
91
|
|
|
return \Locale::getDefault(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|