|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\I18n\Translator; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\I18n\MessageFormatterInterface; |
|
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Yiisoft\I18n\Event\MissingTranslationEvent; |
|
10
|
|
|
use Yiisoft\I18n\Locale; |
|
11
|
|
|
use Yiisoft\I18n\MessageReaderInterface; |
|
12
|
|
|
use Yiisoft\I18n\TranslatorInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Translator implements TranslatorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
17
|
|
|
private MessageReaderInterface $messageReader; |
|
18
|
|
|
private ?MessageFormatterInterface $messageFormatter; |
|
19
|
|
|
private array $messages = []; |
|
20
|
|
|
private ?string $locale = null; |
|
21
|
|
|
private ?string $defaultLocale = null; |
|
22
|
|
|
|
|
23
|
5 |
|
public function __construct( |
|
24
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
25
|
|
|
MessageReaderInterface $messageReader, |
|
26
|
|
|
MessageFormatterInterface $messageFormatter = null |
|
27
|
|
|
) { |
|
28
|
5 |
|
$this->messageReader = $messageReader; |
|
29
|
5 |
|
$this->eventDispatcher = $eventDispatcher; |
|
30
|
5 |
|
$this->messageFormatter = $messageFormatter; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Sets the current locale. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $locale The locale |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setLocale(string $locale): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->locale = $locale; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the current locale. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string The locale |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function getLocale(): string |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->locale ?? $this->getDefaultLocale(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $locale |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function setDefaultLocale(string $locale): void |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->defaultLocale = $locale; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function translate( |
|
65
|
|
|
?string $message, |
|
66
|
|
|
array $parameters = [], |
|
67
|
|
|
string $category = null, |
|
68
|
|
|
string $localeString = null |
|
69
|
|
|
): ?string { |
|
70
|
5 |
|
if ($localeString === null) { |
|
71
|
3 |
|
$localeString = $this->getLocale(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
if ($category === null) { |
|
75
|
3 |
|
$category = $this->getDefaultCategory(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
$messages = $this->getMessages($category, $localeString); |
|
79
|
|
|
|
|
80
|
5 |
|
if (!array_key_exists($message, $messages)) { |
|
81
|
1 |
|
$missingTranslation = new MissingTranslationEvent($category, $localeString, $message); |
|
|
|
|
|
|
82
|
1 |
|
$this->eventDispatcher->dispatch($missingTranslation); |
|
83
|
|
|
|
|
84
|
1 |
|
$locale = new Locale($localeString); |
|
85
|
1 |
|
$fallback = $locale->fallbackLocale(); |
|
86
|
|
|
|
|
87
|
1 |
|
if ($fallback->asString() !== $locale->asString()) { |
|
88
|
|
|
return $this->translate($message, $parameters, $category, $fallback->asString()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
$defaultFallback = (new Locale($this->getDefaultLocale()))->fallbackLocale(); |
|
92
|
|
|
|
|
93
|
1 |
|
if ($defaultFallback->asString() !== $fallback->asString()) { |
|
94
|
1 |
|
return $this->translate($message, $parameters, $category, $this->getDefaultLocale()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
$messages[$message] = $message; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
if ($this->messageFormatter === null) { |
|
101
|
4 |
|
return $messages[$message]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return $this->messageFormatter->format($messages[$message], $parameters, $localeString); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
private function getMessages(string $category, string $language): array |
|
108
|
|
|
{ |
|
109
|
5 |
|
$key = $language . '/' . $category; |
|
110
|
|
|
|
|
111
|
5 |
|
if (!array_key_exists($key, $this->messages)) { |
|
112
|
5 |
|
$this->messages[$key] = $this->messageReader->all($key); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
5 |
|
return $this->messages[$key]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
protected function getDefaultCategory(): string |
|
119
|
|
|
{ |
|
120
|
3 |
|
return 'default'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
protected function getDefaultLocale(): string |
|
124
|
|
|
{ |
|
125
|
4 |
|
return $this->defaultLocale ?? \Locale::getDefault(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|