/
class.lang.php
568 lines (501 loc) · 18.2 KB
/
class.lang.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/**
* @package toolkit
*/
/**
* The Lang class loads and manages languages
*/
class Lang
{
/**
* Array of transliterations
* @var array
*/
private static $_transliterations;
/**
* Code of active language
* @var string
*/
private static $_lang;
/**
* Context information of all available languages
* @var array
*/
private static $_languages;
/**
* Array of localized strings
* @var array
*/
private static $_dictionary;
/**
* Array of localized date and time strings
* @var array
*/
private static $_datetime_dictionary;
/**
* Get the current dictionary
*
* @return array
* Return the dictionary
*/
public static function Dictionary()
{
return self::$_dictionary;
}
/**
* Get a list of either enabled or disabled languages. Example:
*
* array(
* [...]
*
* 'en' => array(
* 'name' => 'English',
* 'handle' => 'english',
* 'extensions' => array()
* ),
*
* 'it' => array(
* 'name' => 'Italiano',
* 'handle' => 'italian',
* 'extensions' => array(
* [...]
* )
* ),
*
* [...]
* )
*
* @see toolkit.Lang#createLanguage()
* @since Symphony 2.3
* @return array
* Return an array of languages (both enabled and disabled)
*/
public static function Languages()
{
return self::$_languages;
}
/**
* Get transliterations
*
* @return array
* Returns the transliterations array
*/
public static function Transliterations()
{
return self::$_transliterations;
}
/**
* Initialize transliterations, datetime dictionary and languages array.
*/
public static function initialize()
{
self::$_dictionary = array();
// Load default datetime strings
if (empty(self::$_datetime_dictionary)) {
include LANG . '/datetime.php';
self::$_datetime_dictionary = $datetime_strings;
}
// Load default transliterations
if (empty(self::$_transliterations)) {
include LANG . '/transliterations.php';
self::$_transliterations = $transliterations;
}
// Load default English language
if (empty(self::$_languages)) {
self::$_languages = self::createLanguage('en', 'English', 'english');
}
// Fetch all available languages
self::fetch();
}
/**
* Create an array of Language information for internal use.
*
* @since Symphony 2.3
* @param string $code
* Language code, e. g. 'en' or 'pt-br'
* @param string $name
* Language name
* @param string $handle (optional)
* Handle for the given language, used to build a valid 'lang_$handle' extension's handle.
* Defaults to null.
* @param array $extensions (optional)
* An array of extensions that support the given language.
* @return array
* An array of Language information.
*/
private static function createLanguage($code, $name, $handle = null, array $extensions = array())
{
return array(
$code => array(
'name' => $name,
'handle' => $handle,
'extensions' => $extensions
)
);
}
/**
* Fetch all languages available in the core language folder and the language extensions.
* The function stores all language information in the private variable `$_languages`.
* It contains an array with the name and handle of each language and an array of all
* extensions available in that language.
*
* @throws UnexpectedValueException
* @throws RuntimeException
*/
private static function fetch()
{
// Fetch extensions
$extensions = new DirectoryIterator(EXTENSIONS);
// Language extensions
foreach ($extensions as $extension) {
if ($extension->isDot() || $extension->isFile()) {
continue;
}
// Core translations
$core_handle = (strpos($extension->getFilename(), 'lang_') !== false)
? str_replace('lang_', '', $extension->getFilename())
: null;
// Loop over the `/lang` directory of this `$extension` searching for language
// files. If `/lang` isn't a directory, `UnexpectedValueException` will be
// thrown.
try {
$path = $extension->getPathname() . '/lang';
if (!is_dir($path)) {
continue;
}
$directory = new DirectoryIterator($path);
foreach ($directory as $file) {
if ($file->isDot() || !preg_match('/\.php$/', $file->getPathname())) {
continue;
}
include($file->getPathname());
// Get language code
$code = explode('.', $file);
$code = $code[1];
$lang = isset(self::$_languages[$code]) ? self::$_languages[$code] : null;
// Available extensions
$extensions = (isset($lang)) ? $lang['extensions'] : array();
// Core translations
if ($core_handle) {
$handle = $core_handle;
// Extension translations
} else {
$handle = (isset($lang)) ? $lang['handle'] : null;
$extensions = array_merge(array($extension->getFilename()), $extensions);
}
// Merge languages ($about is declared inside the included $file)
$temp = self::createLanguage($code, $about['name'], $handle, $extensions);
if (isset($lang)) {
foreach ($lang as $key => $value) {
// Prevent missing or nulled values overwriting existing values
// which can occur if a translation file is not correct.
if (!isset($temp[$code][$key]) || empty($temp[$code][$key])) {
continue;
}
self::$_languages[$code][$key] = $temp[$code][$key];
}
} else {
self::$_languages[$code] = $temp[$code];
}
}
} catch (Exception $ex) {
continue;
}
}
}
/**
* Set system language, load translations for core and extensions. If the specified language
* cannot be found, Symphony will default to English.
*
* Note: Beginning with Symphony 2.2 translations bundled with extensions will only be loaded
* when the core dictionary of the specific language is available.
*
* @param string $code
* Language code, e. g. 'en' or 'pt-br'
* @param boolean $checkStatus (optional)
* If false, set the language even if it's not enabled. Defaults to true.
*/
public static function set($code, $checkStatus = true)
{
if (!$code || $code == self::get()) {
return;
}
// Store current language code
self::$_lang = $code;
// Clear dictionary
self::$_dictionary = array();
// Language file available
if ($code !== 'en' && (self::isLanguageEnabled($code) || $checkStatus === false)) {
// Load core translations
self::load(vsprintf('%s/lang_%s/lang/lang.%s.php', array(
EXTENSIONS, self::$_languages[$code]['handle'], $code
)));
// Load extension translations
if (is_array(self::$_languages[$code]['extensions'])) {
foreach (self::$_languages[$code]['extensions'] as $extension) {
self::load(vsprintf('%s/%s/lang/lang.%s.php', array(
EXTENSIONS, $extension, $code
)));
}
}
// Language file unavailable, use default language
} else {
self::$_lang = 'en';
include LANG . '/transliterations.php';
self::$_transliterations = $transliterations;
// Log error, if possible
if ($code !== 'en' && class_exists('Symphony', false) && Symphony::Log() instanceof Log) {
Symphony::Log()->pushToLog(
__('The selected language, %s, could not be found. Using default English dictionary instead.', array($code)),
E_ERROR,
true
);
}
}
}
/**
* Given a valid language code, this function checks if the language is enabled.
*
* @since Symphony 2.3
* @param string $code
* Language code, e. g. 'en' or 'pt-br'
* @return boolean
* If true, the language is enabled.
*/
public static function isLanguageEnabled($code)
{
if ($code == 'en') {
return true;
}
$handle = (isset(self::$_languages[$code])) ? self::$_languages[$code]['handle'] : '';
$enabled_extensions = array();
// Fetch list of active extensions
if (class_exists('Symphony', false) && (!is_null(Symphony::ExtensionManager()))) {
$enabled_extensions = Symphony::ExtensionManager()->listInstalledHandles();
}
return in_array('lang_' . $handle, $enabled_extensions);
}
/**
* Load language file. Each language file contains three arrays:
* about, dictionary and transliterations.
*
* @param string $path
* Path of the language file that should be loaded
*/
private static function load($path)
{
// Load language file
if (file_exists($path)) {
require($path);
}
// Populate dictionary ($dictionary is declared inside $path)
if (isset($dictionary) && is_array($dictionary)) {
self::$_dictionary = array_merge(self::$_dictionary, $dictionary);
}
// Populate transliterations ($transliterations is declared inside $path)
if (isset($transliterations) && is_array($transliterations)) {
self::$_transliterations = array_merge(self::$_transliterations, $transliterations);
}
}
/**
* Get current language
*
* @return string
*/
public static function get()
{
return self::$_lang;
}
/**
* This function is an internal alias for `__()`.
*
* @since Symphony 2.3
* @see toolkit.__()
* @param string $string
* The string that should be translated
* @param array $inserts (optional)
* Optional array used to replace translation placeholders, defaults to NULL
* @param string $namespace (optional)
* Optional string used to define the namespace, defaults to NULL.
* @return string
* Returns the translated string
*/
public static function translate($string, array $inserts = null, $namespace = null)
{
if (is_null($namespace) && class_exists('Symphony', false)) {
$namespace = Symphony::getPageNamespace();
}
if (isset($namespace) && isset(self::$_dictionary[$namespace][$string])) {
$translated = self::$_dictionary[$namespace][$string];
} elseif (isset(self::$_dictionary[$string])) {
$translated = self::$_dictionary[$string];
} else {
$translated = $string;
}
$translated = empty($translated) ? $string : $translated;
// Replace translation placeholders
if (is_array($inserts) && !empty($inserts)) {
$translated = vsprintf($translated, $inserts);
}
return $translated;
}
/**
* Get an array of the codes and names of all languages that are available system wide.
*
* Note: Beginning with Symphony 2.2 language files are only available
* when the language extension is explicitly enabled.
*
* @param boolean $checkStatus (optional)
* If false, retrieves a list a languages that support core translation.
* @return array
* Returns an associative array of language codes and names, e. g. 'en' => 'English'
*/
public static function getAvailableLanguages($checkStatus = true)
{
$languages = array();
// Get available languages
foreach (self::$_languages as $key => $language) {
if (self::isLanguageEnabled($key) || ($checkStatus === false && isset($language['handle']))) {
$languages[$key] = $language['name'];
}
}
// Return languages codes
return $languages;
}
/**
* Check if Symphony is localised.
*
* @return boolean
* Returns true for localized system, false for English system
*/
public static function isLocalized()
{
return (self::get() !== 'en');
}
/**
* Localize dates.
*
* @param string $string
* Standard date that should be localized
* @return string
* Return the given date with translated month and day names
*/
public static function localizeDate($string)
{
// Only translate dates in localized environments
if (self::isLocalized()) {
foreach (self::$_datetime_dictionary as $value) {
$string = preg_replace('/\b' . $value . '\b/i', self::translate($value), $string);
}
}
return $string;
}
/**
* Standardize dates.
*
* @param string $string
* Localized date that should be standardized
* @return string
* Returns the given date with English month and day names
*/
public static function standardizeDate($string)
{
// Only standardize dates in localized environments
if (self::isLocalized()) {
// Translate names to English
foreach (self::$_datetime_dictionary as $values) {
$string = preg_replace('/\b' . self::translate($values) . '\b/i' . (self::isUnicodeCompiled() === true ? 'u' : null), $values, $string);
}
// Replace custom date and time separator with space:
// This is important, otherwise the `DateTime` constructor may break
// @todo Test if this separator is still required. It's a hidden setting
// and users are only aware of it if they go digging/pointed in the right direction
$separator = Symphony::Configuration()->get('datetime_separator', 'region');
if ($separator !== ' ') {
$string = str_replace($separator, ' ', $string);
}
}
return $string;
}
/**
* Given a string, this will clean it for use as a Symphony handle. Preserves multi-byte characters.
*
* @param string $string
* String to be cleaned up
* @param integer $max_length
* The maximum number of characters in the handle
* @param string $delim
* All non-valid characters will be replaced with this
* @param boolean $uriencode
* Force the resultant string to be uri encoded making it safe for URLs
* @param boolean $apply_transliteration
* If true, this will run the string through an array of substitution characters
* @param array $additional_rule_set
* An array of REGEX patterns that should be applied to the `$string`. This
* occurs after the string has been trimmed and joined with the `$delim`
* @return string
* Returns resultant handle
*/
public static function createHandle($string, $max_length = 255, $delim = '-', $uriencode = false, $apply_transliteration = true, $additional_rule_set = null)
{
// Use the transliteration table if provided
if ($apply_transliteration === true) {
$string = self::applyTransliterations($string);
}
return General::createHandle($string, $max_length, $delim, $uriencode, $additional_rule_set);
}
/**
* Given a string, this will clean it for use as a filename. Preserves multi-byte characters.
*
* @param string $string
* String to be cleaned up
* @param string $delim
* Replacement for invalid characters
* @param boolean $apply_transliteration
* If true, umlauts and special characters will be substituted
* @return string
* Returns created filename
*/
public static function createFilename($string, $delim='-', $apply_transliteration = true)
{
// Use the transliteration table if provided
if ($apply_transliteration === true) {
$file = pathinfo($string);
$string = self::applyTransliterations($file['filename']) . '.' . $file['extension'];
}
return General::createFilename($string, $delim);
}
/**
* This function replaces special characters according to the values stored inside
* `$_transliterations`.
*
* @since Symphony 2.3
* @param string $string
* The string that should be cleaned-up
* @return mixed
* Returns the transliterated string
*/
private static function applyTransliterations($string)
{
// Apply the straight transliterations with strtr as it's much faster
$string = strtr($string, self::$_transliterations['straight']);
// Apply the regex rules over the resulting $string
return preg_replace(
array_keys(self::$_transliterations['regexp']),
array_values(self::$_transliterations['regexp']),
$string
);
}
/**
* Returns boolean if PHP has been compiled with unicode support. This is
* useful to determine if unicode modifier's can be used in regular expression's
*
* @link http://stackoverflow.com/questions/4509576/detect-if-pcre-was-built-without-the-enable-unicode-properties-or-enable-utf8
* @since Symphony 2.2.2
* @return boolean
*/
public static function isUnicodeCompiled()
{
return (@preg_match('/\pL/u', 'a') == 1 ? true : false);
}
}