1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
<?php
class I18nData {
const REFERENCE_LANGUAGE = 'en';
private $data = array();
private $ignore = array();
public function __construct($data, $ignore) {
$this->data = $data;
$this->ignore = $ignore;
$this->synchonizeKeys();
}
public function getData() {
$output = array();
$reference = $this->getReferenceLanguage();
$languages = $this->getNonReferenceLanguages();
foreach ($reference as $file => $values) {
foreach ($values as $key => $value) {
$output[static::REFERENCE_LANGUAGE][$file][$key] = $value;
foreach ($languages as $language) {
if ($this->data[$language][$file][$key] !== $value) {
// This value is translated, there is no need to flag it.
$output[$language][$file][$key] = $this->data[$language][$file][$key];
} elseif (array_key_exists($language, $this->ignore) && in_array($key, $this->ignore[$language])) {
// This value is ignored, there is no need to flag it.
$output[$language][$file][$key] = $this->data[$language][$file][$key];
} else {
// This value is not translated nor ignored, it must be flagged.
$output[$language][$file][$key] = "{$value} -> todo";
}
}
}
}
return $output;
}
public function getIgnore() {
$ignore = array();
foreach ($this->ignore as $language => $keys) {
sort($keys);
$ignore[$language] = $keys;
}
return $ignore;
}
private function synchonizeKeys() {
$this->addMissingKeysFromReference();
$this->removeExtraKeysFromOtherLanguages();
$this->removeUnknownIgnoreKeys();
}
private function addMissingKeysFromReference() {
$reference = $this->getReferenceLanguage();
$languages = $this->getNonReferenceLanguages();
foreach ($reference as $file => $values) {
foreach ($values as $key => $value) {
foreach ($languages as $language) {
if (!array_key_exists($key, $this->data[$language][$file])) {
$this->data[$language][$file][$key] = $value;
}
}
}
}
}
private function removeExtraKeysFromOtherLanguages() {
$reference = $this->getReferenceLanguage();
foreach ($this->getNonReferenceLanguages() as $language) {
foreach ($this->getLanguage($language) as $file => $values) {
foreach ($values as $key => $value) {
if (!array_key_exists($key, $reference[$file])) {
unset($this->data[$language][$file][$key]);
}
}
}
}
}
private function removeUnknownIgnoreKeys() {
$reference = $this->getReferenceLanguage();
foreach ($this->ignore as $language => $keys) {
foreach ($keys as $index => $key) {
if (!array_key_exists($this->getFilenamePrefix($key), $reference) || !array_key_exists($key, $reference[$this->getFilenamePrefix($key)])) {
unset($this->ignore[$language][$index]);
}
}
}
}
/**
* Return the available languages
*
* @return array
*/
public function getAvailableLanguages() {
$languages = array_keys($this->data);
sort($languages);
return $languages;
}
/**
* Return all available languages without the reference language
*
* @return array
*/
public function getNonReferenceLanguages() {
return array_filter(array_keys($this->data), function ($value) {
return static::REFERENCE_LANGUAGE !== $value;
});
}
/**
* Add a new language. It's a copy of the reference language.
*
* @param string $language
* @throws Exception
*/
public function addLanguage($language) {
if (array_key_exists($language, $this->data)) {
throw new Exception('The selected language already exist.');
}
$this->data[$language] = $this->data[static::REFERENCE_LANGUAGE];
}
/**
* Check if the key is known.
*
* @param string $key
* @return bool
*/
public function isKnown($key) {
return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
}
/**
* Add a new key to all languages.
*
* @param string $key
* @param string $value
* @throws Exception
*/
public function addKey($key, $value) {
if ($this->isKnown($key)) {
throw new Exception('The selected key already exist.');
}
foreach ($this->getAvailableLanguages() as $language) {
if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
$this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
}
}
}
/**
* Add a value for a key for the selected language.
*
* @param string $key
* @param string $value
* @param string $language
* @throws Exception
*/
public function addValue($key, $value, $language) {
if (!in_array($language, $this->getAvailableLanguages())) {
throw new Exception('The selected language does not exist.');
}
if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
throw new Exception('The selected key does not exist for the selected language.');
}
$this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
}
/**
* Remove a key in all languages
*
* @param string $key
* @throws Exception
*/
public function removeKey($key) {
if (!$this->isKnown($key)) {
throw new Exception('The selected key does not exist.');
}
foreach ($this->getAvailableLanguages() as $language) {
if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
}
if (array_key_exists($language, $this->ignore) && $position = array_search($key, $this->ignore[$language])) {
unset($this->ignore[$language][$position]);
}
}
}
/**
* Ignore a key from a language, or reverse it.
*
* @param string $key
* @param string $language
* @param boolean $reverse
*/
public function ignore($key, $language, $reverse = false) {
if (!array_key_exists($language, $this->ignore)) {
$this->ignore[$language] = array();
}
$index = array_search($key, $this->ignore[$language]);
if (false !== $index && $reverse) {
unset($this->ignore[$language][$index]);
return;
}
if (false !== $index && !$reverse) {
return;
}
$this->ignore[$language][] = $key;
}
public function getLanguage($language) {
return $this->data[$language];
}
public function getReferenceLanguage() {
return $this->getLanguage(static::REFERENCE_LANGUAGE);
}
/**
* @param string $key
* @return string
*/
private function getFilenamePrefix($key) {
return preg_replace('/\..*/', '.php', $key);
}
}
|