Passed
Push — master ( 54b933...2e559a )
by Vítězslav
05:31
created

Locale::langToLocale()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 9
nop 1
dl 0
loc 15
ccs 0
cts 11
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Internationalization helpers
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2018 [email protected] (G)
7
 */
8
9
namespace Ease;
10
11
/**
12
 * Ease Locale support
13
 *
14
 * @author vitex
15
 */
16
class Locale
17
{
18
    /**
19
     * @var Locale Singleton is stored here
20
     */
21
    public static $_instance;
22
23
    /**
24
     * Current Used locale code
25
     * @var string 
26
     */
27
    public static $localeUsed = null;
28
29
    /**
30
     * i18n files location
31
     * @var string dirpath 
32
     */
33
    public static $i18n = null;
34
35
    /**
36
     * GetText Domain
37
     * @var string 
38
     */
39
    public static $textDomain = null;
40
41
    /**
42
     * All Language Codes => languages
43
     * @var array
44
     */
45
    public static $alllngs = [
46
        "af_NA" => "Afrikaans (Namibia)",
47
        "af_ZA" => "Afrikaans (South Africa)",
48
        "af" => "Afrikaans",
49
        "ak_GH" => "Akan (Ghana)",
50
        "ak" => "Akan",
51
        "sq_AL" => "Albanian (Albania)",
52
        "sq" => "Albanian",
53
        "am_ET" => "Amharic (Ethiopia)",
54
        "am" => "Amharic",
55
        "ar_DZ" => "Arabic (Algeria)",
56
        "ar_BH" => "Arabic (Bahrain)",
57
        "ar_EG" => "Arabic (Egypt)",
58
        "ar_IQ" => "Arabic (Iraq)",
59
        "ar_JO" => "Arabic (Jordan)",
60
        "ar_KW" => "Arabic (Kuwait)",
61
        "ar_LB" => "Arabic (Lebanon)",
62
        "ar_LY" => "Arabic (Libya)",
63
        "ar_MA" => "Arabic (Morocco)",
64
        "ar_OM" => "Arabic (Oman)",
65
        "ar_QA" => "Arabic (Qatar)",
66
        "ar_SA" => "Arabic (Saudi Arabia)",
67
        "ar_SD" => "Arabic (Sudan)",
68
        "ar_SY" => "Arabic (Syria)",
69
        "ar_TN" => "Arabic (Tunisia)",
70
        "ar_AE" => "Arabic (United Arab Emirates)",
71
        "ar_YE" => "Arabic (Yemen)",
72
        "ar" => "Arabic",
73
        "hy_AM" => "Armenian (Armenia)",
74
        "hy" => "Armenian",
75
        "as_IN" => "Assamese (India)",
76
        "as" => "Assamese",
77
        "asa_TZ" => "Asu (Tanzania)",
78
        "asa" => "Asu",
79
        "az_Cyrl" => "Azerbaijani (Cyrillic)",
80
        "az_Cyrl_AZ" => "Azerbaijani (Cyrillic, Azerbaijan)",
81
        "az_Latn" => "Azerbaijani (Latin)",
82
        "az_Latn_AZ" => "Azerbaijani (Latin, Azerbaijan)",
83
        "az" => "Azerbaijani",
84
        "bm_ML" => "Bambara (Mali)",
85
        "bm" => "Bambara",
86
        "eu_ES" => "Basque (Spain)",
87
        "eu" => "Basque",
88
        "be_BY" => "Belarusian (Belarus)",
89
        "be" => "Belarusian",
90
        "bem_ZM" => "Bemba (Zambia)",
91
        "bem" => "Bemba",
92
        "bez_TZ" => "Bena (Tanzania)",
93
        "bez" => "Bena",
94
        "bn_BD" => "Bengali (Bangladesh)",
95
        "bn_IN" => "Bengali (India)",
96
        "bn" => "Bengali",
97
        "bs_BA" => "Bosnian (Bosnia and Herzegovina)",
98
        "bs" => "Bosnian",
99
        "bg_BG" => "Bulgarian (Bulgaria)",
100
        "bg" => "Bulgarian",
101
        "my_MM" => "Burmese (Myanmar [Burma])",
102
        "my" => "Burmese",
103
        "ca_ES" => "Catalan (Spain)",
104
        "ca" => "Catalan",
105
        "tzm_Latn" => "Central Morocco Tamazight (Latin)",
106
        "tzm_Latn_MA" => "Central Morocco Tamazight (Latin, Morocco)",
107
        "tzm" => "Central Morocco Tamazight",
108
        "chr_US" => "Cherokee (United States)",
109
        "chr" => "Cherokee",
110
        "cgg_UG" => "Chiga (Uganda)",
111
        "cgg" => "Chiga",
112
        "zh_Hans" => "Chinese (Simplified Han)",
113
        "zh_Hans_CN" => "Chinese (Simplified Han, China)",
114
        "zh_Hans_HK" => "Chinese (Simplified Han, Hong Kong SAR China)",
115
        "zh_Hans_MO" => "Chinese (Simplified Han, Macau SAR China)",
116
        "zh_Hans_SG" => "Chinese (Simplified Han, Singapore)",
117
        "zh_Hant" => "Chinese (Traditional Han)",
118
        "zh_Hant_HK" => "Chinese (Traditional Han, Hong Kong SAR China)",
119
        "zh_Hant_MO" => "Chinese (Traditional Han, Macau SAR China)",
120
        "zh_Hant_TW" => "Chinese (Traditional Han, Taiwan)",
121
        "zh" => "Chinese",
122
        "kw_GB" => "Cornish (United Kingdom)",
123
        "kw" => "Cornish",
124
        "hr_HR" => "Croatian (Croatia)",
125
        "hr" => "Croatian",
126
        "cs_CZ" => "Czech (Czech Republic)",
127
        "cs" => "Czech",
128
        "da_DK" => "Danish (Denmark)",
129
        "da" => "Danish",
130
        "nl_BE" => "Dutch (Belgium)",
131
        "nl_NL" => "Dutch (Netherlands)",
132
        "nl" => "Dutch",
133
        "ebu_KE" => "Embu (Kenya)",
134
        "ebu" => "Embu",
135
        "en_AS" => "English (American Samoa)",
136
        "en_AU" => "English (Australia)",
137
        "en_BE" => "English (Belgium)",
138
        "en_BZ" => "English (Belize)",
139
        "en_BW" => "English (Botswana)",
140
        "en_CA" => "English (Canada)",
141
        "en_GU" => "English (Guam)",
142
        "en_HK" => "English (Hong Kong SAR China)",
143
        "en_IN" => "English (India)",
144
        "en_IE" => "English (Ireland)",
145
        "en_JM" => "English (Jamaica)",
146
        "en_MT" => "English (Malta)",
147
        "en_MH" => "English (Marshall Islands)",
148
        "en_MU" => "English (Mauritius)",
149
        "en_NA" => "English (Namibia)",
150
        "en_NZ" => "English (New Zealand)",
151
        "en_MP" => "English (Northern Mariana Islands)",
152
        "en_PK" => "English (Pakistan)",
153
        "en_PH" => "English (Philippines)",
154
        "en_SG" => "English (Singapore)",
155
        "en_ZA" => "English (South Africa)",
156
        "en_TT" => "English (Trinidad and Tobago)",
157
        "en_UM" => "English (U.S. Minor Outlying Islands)",
158
        "en_VI" => "English (U.S. Virgin Islands)",
159
        "en_GB" => "English (United Kingdom)",
160
        "en_US" => "English (United States)",
161
        "en_ZW" => "English (Zimbabwe)",
162
        "en" => "English",
163
        "eo" => "Esperanto",
164
        "et_EE" => "Estonian (Estonia)",
165
        "et" => "Estonian",
166
        "ee_GH" => "Ewe (Ghana)",
167
        "ee_TG" => "Ewe (Togo)",
168
        "ee" => "Ewe",
169
        "fo_FO" => "Faroese (Faroe Islands)",
170
        "fo" => "Faroese",
171
        "fil_PH" => "Filipino (Philippines)",
172
        "fil" => "Filipino",
173
        "fi_FI" => "Finnish (Finland)",
174
        "fi" => "Finnish",
175
        "fr_BE" => "French (Belgium)",
176
        "fr_BJ" => "French (Benin)",
177
        "fr_BF" => "French (Burkina Faso)",
178
        "fr_BI" => "French (Burundi)",
179
        "fr_CM" => "French (Cameroon)",
180
        "fr_CA" => "French (Canada)",
181
        "fr_CF" => "French (Central African Republic)",
182
        "fr_TD" => "French (Chad)",
183
        "fr_KM" => "French (Comoros)",
184
        "fr_CG" => "French (Congo - Brazzaville)",
185
        "fr_CD" => "French (Congo - Kinshasa)",
186
        "fr_CI" => "French (Côte d’Ivoire)",
187
        "fr_DJ" => "French (Djibouti)",
188
        "fr_GQ" => "French (Equatorial Guinea)",
189
        "fr_FR" => "French (France)",
190
        "fr_GA" => "French (Gabon)",
191
        "fr_GP" => "French (Guadeloupe)",
192
        "fr_GN" => "French (Guinea)",
193
        "fr_LU" => "French (Luxembourg)",
194
        "fr_MG" => "French (Madagascar)",
195
        "fr_ML" => "French (Mali)",
196
        "fr_MQ" => "French (Martinique)",
197
        "fr_MC" => "French (Monaco)",
198
        "fr_NE" => "French (Niger)",
199
        "fr_RW" => "French (Rwanda)",
200
        "fr_RE" => "French (Réunion)",
201
        "fr_BL" => "French (Saint Barthélemy)",
202
        "fr_MF" => "French (Saint Martin)",
203
        "fr_SN" => "French (Senegal)",
204
        "fr_CH" => "French (Switzerland)",
205
        "fr_TG" => "French (Togo)",
206
        "fr" => "French",
207
        "ff_SN" => "Fulah (Senegal)",
208
        "ff" => "Fulah",
209
        "gl_ES" => "Galician (Spain)",
210
        "gl" => "Galician",
211
        "lg_UG" => "Ganda (Uganda)",
212
        "lg" => "Ganda",
213
        "ka_GE" => "Georgian (Georgia)",
214
        "ka" => "Georgian",
215
        "de_AT" => "German (Austria)",
216
        "de_BE" => "German (Belgium)",
217
        "de_DE" => "German (Germany)",
218
        "de_LI" => "German (Liechtenstein)",
219
        "de_LU" => "German (Luxembourg)",
220
        "de_CH" => "German (Switzerland)",
221
        "de" => "German",
222
        "el_CY" => "Greek (Cyprus)",
223
        "el_GR" => "Greek (Greece)",
224
        "el" => "Greek",
225
        "gu_IN" => "Gujarati (India)",
226
        "gu" => "Gujarati",
227
        "guz_KE" => "Gusii (Kenya)",
228
        "guz" => "Gusii",
229
        "ha_Latn" => "Hausa (Latin)",
230
        "ha_Latn_GH" => "Hausa (Latin, Ghana)",
231
        "ha_Latn_NE" => "Hausa (Latin, Niger)",
232
        "ha_Latn_NG" => "Hausa (Latin, Nigeria)",
233
        "ha" => "Hausa",
234
        "haw_US" => "Hawaiian (United States)",
235
        "haw" => "Hawaiian",
236
        "he_IL" => "Hebrew (Israel)",
237
        "he" => "Hebrew",
238
        "hi_IN" => "Hindi (India)",
239
        "hi" => "Hindi",
240
        "hu_HU" => "Hungarian (Hungary)",
241
        "hu" => "Hungarian",
242
        "is_IS" => "Icelandic (Iceland)",
243
        "is" => "Icelandic",
244
        "ig_NG" => "Igbo (Nigeria)",
245
        "ig" => "Igbo",
246
        "id_ID" => "Indonesian (Indonesia)",
247
        "id" => "Indonesian",
248
        "ga_IE" => "Irish (Ireland)",
249
        "ga" => "Irish",
250
        "it_IT" => "Italian (Italy)",
251
        "it_CH" => "Italian (Switzerland)",
252
        "it" => "Italian",
253
        "ja_JP" => "Japanese (Japan)",
254
        "ja" => "Japanese",
255
        "kea_CV" => "Kabuverdianu (Cape Verde)",
256
        "kea" => "Kabuverdianu",
257
        "kab_DZ" => "Kabyle (Algeria)",
258
        "kab" => "Kabyle",
259
        "kl_GL" => "Kalaallisut (Greenland)",
260
        "kl" => "Kalaallisut",
261
        "kln_KE" => "Kalenjin (Kenya)",
262
        "kln" => "Kalenjin",
263
        "kam_KE" => "Kamba (Kenya)",
264
        "kam" => "Kamba",
265
        "kn_IN" => "Kannada (India)",
266
        "kn" => "Kannada",
267
        "kk_Cyrl" => "Kazakh (Cyrillic)",
268
        "kk_Cyrl_KZ" => "Kazakh (Cyrillic, Kazakhstan)",
269
        "kk" => "Kazakh",
270
        "km_KH" => "Khmer (Cambodia)",
271
        "km" => "Khmer",
272
        "ki_KE" => "Kikuyu (Kenya)",
273
        "ki" => "Kikuyu",
274
        "rw_RW" => "Kinyarwanda (Rwanda)",
275
        "rw" => "Kinyarwanda",
276
        "kok_IN" => "Konkani (India)",
277
        "kok" => "Konkani",
278
        "ko_KR" => "Korean (South Korea)",
279
        "ko" => "Korean",
280
        "khq_ML" => "Koyra Chiini (Mali)",
281
        "khq" => "Koyra Chiini",
282
        "ses_ML" => "Koyraboro Senni (Mali)",
283
        "ses" => "Koyraboro Senni",
284
        "lag_TZ" => "Langi (Tanzania)",
285
        "lag" => "Langi",
286
        "lv_LV" => "Latvian (Latvia)",
287
        "lv" => "Latvian",
288
        "lt_LT" => "Lithuanian (Lithuania)",
289
        "lt" => "Lithuanian",
290
        "luo_KE" => "Luo (Kenya)",
291
        "luo" => "Luo",
292
        "luy_KE" => "Luyia (Kenya)",
293
        "luy" => "Luyia",
294
        "mk_MK" => "Macedonian (Macedonia)",
295
        "mk" => "Macedonian",
296
        "jmc_TZ" => "Machame (Tanzania)",
297
        "jmc" => "Machame",
298
        "kde_TZ" => "Makonde (Tanzania)",
299
        "kde" => "Makonde",
300
        "mg_MG" => "Malagasy (Madagascar)",
301
        "mg" => "Malagasy",
302
        "ms_BN" => "Malay (Brunei)",
303
        "ms_MY" => "Malay (Malaysia)",
304
        "ms" => "Malay",
305
        "ml_IN" => "Malayalam (India)",
306
        "ml" => "Malayalam",
307
        "mt_MT" => "Maltese (Malta)",
308
        "mt" => "Maltese",
309
        "gv_GB" => "Manx (United Kingdom)",
310
        "gv" => "Manx",
311
        "mr_IN" => "Marathi (India)",
312
        "mr" => "Marathi",
313
        "mas_KE" => "Masai (Kenya)",
314
        "mas_TZ" => "Masai (Tanzania)",
315
        "mas" => "Masai",
316
        "mer_KE" => "Meru (Kenya)",
317
        "mer" => "Meru",
318
        "mfe_MU" => "Morisyen (Mauritius)",
319
        "mfe" => "Morisyen",
320
        "naq_NA" => "Nama (Namibia)",
321
        "naq" => "Nama",
322
        "ne_IN" => "Nepali (India)",
323
        "ne_NP" => "Nepali (Nepal)",
324
        "ne" => "Nepali",
325
        "nd_ZW" => "North Ndebele (Zimbabwe)",
326
        "nd" => "North Ndebele",
327
        "nb_NO" => "Norwegian Bokmål (Norway)",
328
        "nb" => "Norwegian Bokmål",
329
        "nn_NO" => "Norwegian Nynorsk (Norway)",
330
        "nn" => "Norwegian Nynorsk",
331
        "nyn_UG" => "Nyankole (Uganda)",
332
        "nyn" => "Nyankole",
333
        "or_IN" => "Oriya (India)",
334
        "or" => "Oriya",
335
        "om_ET" => "Oromo (Ethiopia)",
336
        "om_KE" => "Oromo (Kenya)",
337
        "om" => "Oromo",
338
        "ps_AF" => "Pashto (Afghanistan)",
339
        "ps" => "Pashto",
340
        "fa_AF" => "Persian (Afghanistan)",
341
        "fa_IR" => "Persian (Iran)",
342
        "fa" => "Persian",
343
        "pl_PL" => "Polish (Poland)",
344
        "pl" => "Polish",
345
        "pt_BR" => "Portuguese (Brazil)",
346
        "pt_GW" => "Portuguese (Guinea-Bissau)",
347
        "pt_MZ" => "Portuguese (Mozambique)",
348
        "pt_PT" => "Portuguese (Portugal)",
349
        "pt" => "Portuguese",
350
        "pa_Arab" => "Punjabi (Arabic)",
351
        "pa_Arab_PK" => "Punjabi (Arabic, Pakistan)",
352
        "pa_Guru" => "Punjabi (Gurmukhi)",
353
        "pa_Guru_IN" => "Punjabi (Gurmukhi, India)",
354
        "pa" => "Punjabi",
355
        "ro_MD" => "Romanian (Moldova)",
356
        "ro_RO" => "Romanian (Romania)",
357
        "ro" => "Romanian",
358
        "rm_CH" => "Romansh (Switzerland)",
359
        "rm" => "Romansh",
360
        "rof_TZ" => "Rombo (Tanzania)",
361
        "rof" => "Rombo",
362
        "ru_MD" => "Russian (Moldova)",
363
        "ru_RU" => "Russian (Russia)",
364
        "ru_UA" => "Russian (Ukraine)",
365
        "ru" => "Russian",
366
        "rwk_TZ" => "Rwa (Tanzania)",
367
        "rwk" => "Rwa",
368
        "saq_KE" => "Samburu (Kenya)",
369
        "saq" => "Samburu",
370
        "sg_CF" => "Sango (Central African Republic)",
371
        "sg" => "Sango",
372
        "seh_MZ" => "Sena (Mozambique)",
373
        "seh" => "Sena",
374
        "sr_Cyrl" => "Serbian (Cyrillic)",
375
        "sr_Cyrl_BA" => "Serbian (Cyrillic, Bosnia and Herzegovina)",
376
        "sr_Cyrl_ME" => "Serbian (Cyrillic, Montenegro)",
377
        "sr_Cyrl_RS" => "Serbian (Cyrillic, Serbia)",
378
        "sr_Latn" => "Serbian (Latin)",
379
        "sr_Latn_BA" => "Serbian (Latin, Bosnia and Herzegovina)",
380
        "sr_Latn_ME" => "Serbian (Latin, Montenegro)",
381
        "sr_Latn_RS" => "Serbian (Latin, Serbia)",
382
        "sr" => "Serbian",
383
        "sn_ZW" => "Shona (Zimbabwe)",
384
        "sn" => "Shona",
385
        "ii_CN" => "Sichuan Yi (China)",
386
        "ii" => "Sichuan Yi",
387
        "si_LK" => "Sinhala (Sri Lanka)",
388
        "si" => "Sinhala",
389
        "sk_SK" => "Slovak (Slovakia)",
390
        "sk" => "Slovak",
391
        "sl_SI" => "Slovenian (Slovenia)",
392
        "sl" => "Slovenian",
393
        "xog_UG" => "Soga (Uganda)",
394
        "xog" => "Soga",
395
        "so_DJ" => "Somali (Djibouti)",
396
        "so_ET" => "Somali (Ethiopia)",
397
        "so_KE" => "Somali (Kenya)",
398
        "so_SO" => "Somali (Somalia)",
399
        "so" => "Somali",
400
        "es_AR" => "Spanish (Argentina)",
401
        "es_BO" => "Spanish (Bolivia)",
402
        "es_CL" => "Spanish (Chile)",
403
        "es_CO" => "Spanish (Colombia)",
404
        "es_CR" => "Spanish (Costa Rica)",
405
        "es_DO" => "Spanish (Dominican Republic)",
406
        "es_EC" => "Spanish (Ecuador)",
407
        "es_SV" => "Spanish (El Salvador)",
408
        "es_GQ" => "Spanish (Equatorial Guinea)",
409
        "es_GT" => "Spanish (Guatemala)",
410
        "es_HN" => "Spanish (Honduras)",
411
        "es_419" => "Spanish (Latin America)",
412
        "es_MX" => "Spanish (Mexico)",
413
        "es_NI" => "Spanish (Nicaragua)",
414
        "es_PA" => "Spanish (Panama)",
415
        "es_PY" => "Spanish (Paraguay)",
416
        "es_PE" => "Spanish (Peru)",
417
        "es_PR" => "Spanish (Puerto Rico)",
418
        "es_ES" => "Spanish (Spain)",
419
        "es_US" => "Spanish (United States)",
420
        "es_UY" => "Spanish (Uruguay)",
421
        "es_VE" => "Spanish (Venezuela)",
422
        "es" => "Spanish",
423
        "sw_KE" => "Swahili (Kenya)",
424
        "sw_TZ" => "Swahili (Tanzania)",
425
        "sw" => "Swahili",
426
        "sv_FI" => "Swedish (Finland)",
427
        "sv_SE" => "Swedish (Sweden)",
428
        "sv" => "Swedish",
429
        "gsw_CH" => "Swiss German (Switzerland)",
430
        "gsw" => "Swiss German",
431
        "shi_Latn" => "Tachelhit (Latin)",
432
        "shi_Latn_MA" => "Tachelhit (Latin, Morocco)",
433
        "shi_Tfng" => "Tachelhit (Tifinagh)",
434
        "shi_Tfng_MA" => "Tachelhit (Tifinagh, Morocco)",
435
        "shi" => "Tachelhit",
436
        "dav_KE" => "Taita (Kenya)",
437
        "dav" => "Taita",
438
        "ta_IN" => "Tamil (India)",
439
        "ta_LK" => "Tamil (Sri Lanka)",
440
        "ta" => "Tamil",
441
        "te_IN" => "Telugu (India)",
442
        "te" => "Telugu",
443
        "teo_KE" => "Teso (Kenya)",
444
        "teo_UG" => "Teso (Uganda)",
445
        "teo" => "Teso",
446
        "th_TH" => "Thai (Thailand)",
447
        "th" => "Thai",
448
        "bo_CN" => "Tibetan (China)",
449
        "bo_IN" => "Tibetan (India)",
450
        "bo" => "Tibetan",
451
        "ti_ER" => "Tigrinya (Eritrea)",
452
        "ti_ET" => "Tigrinya (Ethiopia)",
453
        "ti" => "Tigrinya",
454
        "to_TO" => "Tonga (Tonga)",
455
        "to" => "Tonga",
456
        "tr_TR" => "Turkish (Turkey)",
457
        "tr" => "Turkish",
458
        "uk_UA" => "Ukrainian (Ukraine)",
459
        "uk" => "Ukrainian",
460
        "ur_IN" => "Urdu (India)",
461
        "ur_PK" => "Urdu (Pakistan)",
462
        "ur" => "Urdu",
463
        "uz_Arab" => "Uzbek (Arabic)",
464
        "uz_Arab_AF" => "Uzbek (Arabic, Afghanistan)",
465
        "uz_Cyrl" => "Uzbek (Cyrillic)",
466
        "uz_Cyrl_UZ" => "Uzbek (Cyrillic, Uzbekistan)",
467
        "uz_Latn" => "Uzbek (Latin)",
468
        "uz_Latn_UZ" => "Uzbek (Latin, Uzbekistan)",
469
        "uz" => "Uzbek",
470
        "vi_VN" => "Vietnamese (Vietnam)",
471
        "vi" => "Vietnamese",
472
        "vun_TZ" => "Vunjo (Tanzania)",
473
        "vun" => "Vunjo",
474
        "cy_GB" => "Welsh (United Kingdom)",
475
        "cy" => "Welsh",
476
        "yo_NG" => "Yoruba (Nigeria)",
477
        "yo" => "Yoruba",
478
        "zu_ZA" => "Zulu (South Africa)",
479
        "zu" => "Zulu"
480
    ];
481
482
    /**
483
     * Prepare use of localization
484
     * 
485
     * @param string $setLocale  en_US|cs_CZ|..
486
     * @param string $i18n       directory ( /usr/lib/locale/ in Debian )
487
     * @param string $textDomain we want use $i18n/$setLocale/LC_ALL/$textDomain.mo
488
     */
489
    public function __construct($setLocale = null, $i18n = '../i18n',
490
                                $textDomain = null)
491
    {
492
        if (is_null($setLocale)) {
493
            $setLocale = self::getPreferedLocale();
494
        }
495
        if (is_null($textDomain)) {
496
            if (is_null(self::$textDomain)) {
0 ignored issues
show
introduced by
The condition is_null(self::textDomain) is always false.
Loading history...
497
                if (defined('EASE_APPNAME')) {
498
                    $textDomain = strtolower(constant('EASE_APPNAME'));
499
                }
500
            } else {
501
                $textDomain = self::$textDomain;
502
            }
503
        }
504
        self::$i18n = $i18n;
505
        self::initializeGetText($textDomain, $setLocale, $i18n);
506
    }
507
508
    /**
509
     * Prefered Locale Code - 1) Requested 2) Session 3) Browser for WebPage or
510
     *                        getenv('LC_ALL') for CLI
511
     * 
512
     * @return string locale code 
513
     */
514
    public static function getPreferedLocale()
515
    {
516
//        $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); 
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
517
        if (php_sapi_name() == 'cli') {
518
            $locale = getenv('LC_ALL');
519
        } else {
520
            $reqLocale = self::requestLocale();
521
            if (is_null($reqLocale)) {
0 ignored issues
show
introduced by
The condition is_null($reqLocale) is always false.
Loading history...
522
                $sesLocale = self::sessionLocale();
523
                if (is_null($sesLocale)) {
524
                    $locale = self::browserLocale();
525
                } else {
526
                    $locale = $sesLocale;
527
                }
528
            } else {
529
                $locale = $reqLocale;
530
            }
531
        }
532
        return $locale;
533
    }
534
535
    /**
536
     * Session by page GET or POST request with 'locale' field
537
     * 
538
     * @return string Locale Code
539
     */
540
    public static function requestLocale()
541
    {
542
        return isset($_REQUEST) && array_key_exists('locale', $_REQUEST) ? $_REQUEST['locale']
543
                : null;
544
    }
545
546
    /**
547
     * Locale code saved to session field $_SESSION['locale']
548
     * 
549
     * @return string locale code
550
     */
551
    public static function sessionLocale()
552
    {
553
        return isset($_SESSION) && array_key_exists('locale', $_SESSION) ? $_SESSION['locale']
554
                : null;
555
    }
556
557
    /**
558
     * Locale code by browser default language
559
     * 
560
     * @return string locale code
561
     */
562
    public static function browserLocale()
563
    {
564
        return self::langToLocale(self::autodetected());
565
    }
566
567
    /**
568
     * List of availble locales
569
     * 
570
     * @return array locales availble
571
     */
572
    public function availble()
573
    {
574
        $locales = [];
575
        $d       = dir(self::$i18n);
576
        while (false !== ($entry   = $d->read())) {
577
            if (($entry[0] != '.') && file_exists(self::$i18n.'/'.$entry.'/LC_MESSAGES/'.self::$textDomain.'.mo')) {
578
                $locales[$entry] = _(self::$alllngs[$entry]);
579
            }
580
        }
581
        $d->close();
582
        return $locales;
583
    }
584
585
    /**
586
     * Store GetText Domain
587
     * @param string $textDomain
588
     */
589
    public static function setTextDomain($textDomain)
590
    {
591
        self::$textDomain = $textDomain;
592
    }
593
594
    /**
595
     * Initialise Gettext
596
     *
597
     * $i18n/$defaultLocale/LC_MESSAGES/$appname.mo
598
     *
599
     * @param string $appname        name for binddomain
600
     * @param string $defaultLocale  locale of source code localstring
601
     * @param string $i18n           directory base localisation directory
602
     *
603
     * @return
604
     */
605 1
    public static function initializeGetText($appname, $defaultLocale = 'en_US',
606
                                             $i18n = '../i18n')
0 ignored issues
show
Unused Code introduced by
The parameter $i18n is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

606
                                             /** @scrutinizer ignore-unused */ $i18n = '../i18n')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
