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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*
*/
namespace Piwik\Translate;
use Exception;
use Piwik\Common;
use Piwik\PluginsManager;
use Piwik\Translate\Filter\FilterAbstract;
use Piwik\Translate\Validate\ValidateAbstract;
/**
* Writes clean translations to file
*
* @package Piwik
* @package Piwik_Translate
*/
class Writer
{
/**
* current language to write files for
*
* @var string
*/
protected $_language = '';
/**
* Name of a plugin (if set in contructor)
*
* @var string|null
*/
protected $_pluginName = null;
/**
* translations to write to file
*
* @var array
*/
protected $_translations = array();
/**
* Validators to check translations with
*
* @var ValidateAbstract[]
*/
protected $_validators = array();
/**
* Message why validation failed
*
* @var string|null
*/
protected $_validationMessage = null;
/**
* Filters to to apply to translations
*
* @var FilterAbstract[]
*/
protected $_filters = array();
/**
* Messages which filter changed the data
*
* @var array
*/
protected $_filterMessages = array();
const __UNFILTERED__ = 'unfiltered';
const __FILTERED__ = 'filtered';
protected $_currentState = self::__UNFILTERED__;
/**
* If $pluginName is given, Writer will be initialized for the given plugin if it exists
* Otherwise it will be initialized for core translations
*
* @param string $language ISO 639-1 alpha-2 language code
* @param string $pluginName optional plugin name
* @throws \Exception
*/
public function __construct($language, $pluginName=null)
{
$this->setLanguage($language);
if (!empty($pluginName)) {
$installedPlugins = PluginsManager::getInstance()->readPluginsDirectory();
if (!in_array($pluginName, $installedPlugins)) {
throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($pluginName)));
}
$this->_pluginName = $pluginName;
}
}
/**
* @param string $language ISO 639-1 alpha-2 language code
*
* @throws \Exception
*/
public function setLanguage($language)
{
if (!preg_match('/^([a-z]{2,3}(-[a-z]{2,3})?)$/i', $language)) {
throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($language)));
}
$this->_language = strtolower($language);
}
/**
* @return string ISO 639-1 alpha-2 language code
*/
public function getLanguage()
{
return $this->_language;
}
/**
* Returns if there are translations available or not
* @return bool
*/
public function hasTranslations()
{
return !empty($this->_translations);
}
/**
* Set the translations to write (and cleans them)
*
* @param $translations
*/
public function setTranslations($translations)
{
$this->_currentState = self::__UNFILTERED__;
$this->_translations = $translations;
$this->_applyFilters();
}
/**
* Get translations from file
*
* @param string $lang ISO 639-1 alpha-2 language code
* @throws Exception
* @return array Array of translations ( plugin => ( key => translated string ) )
*/
public function getTranslations($lang)
{
$path = $this->_getTranslationPath('lang', $lang);
if (!is_readable($path)) {
return array();
}
$data = file_get_contents($path);
$translations = json_decode($data, true);
return $translations;
}
/**
* Returns the temporary path for translations
*
* @return string
*/
public function getTemporaryTranslationPath()
{
return $this->_getTranslationPath('tmp');
}
/**
* Returns the path to translation files
*
* @return string
*/
public function getTranslationPath()
{
return $this->_getTranslationPath('lang');
}
/**
* Get translation file path based on given params
*
* @param string $base Optional base directory (either 'lang' or 'tmp')
* @param string|null $lang forced language
* @throws \Exception
* @return string path
*/
protected function _getTranslationPath($base, $lang=null)
{
if (empty($lang)) $lang = $this->getLanguage();
if (!empty($this->_pluginName)) {
if ($base == 'tmp') {
return sprintf('%s/tmp/plugins/%s/lang/%s.json', PIWIK_INCLUDE_PATH, $this->_pluginName, $lang);
} else {
return sprintf('%s/plugins/%s/lang/%s.json', PIWIK_INCLUDE_PATH, $this->_pluginName, $lang);
}
}
return sprintf('%s/%s/%s.json', PIWIK_INCLUDE_PATH, $base, $lang);
}
/**
* Converts translations to a string that can be written to a file
*
* @return string
*/
public function __toString()
{
/*
* Use JSON_UNESCAPED_UNICODE and JSON_PRETTY_PRINT for PHP >= 5.4
*/
$options = 0;
if (defined('JSON_UNESCAPED_UNICODE')) $options |= JSON_UNESCAPED_UNICODE;
if (defined('JSON_PRETTY_PRINT')) $options |= JSON_PRETTY_PRINT;
return json_encode($this->_translations, $options);
}
/**
* Save translations to file; translations should already be cleaned.
*
* @throws \Exception
* @return bool|int False if failure, or number of bytes written
*/
public function save()
{
$this->_applyFilters();
if (!$this->hasTranslations() || !$this->isValid()) {
throw new Exception('unable to save empty or invalid translations');
}
$path = $this->getTranslationPath();
Common::mkdir(dirname($path));
return file_put_contents($path, $this->__toString());
}
/**
* Save translations to temporary file; translations should already be cleansed.
*
* @throws \Exception
* @return bool|int False if failure, or number of bytes written
*/
public function saveTemporary()
{
$this->_applyFilters();
if (!$this->hasTranslations() || !$this->isValid()) {
throw new Exception('unable to save empty or invalid translations');
}
$path = $this->getTemporaryTranslationPath();
Common::mkdir(dirname($path));
return file_put_contents($path, $this->__toString());
}
/**
* Adds an validator to check before saving
*
* @param ValidateAbstract $validator
*/
public function addValidator(ValidateAbstract $validator)
{
$this->_validators[] = $validator;
}
/**
* Returns if translations are valid to save or not
*
* @return bool
*/
public function isValid()
{
$this->_applyFilters();
$this->_validationMessage = null;
foreach ($this->_validators AS $validator) {
if (!$validator->isValid($this->_translations)) {
$this->_validationMessage = $validator->getMessage();
return false;
}
}
return true;
}
/**
* Returns last validation message
*
* @return null|string
*/
public function getValidationMessage()
{
return $this->_validationMessage;
}
/**
* Returns if the were translations removed while cleaning
*
* @return bool
*/
public function wasFiltered()
{
return !empty($this->_filterMessages);
}
/**
* Returns the cleaning errors
*
* @return array
*/
public function getFilterMessages()
{
return $this->_filterMessages;
}
/**
* @param FilterAbstract $filter
*/
public function addFilter(FilterAbstract $filter)
{
$this->_filters[] = $filter;
}
/**
* @throws \Exception
*
* @return bool error state
*/
protected function _applyFilters()
{
// skip if already cleaned
if ($this->_currentState == self::__FILTERED__) {
return $this->wasFiltered();
}
$this->_filterMessages = array();
// skip if not translations available
if (!$this->hasTranslations()) {
$this->_currentState = self::__FILTERED__;
return false;
}
$cleanedTranslations = $this->_translations;
foreach ($this->_filters AS $filter) {
$cleanedTranslations = $filter->filter($cleanedTranslations);
$filteredData = $filter->getFilteredData();
if (!empty($filteredData)) {
$this->_filterMessages[] = get_class($filter) . " changed: " .var_export($filteredData, 1);
}
}
$this->_currentState = self::__FILTERED__;
if ($cleanedTranslations != $this->_translations) {
$this->_filterMessages[] = 'translations have been cleaned';
}
$this->_translations = $cleanedTranslations;
return $this->wasFiltered();
}
}
|