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 ?string $locale = null; |
20
|
|
|
private ?string $defaultLocale = null; |
21
|
|
|
|
22
|
4 |
|
public function __construct( |
23
|
|
|
EventDispatcherInterface $eventDispatcher, |
24
|
|
|
MessageReaderInterface $messageReader, |
25
|
|
|
MessageFormatterInterface $messageFormatter = null |
26
|
|
|
) { |
27
|
4 |
|
$this->messageReader = $messageReader; |
28
|
4 |
|
$this->eventDispatcher = $eventDispatcher; |
29
|
4 |
|
$this->messageFormatter = $messageFormatter; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the current locale. |
34
|
|
|
* |
35
|
|
|
* @param string $locale The locale |
36
|
|
|
*/ |
37
|
|
|
public function setLocale(string $locale): void |
38
|
|
|
{ |
39
|
|
|
$this->locale = $locale; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the current locale. |
44
|
|
|
* |
45
|
|
|
* @return string The locale |
46
|
|
|
*/ |
47
|
2 |
|
public function getLocale(): string |
48
|
|
|
{ |
49
|
2 |
|
return $this->locale ?? $this->getDefaultLocale(); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function setDefaultLocale(string $locale): void |
53
|
|
|
{ |
54
|
2 |
|
$this->defaultLocale = $locale; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function translate( |
58
|
|
|
string $id, |
59
|
|
|
array $parameters = [], |
60
|
|
|
string $category = null, |
61
|
|
|
string $localeString = null |
62
|
|
|
): ?string { |
63
|
4 |
|
if ($localeString === null) { |
64
|
2 |
|
$localeString = $this->getLocale(); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
if ($category === null) { |
68
|
2 |
|
$category = $this->getDefaultCategory(); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
$message = $this->messageReader->one($id, $localeString . '/' . $category); |
72
|
4 |
|
if ($message === null) { |
73
|
1 |
|
$missingTranslation = new MissingTranslationEvent($category, $localeString, $id); |
74
|
1 |
|
$this->eventDispatcher->dispatch($missingTranslation); |
75
|
|
|
|
76
|
1 |
|
$locale = new Locale($localeString); |
77
|
1 |
|
$fallback = $locale->fallbackLocale(); |
78
|
|
|
|
79
|
1 |
|
if ($fallback->asString() !== $locale->asString()) { |
80
|
|
|
return $this->translate($id, $parameters, $category, $fallback->asString()); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$defaultFallback = (new Locale($this->getDefaultLocale()))->fallbackLocale(); |
84
|
|
|
|
85
|
1 |
|
if ($defaultFallback->asString() !== $fallback->asString()) { |
86
|
1 |
|
return $this->translate($id, $parameters, $category, $this->getDefaultLocale()); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$message = $id; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
if ($this->messageFormatter === null) { |
93
|
3 |
|
return $message; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $this->messageFormatter->format($message, $parameters, $localeString); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
protected function getDefaultCategory(): string |
100
|
|
|
{ |
101
|
2 |
|
return 'default'; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
protected function getDefaultLocale(): string |
105
|
|
|
{ |
106
|
3 |
|
return $this->defaultLocale ?? \Locale::getDefault(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|