|
1
|
|
|
<?php |
|
2
|
|
|
namespace l10nNetteTranslator; |
|
3
|
|
|
|
|
4
|
|
|
use l10n\Language\ILanguage; |
|
5
|
|
|
use l10n\Plural\IPlural; |
|
6
|
|
|
use l10nNetteTranslator\Storage\IStorage; |
|
7
|
|
|
use Nette\InvalidStateException; |
|
8
|
|
|
use Nette\Localization\ITranslator; |
|
9
|
|
|
use Nette\Object; |
|
10
|
|
|
|
|
11
|
|
|
class Translator extends Object implements ITranslator { |
|
12
|
|
|
/** @var \l10nNetteTranslator\LanguageAndPlural[] */ |
|
13
|
|
|
private $languages_and_plurals; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \l10nNetteTranslator\Storage\IStorage */ |
|
16
|
|
|
private $storage; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \l10n\Translator\Translator */ |
|
19
|
|
|
private $translator; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $active_language_code; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $code |
|
26
|
|
|
*/ |
|
27
|
12 |
|
protected function testLanguageCode($code) { |
|
28
|
12 |
|
if (empty($this->languages_and_plurals[$code])) { |
|
29
|
4 |
|
throw new InvalidStateException(sprintf('Language with code "%s" is not set', $code)); |
|
30
|
|
|
} |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param \l10n\Language\ILanguage $language |
|
35
|
|
|
* @param \l10n\Plural\IPlural $plural |
|
36
|
|
|
* @param bool $default |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function addLanguageAndPlural(ILanguage $language, IPlural $plural, $default = false) { |
|
39
|
1 |
|
$language_and_plural = new LanguageAndPlural(); |
|
40
|
1 |
|
$language_and_plural->setLanguage($language); |
|
41
|
1 |
|
$language_and_plural->setPlural($plural); |
|
42
|
1 |
|
$code = $language->getIso639_1(); |
|
43
|
|
|
|
|
44
|
1 |
|
$this->languages_and_plurals[$code] = $language_and_plural; |
|
45
|
|
|
|
|
46
|
1 |
|
if ($default || !$this->active_language_code) { |
|
47
|
1 |
|
$this->setActiveLanguageCode($code); |
|
48
|
1 |
|
} |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $code |
|
53
|
|
|
* @throws InvalidStateException |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function setActiveLanguageCode($code) { |
|
56
|
4 |
|
$this->testLanguageCode($code); |
|
57
|
3 |
|
$this->active_language_code = $code; |
|
58
|
3 |
|
$this->translator = null; |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string|null |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function getActiveLanguageCode() { |
|
65
|
1 |
|
return $this->active_language_code; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $code |
|
70
|
|
|
* @return \l10nNetteTranslator\LanguageAndPlural |
|
71
|
|
|
* @throws InvalidStateException |
|
72
|
|
|
*/ |
|
73
|
6 |
|
public function getLanguageAndPluralByCode($code) { |
|
74
|
6 |
|
$this->testLanguageCode($code); |
|
75
|
|
|
|
|
76
|
4 |
|
return $this->languages_and_plurals[$code]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $code |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function hasLanguageAndPluralByCode($code) { |
|
84
|
1 |
|
return isset($this->languages_and_plurals[$code]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \l10nNetteTranslator\LanguageAndPlural |
|
89
|
|
|
* @throws InvalidStateException |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function getActiveLanguageAndPlural() { |
|
92
|
4 |
|
return $this->getLanguageAndPluralByCode($this->active_language_code); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \l10nNetteTranslator\LanguageAndPlural[] |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getLanguagesAndPlurals() { |
|
99
|
1 |
|
return $this->languages_and_plurals; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return \l10nNetteTranslator\Storage\IStorage |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function getStorage() { |
|
106
|
3 |
|
return $this->storage; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param \l10nNetteTranslator\Storage\IStorage $storage |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function setStorage(IStorage $storage) { |
|
113
|
2 |
|
$this->storage = $storage; |
|
114
|
2 |
|
$this->translator = null; |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return \l10n\Translator\Translator |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function getTranslator() { |
|
121
|
2 |
|
if (!($this->translator instanceof \l10n\Translator\Translator)) { |
|
122
|
2 |
|
$plural = $this->getActiveLanguageAndPlural()->getPlural(); |
|
123
|
2 |
|
$storage = $this->getStorage(); |
|
124
|
|
|
|
|
125
|
2 |
|
if ($storage) { |
|
126
|
1 |
|
$storage->setTranslator($this); |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
$this->translator = new \l10n\Translator\Translator($plural, $storage); |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
return $this->translator; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $key |
|
137
|
|
|
* @param int|array|null $n When $n is null, than singular will be selected. When $n is an array, it's used as $parameters. |
|
138
|
|
|
* @param array $parameters |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function translate($key, $n = null, array $parameters = []) { |
|
142
|
1 |
|
return $this->getTranslator()->translate($key, $n, $parameters); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|