|
1
|
|
|
<?php |
|
2
|
|
|
namespace l10n\Translator; |
|
3
|
|
|
|
|
4
|
|
|
use l10n\Plural\IPlural; |
|
5
|
|
|
|
|
6
|
|
|
class Translator { |
|
7
|
|
|
/** @var \l10n\Plural\IPlural */ |
|
8
|
|
|
protected $plural; |
|
9
|
|
|
|
|
10
|
|
|
/** @var array */ |
|
11
|
|
|
private $translated = array(); |
|
12
|
|
|
|
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
private $untranslated = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** @var \l10n\Translator\IStorage */ |
|
17
|
|
|
protected $storage; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param \l10n\Plural\IPlural $plural |
|
21
|
|
|
* @param \l10n\Translator\IStorage|null $storage |
|
22
|
|
|
*/ |
|
23
|
9 |
|
public function __construct(IPlural $plural, IStorage $storage = null) { |
|
24
|
9 |
|
$this->plural = $plural; |
|
25
|
|
|
|
|
26
|
9 |
|
if ($storage) { |
|
27
|
1 |
|
$this->storage = $storage; |
|
28
|
1 |
|
$this->storage->load($this); |
|
29
|
1 |
|
} |
|
30
|
9 |
|
} |
|
31
|
|
|
|
|
32
|
9 |
|
public function __destruct() { |
|
33
|
9 |
|
if ($this->storage instanceof IStorage) { |
|
34
|
1 |
|
$this->storage->save($this); |
|
35
|
1 |
|
} |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return \l10n\Plural\IPlural |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function getPlural() { |
|
42
|
1 |
|
return $this->plural; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $plural |
|
47
|
|
|
*/ |
|
48
|
8 |
|
protected function checkPlural($plural) { |
|
49
|
8 |
|
$max = $this->plural->getPluralsCount() - 1; |
|
50
|
|
|
|
|
51
|
8 |
|
if ($plural > $max) { |
|
52
|
1 |
|
throw new \RangeException(sprintf('The plural (%d) is bigger than is allowed (%d)', $plural, $max)); |
|
53
|
|
|
} |
|
54
|
8 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param string $text |
|
59
|
|
|
* @param int $plural |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function setText($key, $text, $plural = 0) { |
|
62
|
6 |
|
$this->checkPlural($plural); |
|
63
|
6 |
|
$this->translated[$key][$plural] = $text; |
|
64
|
6 |
|
$this->removeUntranslated($key, $plural); |
|
65
|
6 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param int $plural |
|
70
|
|
|
* @return null|string |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public function getText($key, $plural = 0) { |
|
73
|
4 |
|
$this->checkPlural($plural); |
|
74
|
|
|
|
|
75
|
4 |
|
if (func_num_args() === 2) { |
|
76
|
4 |
|
if (isset($this->translated[$key][$plural])) { |
|
77
|
2 |
|
$this->removeUntranslated($key, $plural); |
|
78
|
|
|
|
|
79
|
2 |
|
return $this->translated[$key][$plural]; |
|
80
|
|
|
} |
|
81
|
3 |
|
} |
|
82
|
|
|
else { |
|
83
|
1 |
|
if (isset($this->translated[$key])) { |
|
84
|
1 |
|
$this->removeUntranslated($key); |
|
85
|
|
|
|
|
86
|
1 |
|
return $this->translated[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $key |
|
95
|
|
|
* @param int $plural Nothing for all plurals |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function removeText($key, $plural = 0) { |
|
98
|
2 |
|
if (func_num_args() === 2) { |
|
99
|
2 |
|
unset($this->translated[$key][$plural]); |
|
100
|
2 |
|
$this->removeUntranslated($key, $plural); |
|
101
|
2 |
|
} |
|
102
|
|
|
else { |
|
103
|
1 |
|
unset($this->translated[$key]); |
|
104
|
1 |
|
$this->removeUntranslated($key); |
|
105
|
|
|
} |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $key |
|
110
|
|
|
* @param int $plural |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function setUntranslated($key, $plural = 0) { |
|
113
|
3 |
|
$this->checkPlural($plural); |
|
114
|
3 |
|
$this->untranslated[$key][$plural] = true; |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $key |
|
119
|
|
|
* @param int $plural Nothing for all plurals |
|
120
|
|
|
*/ |
|
121
|
7 |
|
public function removeUntranslated($key, $plural = 0) { |
|
122
|
7 |
|
if (func_num_args() === 2) { |
|
123
|
7 |
|
unset($this->untranslated[$key][$plural]); |
|
124
|
|
|
|
|
125
|
7 |
|
if (empty($this->untranslated[$key])) { |
|
126
|
7 |
|
unset($this->untranslated[$key]); |
|
127
|
7 |
|
} |
|
128
|
7 |
|
} |
|
129
|
|
|
else { |
|
130
|
2 |
|
unset($this->untranslated[$key]); |
|
131
|
|
|
} |
|
132
|
7 |
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
public function clearTranslated() { |
|
135
|
1 |
|
$this->translated = array(); |
|
136
|
1 |
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public function clearUntranslated() { |
|
139
|
1 |
|
$this->untranslated = array(); |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $key |
|
144
|
|
|
* @param int|array|null $n When $n is null, than singular will be selected. When $n is an array, it's used as $parameters. |
|
145
|
|
|
* @param array $parameters |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
3 |
|
public function translate($key, $n = null, array $parameters = array()) { |
|
149
|
3 |
|
if (is_array($n)) { |
|
150
|
1 |
|
$parameters = $n; |
|
151
|
1 |
|
$n = null; |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
3 |
|
$plural = 0; |
|
155
|
|
|
|
|
156
|
3 |
|
if ($n !== null) { |
|
157
|
3 |
|
$plural = $this->plural->getPlural($n); |
|
158
|
3 |
|
} |
|
159
|
|
|
|
|
160
|
3 |
|
$translated = $this->getText($key, $plural); |
|
161
|
|
|
|
|
162
|
3 |
|
if ($translated === null) { |
|
163
|
2 |
|
$this->setUntranslated($key, $plural); |
|
164
|
|
|
|
|
165
|
2 |
|
return $key; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
$this->removeUntranslated($key, $plural); |
|
169
|
|
|
|
|
170
|
1 |
|
$parameters += array('%n%' => $n); |
|
171
|
1 |
|
$translated = strtr($translated, $parameters); |
|
172
|
|
|
|
|
173
|
1 |
|
return $translated; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
2 |
|
public function getTranslated() { |
|
180
|
2 |
|
return $this->translated; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
3 |
|
public function getUntranslated() { |
|
187
|
3 |
|
return $this->untranslated; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|