-
-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathL10n.php
529 lines (455 loc) · 17 KB
/
L10n.php
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/**
* _backend/Lib/L10n.php
* All of the main translation logic
*/
namespace App\Lib;
class L10n
{
/**
* directory
* The directory that holds all translation files
*
* @var string
*/
public static $directory = __DIR__ . '/../../_lang';
/**
* blacklistedPages
* A list of basic regex functions used for black listing translatble pages
*
* @var array
*/
public static $blacklistedPages = array(
'/CODE_OF_CONDUCT.md/',
'/getting-started.md/',
'/human-interface-guidelines.md/',
'/inventory.php/',
'/LICENSE.md/',
'/os-dev.md/',
'/README.md/',
'/reference.md/',
'/router.php/',
'/SECURITY.md/',
'/TRANSLATE.md/',
);
/**
* language_folders
* Returns a list of all langauges the website currently has.
* NOTE: this does not return a list of enabled languages.
*
* @return array A list of languages we currently have
*/
public static function languageFolders()
{
$languages = array();
foreach (glob(static::$directory . '/*/', GLOB_ONLYDIR) as $path) {
$languages[] = basename($path);
}
return $languages;
}
/**
* pages
* Returns a list of pages that we can translate.
*
* @return array A list of pages we can translate
*/
public static function pages()
{
$rootDirectory = static::$directory . '/..';
$files = array_merge(
glob($rootDirectory . '/*.{php,md}', GLOB_BRACE),
glob($rootDirectory . '/docs/*.md'),
glob($rootDirectory . '/docs/code/*.md'),
glob($rootDirectory . '/store/*.php'),
glob($rootDirectory . '/_templates/*.php')
);
$pages = array();
foreach ($files as $file) {
$blacklisted = false;
foreach (static::$blacklistedPages as $reg) {
if (preg_match($reg, $file)) {
$blacklisted = true;
}
}
if ($blacklisted === false) {
$pages[] = $file;
}
}
return $pages;
}
/**
* languageDirectory
* Returns the directory for a language
*
* @param string $lang The language code
* @return string Directory the language files are in
*/
public static function languageDirectory($lang)
{
return realpath(static::$directory . '/' . $lang);
}
protected $available_langs = array(
'en' => 'English',
'ar' => 'العَرَبِيَّة',
'ca' => 'català',
'cs' => 'čeština',
'da' => 'Dansk',
'de' => 'Deutsch',
'es' => 'Español',
'fi' => 'Finnish',
'fr' => 'Français',
'gl' => 'Galego',
'he' => 'עִברִית',
'id' => 'Bahasa Indonesia',
'it' => 'Italiano',
'ja' => '日本語',
'ko' => '한국어',
'lt' => 'Lietuvių kalba',
'ms' => 'bahasa Melayu',
'mr' => 'मराठी',
'nb' => 'Bokmål',
'nl' => 'Nederlands',
'nn' => 'Nynorsk',
'pa' => 'ਪੰਜਾਬੀ',
'pl' => 'Polski',
'pt_BR' => 'Português (Brasil)',
'pt' => 'Português (Portugal)',
'ru' => 'Русский',
'sk' => 'Slovak',
'sv' => 'Swedish',
'tr' => 'Türkçe',
'uk' => 'українська',
'vi' => 'Tiếng Việt',
'zh_CN' => '简体中文',
);
protected $lang = 'en';
protected $domain = null;
public function __construct($lang = null)
{
if (empty($lang)) {
$lang = $this->getPageLang();
}
$this->lang = $lang;
}
public function init()
{
global $sitewide, $page; // Global site variables
if (defined('HTML_I18N')) {
return;
}
// Redirect the user if we are translating the page
if ((isset($_GET['lang']) || isset($_COOKIE['language']))
&& (isset($_GET['lang']) ? $_GET['lang'] : 'en') != $this->lang
&& $this->lang != 'en') {
$url = $sitewide['root'];
$url .= $this->lang.$page['path'];
$url = '/'.ltrim($url, '/'); // Make sure there is a / at the begining
header('Location: '.$url);
exit();
}
// Set cookie if the user has chosen the current language
if (isset($_GET['lang'])) {
setcookie('language', $this->lang, time() + 60*60*24*30, '/'); // 30 days
}
}
public function listLangs()
{
return $this->available_langs;
}
// DEPRECATED: use the static function instead
public function langDir($lang)
{
return static::languageDirectory($lang);
}
public function isLang($lang)
{
if (!is_string($lang)) {
return false;
}
if (!preg_match('#^[a-z]{2}(_([A-Z]{2}|[A-Z][a-z]+))?$#', $lang)) {
return false;
}
if ($lang == 'en') {
return true;
}
return array_key_exists($lang, $this->available_langs);
}
public function userLang()
{
if (isset($_COOKIE['language']) && $this->isLang($_COOKIE['language'])) {
return $_COOKIE['language'];
}
if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return null;
}
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($languages as $locale_str) {
$split = array_map('trim', explode(';', $locale_str, 2));
$lang_tag = $split[0];
$lang_tag = str_replace('-*', '', $lang_tag);
if (function_exists('locale_parse')) {
$locale = locale_parse($lang_tag);
} else {
$locale = array('language' => substr($lang_tag, 0, 2));
}
if (!empty($locale['region'])) {
$lang = $locale['language'].'_'.$locale['region'];
if ($this->isLang($lang)) {
return $lang;
}
}
if (!empty($locale['language']) && $this->isLang($locale['language'])) {
return $locale['language'];
}
}
return null;
}
public function loadTranslations($index)
{
$lang = $this->lang;
if (!$this->isLang($lang)) {
return false;
}
$langFile = $this->langDir($lang).'/'.$index.'.json';
if (!file_exists($langFile)) {
return false;
}
$json = file_get_contents($langFile);
return json_decode($json, true);
}
protected function loadDomain($domain)
{
$this->translations[$domain] = $this->loadTranslations($domain);
}
public function setDomain($domain)
{
if (ob_get_level()) {
ob_flush(); // Flush output buffer
}
$this->domain = $domain;
if (!isset($this->translations[$domain])) {
$this->loadDomain($domain);
}
}
/**
* Translate a string. Returns the original string if no translation was found.
*/
public function translate($id, $domain = null, $string = null)
{
if (empty($domain)) {
$domain = $this->domain;
}
if (empty($string)) {
$string = $id;
}
if (!isset($this->translations[$domain])) {
$this->loadDomain($domain);
}
if (isset($this->translations[$domain][$id]) &&
is_string($this->translations[$domain][$id]) &&
($this->translations[$domain][$id] !== "")) {
return $this->translations[$domain][$id];
} else {
return $string;
}
}
/**
* Translate a HTML string. This includes a minimalist XML parser.
* Can be provided a $translate callback to override default behaviour.
* (Useful to extract strings from a file for instance.)
*/
public function translateHtml($input, $translate = null)
{
if (empty($translate)) {
$translate = array($this, 'translate');
}
$output = ''; // Output HTML string
// Tags that doesn't contain translatable text
$tagsBlacklist = array('script', 'style', 'kbd', 'code');
// Attributes that can be translated
$attrsWhitelist = array(
'input' => array('placeholder', 'value', 'title'),
'a' => array('title'),
'img' => array('alt')
);
// Tags included in translation strings when used in <p>, <hX> or <li>
$ignoredTags = array('a', 'kbd', 'strong', 'em', 'code', 'sup', 'sub');
// Begin parsing input HTML
$i = 0;
$tagName = '';
$l10nId = '';
$l10nDisabled = false;
while ($i < strlen($input)) {
$char = $input[$i]; // Current char
if ($char == '<') { // Tag node (<HERE>)
$next = strpos($input, '>', $i);
$tag = substr($input, $i + 1, $next - $i - 1);
// Parse <tag>
$attrsStart = strpos($tag, ' ');
if ($attrsStart !== false) { // There are attributes specified
$tagName = substr($tag, 0, $attrsStart);
$attrs = substr($tag, $attrsStart + 1);
// Parse attributes only if it's interesting
// (translatable attributes or data-l10n-* attributes)
if (isset($attrsWhitelist[$tagName]) || strpos($attrs, 'data-l10n-') !== false) {
// Attributes that can be translated in this tag
if (isset($attrsWhitelist[$tagName])) {
$allowedAttrs = $attrsWhitelist[$tagName];
} else {
$allowedAttrs = array();
}
$tag = substr($tag, 0, $attrsStart + 1);
// Parse attributes
$j = 0;
while ($j < strlen($attrs)) {
$char = $attrs[$j];
if ($j == 0 || $char == ' ') {
if ($char == ' ') {
$j++;
}
// Extract attribute name and value
$nameEnd = strpos($attrs, '=', $j);
if ($nameEnd === false) {
// In case the last attribute is a boolean one (without value, e.g. <input disabled>)
$boolAttrName = substr($attrs, $j);
if (preg_match('#^[a-zA-Z0-9-]+$#', $boolAttrName)) {
$valueEnd = $j + strlen($boolAttrName);
$name = $boolAttrName;
$value = true;
} else {
break;
}
} else {
$valueEnd = strpos($attrs, '"', $nameEnd + 2);
if ($valueEnd === false) {
break;
}
$name = substr($attrs, $j, $nameEnd - $j);
$value = substr($attrs, $nameEnd + 2, $valueEnd - ($nameEnd + 2));
}
if ($name == 'data-l10n-id') { // Set translation ID for this tag
$l10nId = $value;
}
if ($name == 'data-l10n-off') { // Disable translation for this tag
$l10nDisabled = true;
}
if ($name == 'type' && $value == 'hidden') {
$l10nDisabled = true;
}
if (in_array($name, $allowedAttrs) && !$l10nDisabled) { // Translate attribute
$tag .= ' '.$name.'="'.$translate($value, $this->domain, $value).'"';
} else {
$tag .= ' '.substr($attrs, $j, $valueEnd - $j + 1);
}
$j = $valueEnd + 1;
} else {
break;
}
}
if ($j < strlen($attrs)) { // Broke inside the loop, append the remaining chars
$tag .= substr($attrs, $j);
}
}
} elseif (rtrim($tag, '/ ') != 'br') {
// No attributes in this tag
// Set current tag, if not a line break
$tagName = $tag;
}
$output .= '<'.$tag.'>';
} else { // Text node (<tag>HERE</tag>)
$next = strpos($input, '<', $i);
if ($next === false) { // End Of File
$next = strlen($input);
} elseif ($tagName == 'p' || $tagName == 'li' || preg_match('#^h[1-6]$#', $tagName)) {
// Do not process ignored tags in <p>, <hX> and <li>
$originalNext = $next;
$ignoredCount = 0;
do {
$found = false;
foreach ($ignoredTags as $ignoredTag) {
if (substr($input, $next + 1, strlen($ignoredTag)) == $ignoredTag) {
$nextChar = $input[$next + strlen($ignoredTag) + 1];
if ($nextChar == '>' || $nextChar == ' ') {
$tagEnd = strpos($input, '</'.$ignoredTag.'>', $next);
$next = strpos($input, '<', $tagEnd + 1);
$ignoredCount++;
$found = true;
break;
}
}
}
} while ($found);
// Just one link in the <p> ? Don't ignore it.
if ($ignoredCount == 1 && substr($input, $originalNext, 3) == '<a ' && substr($input, $next - 4, 4) == '</a>') {
$next = $originalNext;
}
} elseif (in_array($tagName, $tagsBlacklist)) {
// Avoid some bugs when < and > are present in script/style tags
$closeTag = '</'.$tagName.'>';
while (substr($input, $next, strlen($closeTag)) != $closeTag) {
$next = strpos($input, '<', $next + 1);
}
}
// Extract text to translate
$text = substr($input, $i + 1, $next - $i - 1);
if ((!in_array($tagName, $tagsBlacklist) || !empty($l10nId)) && !$l10nDisabled) {
$cleanedText = trim($text);
if (!empty($cleanedText) || !empty($l10nId)) {
if (empty($l10nId)) { // data-l10n-id attribute not set, use text as ID
$l10nId = $cleanedText;
}
// Properly re-inject whitespaces
$text = substr($text, 0, strpos($text, $cleanedText[0])) .
$translate($l10nId, $this->domain, $cleanedText) .
substr($text, strrpos($text, substr($cleanedText, -1)) + 1);
}
}
$output .= $text; // Append text to output
// Reset per-tag vars
$l10nId = '';
$l10nDisabled = false;
}
$i = $next; // Jump to next interesting char
}
return $output;
}
/**
* Begin to translate outputted HTML.
*/
public function beginHtmlTranslation()
{
if (defined('HTML_I18N')) { // Do not allow nested output buffering
return;
}
define('HTML_I18N', 1);
ob_start(function ($input) {
return $this->translateHtml($input);
});
}
/**
* End outputted HTML translation.
*/
public function endHtmlTranslation()
{
if (!ob_get_level()) {
return;
}
ob_end_flush();
}
public function getPageLang()
{
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
} else {
$lang = $this->userLang();
}
if (!$this->isLang($lang)) {
$lang = 'en';
}
return $lang;
}
public function lang()
{
return $this->lang;
}
}