607
    {
608 1
        self::setTextDomain($appname);
609 1
        return self::useLocale($defaultLocale);
610
    }
611
612
    /**
613
     * Find Locale Code for browser language
614
     * 
615
     * @param string $lang browser lan en|cs|..
616
     * 
617
     * @return string locale code
618
     */
619
    public static function langToLocale($lang)
620
    {
621
        $defaultLocale = 'C';
622
        $langs         = [];
623
        foreach (self::$alllngs as $langCode => $language) {
624
            $langs[$langCode] = [strstr($langCode, '_') ? substr($langCode, 0,
625
                    strpos($langCode, '_')) : $langCode, $language];
626
        }
627
        foreach ($langs as $code => $langInfo) {
628
            if ($lang == $langInfo[0]) {
629
                $defaultLocale = $code;
630
                break;
631
            }
632
        }
633
        return $defaultLocale;
634
    }
635
636
    /**
637
     * Use Effective locale to requested
638
     * 
639
     * @param string $localeCode locale code to use
640
     * 
641
     * @return string used locale code
642
     */
643
    public static function useLocale($localeCode)
644
    {
645
        \setlocale(LC_ALL, $localeCode);
646
        \bind_textdomain_codeset(self::$textDomain, 'UTF-8');
647
        \putenv("LC_ALL=$localeCode");
648
        if (file_exists(self::$i18n)) {
649
            \bindtextdomain(self::$textDomain, self::$i18n);
650
        }
651
        \textdomain(self::$textDomain);
652
        if (isset($_SESSION)) {
653
            $_SESSION['locale'] = $localeCode;
654
        }
655
        self::$localeUsed = $localeCode;
656
    }
657
658
    /**
659
     * Try to autodetect default language
660
     * 
661
     * @return string lang code 
662
     */
663
    public static function autodetected()
664
    {
665
        return array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) && function_exists('\locale_accept_from_http')
666
                ? \locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']) : null;
667
    }
668
669
    /**
670
     * obtain ISO 639-1 language code
671
     * 
672
     * @return string
673
     */
674
    public function get2Code()
675
    {
676
        $localeUsed = $this->getLocaleUsed();
677
        return strstr($localeUsed, '_') ? current(explode('_', $localeUsed)) : $localeUsed;
678
    }
679
680
    /**
681
     * get Current Used locale code
682
     * 
683
     * @return string
684
     */
685
    public function getLocaleUsed()
686
    {
687
        return isset(self::$_instance) ? self::$_instance->getLocaleUsed() : self::$localeUsed;
688
    }
689
690
    /**
691
     * Common instance of Locale class
692
     * 
693
     * @return \Ease\Locale
694
     */
695
    public static function singleton()
696
    {
697
        if (!isset(self::$_instance)) {
698
            $class           = __CLASS__;
699
            self::$_instance = new $class();
700
        }
701
        return self::$_instance;
702
    }
703
}
704