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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Common;
use Piwik\Plugins\LanguagesManager\API;
use Piwik\TranslationWriter;
require_once 'LanguagesManager/API.php';
class Test_LanguagesManager extends PHPUnit_Framework_TestCase
{
public function setUp()
{
parent::setUp();
TranslationWriter::$disableJsonOptions = false;
include PIWIK_INCLUDE_PATH . '/core/DataFiles/Languages.php';
}
static $englishStringsIndexed = array();
static $englishStringsWithParameters = array();
static $allLanguages = array();
static $allCountries = array();
static $expectedLanguageKeys = array();
function getTestDataForLanguageFiles()
{
self::$allLanguages = Common::getLanguagesList();
self::$allCountries = Common::getCountriesList();
self::$englishStringsWithParameters = array();
self::$englishStringsIndexed = array();
self::$expectedLanguageKeys = array();
$englishStrings = API::getInstance()->getTranslationsForLanguage('en');
foreach ($englishStrings as $englishString) {
$stringLabel = $englishString['label'];
$stringValue = $englishString['value'];
$count = $this->getCountParametersToReplace($stringValue);
if (array_sum($count) > 0) {
self::$englishStringsWithParameters[$stringLabel] = $count;
}
self::$englishStringsIndexed[$stringLabel] = $stringValue;
self::$expectedLanguageKeys[] = $stringLabel;
}
// we also test that none of the language php files outputs any character on the screen (eg. space before the <?php)
$languages = API::getInstance()->getAvailableLanguages();
$return = array();
foreach ($languages AS $language) {
if ($language != 'en') {
$return[] = array($language);
}
}
return $return;
}
/**
* test all languages
*
* @group Plugins
* @group LanguagesManager
* @dataProvider getTestDataForLanguageFiles
*/
function testGetTranslationsForLanguages($language)
{
/** Indicates wether the translation files needs to be changed */
$writeCleanedFile = false;
$errorsInCurrentFile = array();
$strings = API::getInstance()->getTranslationsForLanguage($language);
// check if any translation contains restricted script tags
$serializedStrings = serialize($strings);
$invalids = array("<script", 'document.', 'javascript:', 'src=', 'BACKGROUND=', 'onload=');
foreach ($invalids as $invalid) {
$this->assertTrue(stripos($serializedStrings, $invalid) === false, "$language: language file containing javascript");
}
// check for at least 250 translation in file
$this->assertTrue(count($strings) > 250, "$language: expecting at least 250 translations in the language file");
$cleanedStrings = array();
foreach ($strings as $string) {
$stringLabel = $string['label'];
$stringValue = $string['value'];
// translations that are empty or don't exist in english translations should be removed
if (empty($stringValue) || trim($stringValue) === '' || !in_array($stringLabel, self::$expectedLanguageKeys)) {
$writeCleanedFile = true;
} else {
// checking that translated strings have the same number of %s as the english source strings
if (isset(self::$englishStringsWithParameters[$stringLabel])) {
$englishParametersCount = self::$englishStringsWithParameters[$stringLabel];
$countTranslation = $this->getCountParametersToReplace($stringValue);
if ($englishParametersCount != $countTranslation) {
$writeCleanedFile = true;
$errorsInCurrentFile[] = "$language: The string $stringLabel has ".json_encode($englishParametersCount)." parameters in English, but ".json_encode($countTranslation)." in this translation.";
} else {
$cleanedStrings[$stringLabel] = $stringValue;
}
} // No %s found
else {
$cleanedStrings[$stringLabel] = $stringValue;
}
}
// If the translation is the same as in English, we remove it from the translation file (as it might have been copied by
// the translator but this would skew translation stats
if (isset(self::$englishStringsIndexed[$stringLabel])
&& self::$englishStringsIndexed[$stringLabel] == $stringValue
// Do not however remove the General_ since there are definiely legit translations that are same as in english (eg. short days)
&& strpos($stringLabel, 'General_') === false
&& strpos($stringLabel, 'CoreHome_') === false
&& strpos($stringLabel, 'UserCountry_') === false
&& strpos($stringLabel, 'UserLanguage_') === false
&& $language != 'de'
) {
$writeCleanedFile = true;
$errorsInCurrentFile[] = "$language: The string $stringLabel is the same as in English, removing...";
unset($cleanedStrings[$stringLabel]);
}
// remove excessive line breaks (and leading/trailing whitespace) from translations
if (!empty($cleanedStrings[$stringLabel])) {
$stringNoLineBreak = trim($cleanedStrings[$stringLabel]);
$stringNoLineBreak = str_replace("\r", "", $stringNoLineBreak); # remove useless carrige renturns
$stringNoLineBreak = preg_replace('/([\n]{2,})/', "\n\n", $stringNoLineBreak); # remove excessive line breaks
if (!isset(self::$englishStringsIndexed[$stringLabel]) || !substr_count(self::$englishStringsIndexed[$stringLabel], "\n")) {
$stringNoLineBreak = str_replace("\n", " ", $stringNoLineBreak); # remove all line breaks if english string doesn't contain any
}
if ($cleanedStrings[$stringLabel] !== $stringNoLineBreak) {
$errorsInCurrentFile[] = "$language: found unnecessary whitespace in some strings in $stringLabel";
$writeCleanedFile = true;
$cleanedStrings[$stringLabel] = $stringNoLineBreak;
}
}
// Test locale
if ($stringLabel == 'General_Locale'
&& !empty($cleanedStrings[$stringLabel])
) {
if (!preg_match('/^([a-z]{2})_([A-Z]{2})\.UTF-8$/', $cleanedStrings[$stringLabel], $matches)) {
$this->fail("$language: invalid locale in $stringLabel");
} else if (!array_key_exists($matches[1], self::$allLanguages)) {
$this->fail("$language: invalid language code in $stringLabel");
} else if (!array_key_exists(strtolower($matches[2]), self::$allCountries)) {
$this->fail("$language: invalid region (country code) in $stringLabel");
}
}
if (isset($cleanedStrings[$stringLabel])) {
$currentString = $cleanedStrings[$stringLabel];
$decoded = TranslationWriter::clean($currentString);
if ($currentString != $decoded) {
$errorsInCurrentFile[] = "$language: found encoded entities in $stringLabel, converting entities to characters";
$writeCleanedFile = true;
$cleanedStrings[$stringLabel] = $decoded;
}
}
}
$this->assertTrue(!empty($cleanedStrings['General_TranslatorName']), "$language: translator info not specified");
$this->assertTrue(!empty($cleanedStrings['General_TranslatorEmail']), "$language: translator email not specified");
if (!empty($cleanedStrings['General_LayoutDirection'])
&& !in_array($cleanedStrings['General_LayoutDirection'], array('rtl', 'ltr'))
) {
$writeCleanedFile = true;
$errorsInCurrentFile[] = "$language: General_LayoutDirection must be rtl or ltr";
}
if ($writeCleanedFile) {
$path = TranslationWriter::getTranslationPath($language, 'tmp');
// Reorder cleaned up translations as the same order as en.php
uksort($cleanedStrings, array($this, 'sortTranslationsKeys'));
$nested = array();
foreach ($cleanedStrings as $key => $value) {
list($plugin, $nkey) = explode("_", $key, 2);
$nested[$plugin][$nkey] = $value;
}
$cleanedStrings = $nested;
TranslationWriter::saveTranslation($cleanedStrings, $path);
$output[] = (implode("\n", $errorsInCurrentFile) . "\n" . 'Translation file errors detected in ' . $language . '...
Wrote cleaned translation file in: ' . $path . ".
You can copy the cleaned files to /lang/\n");
}
if (!empty($output)) {
$this->fail(implode(",", $output));
}
}
/**
* Keep strings in the order of the english file
*
* @param $k1
* @param $k2
*
* @return int
*/
protected function sortTranslationsKeys($k1, $k2)
{
static $order = array();
if (empty($order)) {
$i = 0;
foreach (self::$englishStringsIndexed as $key => $value) {
$order[$key] = $i;
$i++;
}
}
if (empty($order[$k1])) {
$order[$k1] = 5000;
}
if (empty($order[$k2])) {
$order[$k2] = 5000;
}
return $order[$k1] < $order[$k2] ? -1 : ($order[$k1] == $order[$k2] ? strcmp($k1, $k2) : 1);
}
private function getCountParametersToReplace($string)
{
$sprintfParameters = array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s', '%6$s', '%7$s', '%8$s', '%9$s');
$count = array();
foreach ($sprintfParameters as $parameter) {
$placeholderCount = substr_count($string, $parameter);
if ($placeholderCount > 0) {
$count[$parameter] = $placeholderCount;
}
}
return $count;
}
/**
* test language when it's not defined
*
* @group Plugins
* @group LanguagesManager
*/
function testGetTranslationsForLanguagesNot()
{
$this->assertFalse(API::getInstance()->getTranslationsForLanguage("../no-language"));
}
/**
* test English short name for language
*
* @group Plugins
* @group LanguagesManager
*/
function testGetLanguageNamesInEnglish()
{
$languages = API::getInstance()->getAvailableLanguages();
foreach ($languages as $language) {
$data = file_get_contents(PIWIK_INCLUDE_PATH . "/lang/$language.json");
$translations = json_decode($data, true);
$name = $translations['General']['EnglishLanguageName'];
if ($language != 'en') {
$this->assertFalse($name == 'English', "for $language");
}
$languageCode = substr($language, 0, 2);
$this->assertTrue(isset($GLOBALS['Piwik_LanguageList'][$languageCode]));
$names = $GLOBALS['Piwik_LanguageList'][$languageCode];
if (isset($GLOBALS['Piwik_LanguageList'][$language])) {
if (is_array($names)) {
$this->assertTrue(in_array($name, $names), "$language: failed because $name not a known language name");
} else {
$this->assertTrue($name == $names, "$language: failed because $name == $names");
}
} else {
if (is_array($names)) {
$this->assertTrue(strpos($name, $names[0]) !== false);
} else {
$this->fail("$language: expected an array of language names");
}
}
}
}
/**
* test format of DataFile/Languages.php
*
* @group Plugins
* @group LanguagesManager
*/
function testGetLanguagesList()
{
$languages = Common::getLanguagesList();
$this->assertTrue(count($languages) > 0);
foreach ($languages as $langCode => $langs) {
$this->assertTrue(strlen($langCode) == 2, "$langCode length = 2");
$this->assertTrue(is_array($langs) && count($langs) >= 1, "$langCode array(names) >= 1");
}
}
}
|