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
|
|
|
public function __construct( |
24
|
|
|
EventDispatcherInterface $eventDispatcher, |
25
|
|
|
MessageReaderInterface $messageReader, |
26
|
|
|
MessageFormatterInterface $messageFormatter = null |
27
|
4 |
|
) { |
28
|
|
|
$this->messageReader = $messageReader; |
29
|
|
|
$this->eventDispatcher = $eventDispatcher; |
30
|
|
|
$this->messageFormatter = $messageFormatter; |
31
|
4 |
|
} |
32
|
4 |
|
|
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
|
4 |
|
* Returns the current locale. |
45
|
|
|
* |
46
|
4 |
|
* @return string The locale |
47
|
3 |
|
*/ |
48
|
|
|
public function getLocale(): string |
49
|
|
|
{ |
50
|
4 |
|
return $this->locale ?? $this->getDefaultLocale(); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
4 |
|
* @param string $locale |
55
|
|
|
*/ |
56
|
4 |
|
public function setDefaultLocale(string $locale): void |
57
|
3 |
|
{ |
58
|
|
|
$this->defaultLocale = $locale; |
59
|
|
|
} |
60
|
1 |
|
|
61
|
1 |
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
1 |
|
*/ |
64
|
1 |
|
public function translate( |
65
|
|
|
?string $message, |
66
|
1 |
|
array $parameters = [], |
67
|
|
|
string $category = null, |
68
|
|
|
string $localeString = null |
69
|
|
|
): ?string { |
70
|
1 |
|
if ($localeString === null) { |
71
|
|
|
$localeString = $this->getLocale(); |
72
|
|
|
} |
73
|
4 |
|
|
74
|
|
|
if ($category === null) { |
75
|
4 |
|
$category = $this->getDefaultCategory(); |
76
|
|
|
} |
77
|
4 |
|
|
78
|
4 |
|
$messages = $this->getMessages($category, $localeString); |
79
|
|
|
|
80
|
|
|
if (!array_key_exists($message, $messages)) { |
81
|
4 |
|
$missingTranslation = new MissingTranslationEvent($category, $localeString, $message); |
|
|
|
|
82
|
|
|
$this->eventDispatcher->dispatch($missingTranslation); |
83
|
|
|
|
84
|
3 |
|
$locale = new Locale($localeString); |
85
|
|
|
$fallback = $locale->fallbackLocale(); |
86
|
3 |
|
|
87
|
|
|
if ($fallback->asString() !== $locale->asString()) { |
88
|
|
|
return $this->translate($message, $parameters, $category, $fallback->asString()); |
89
|
3 |
|
} |
90
|
|
|
|
91
|
3 |
|
$defaultFallback = (new Locale($this->getDefaultLocale()))->fallbackLocale(); |
92
|
|
|
|
93
|
|
|
if ($defaultFallback->asString() !== $fallback->asString()) { |
94
|
|
|
return $this->translate($message, $parameters, $category, $this->getDefaultLocale()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$messages[$message] = $message; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->messageFormatter === null) { |
101
|
|
|
return $messages[$message]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->messageFormatter->format($messages[$message], $parameters, $localeString); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function getMessages(string $category, string $language): array |
108
|
|
|
{ |
109
|
|
|
$key = $language . '/' . $category; |
110
|
|
|
|
111
|
|
|
if (!array_key_exists($key, $this->messages)) { |
112
|
|
|
$this->messages[$key] = $this->messageReader->all($key); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->messages[$key]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function getDefaultCategory(): string |
119
|
|
|
{ |
120
|
|
|
return 'default'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getDefaultLocale(): string |
124
|
|
|
{ |
125
|
|
|
return $this->defaultLocale ?? \Locale::getDefault(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|