Locale   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 706
Duplicated Lines 0 %

Test Coverage

Coverage 88.75%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 512
c 2
b 1
f 0
dl 0
loc 706
ccs 71
cts 80
cp 0.8875
rs 9.0399
wmc 42

14 Methods

Rating   Name   Duplication   Size   Complexity  
A availble() 0 16 5
A browserLocale() 0 3 1
A setTextDomain() 0 3 1
A autodetected() 0 4 3
A useLocale() 0 15 4
A sessionLocale() 0 4 3
A getLocaleUsed() 0 3 2
A __construct() 0 16 5
A getPreferedLocale() 0 19 5
A langToLocale() 0 17 5
A singleton() 0 7 2
A initializeGetText() 0 6 1
A requestLocale() 0 4 3
A get2Code() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like Locale often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Locale, and based on these observations, apply Extract Interface, too.

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