@@ -66,6 +66,43 @@ class Babble_Languages extends Babble_Plugin {
66
66
**/
67
67
protected $ errors ;
68
68
69
+ /**
70
+ * Array of language codes where the language writes right-to-left.
71
+ *
72
+ * Based primarily on GlotPress' list of locales and Wikipedia's article on RTL:
73
+ *
74
+ * @link https://glotpress.trac.wordpress.org/browser/trunk/locales/locales.php
75
+ * @link https://en.wikipedia.org/wiki/Right-to-left
76
+ *
77
+ * @var array
78
+ */
79
+ protected static $ rtl_languages = array (
80
+ 'ar ' , // Arabic / العربية
81
+ 'arc ' , // Aramaic
82
+ 'arq ' , // Algerian Arabic / الدارجة الجزايرية
83
+ 'azb ' , // South Azerbaijani / گؤنئی آذربایجان
84
+ 'az_TR ' , // Azerbaijani (Turkey) / Azərbaycan Türkcəsi
85
+ 'bcc ' , // Balochi Southern / بلوچی مکرانی
86
+ 'bqi ' , // Bakthiari / بختياري
87
+ 'ckb ' , // Sorani Kurdish / کوردی
88
+ 'dv ' , // Dhivehi
89
+ 'fa ' , // Persian / فارسی
90
+ 'fa_IR ' , // Persian / فارسی
91
+ 'fa_AF ' , // Persian (Afghanistan) / (افغانستان) فارسی
92
+ 'glk ' , // Gilaki / گیلکی
93
+ 'ha ' , // Hausa / هَوُسَ
94
+ 'haz ' , // Hazaragi / هزاره گی
95
+ 'he ' , // Hebrew / עִבְרִית
96
+ 'he_IL ' , // Hebrew / עִבְרִית
97
+ 'mzn ' , // Mazanderani / مازِرونی
98
+ 'pnb ' , // Western Punjabi / پنجابی
99
+ 'ps ' , // Pashto / پښتو
100
+ 'sd ' , // Sindhi / سنڌي
101
+ 'ug ' , // Uyghur / ئۇيغۇرچە
102
+ 'ur ' , // Urdu / اردو
103
+ 'yi ' , // Yiddish / ייִדיש'
104
+ );
105
+
69
106
/**
70
107
* Setup any add_action or add_filter calls. Initiate properties.
71
108
*
@@ -153,21 +190,37 @@ public function options() {
153
190
$ langs = $ this ->merge_lang_sets ( $ this ->available_langs , $ this ->lang_prefs );
154
191
// Merge in any POSTed field values
155
192
foreach ( $ langs as $ code => & $ lang ) {
156
- $ lang ->url_prefix = ( @ isset ( $ _POST [ 'url_prefix_ ' . $ code ] ) ) ? $ _POST [ "url_prefix_ $ code " ] : @ $ lang ->url_prefix ;
157
- if ( ! $ lang ->url_prefix )
158
- $ lang ->url_prefix = $ lang ->url_prefix ;
159
- $ lang ->text_direction = $ lang ->text_direction ;
193
+ if ( ! empty ( $ _POST [ "url_prefix_ $ code " ] ) ) {
194
+ $ lang ->url_prefix = $ _POST [ "url_prefix_ $ code " ];
195
+ } else if ( empty ( $ lang ->url_prefix ) ) {
196
+ $ parts = explode ( '_ ' , $ lang ->code );
197
+ $ lang ->url_prefix = $ parts [0 ];
198
+ }
199
+ if ( ! in_array ( $ lang ->text_direction , array ( 'ltr ' , 'rtl ' ) ) ) {
200
+ // @TODO is this needed?
201
+ $ lang ->text_direction = 'ltr ' ;
202
+ }
160
203
// This line must come after the text direction value is set
161
204
$ lang ->input_lang_class = ( 'rtl ' == $ lang ->text_direction ) ? 'lang-rtl ' : 'lang-ltr ' ;
162
- $ lang ->display_name = ( @ isset ( $ _POST [ "display_name_ $ code " ] ) ) ? $ _POST [ "display_name_ $ code " ] : @ $ lang ->display_name ;
163
- if ( ! $ lang ->display_name )
205
+
206
+ if ( ! empty ( $ _POST [ "display_name_ $ code " ] ) ) {
207
+ $ lang ->display_name = $ _POST [ "display_name_ $ code " ];
208
+ } else if ( empty ( $ lang ->display_name ) ) {
164
209
$ lang ->display_name = $ lang ->name ;
210
+ }
211
+
165
212
// Note any url_prefix errors
166
- $ lang ->url_prefix_error = ( @ $ this ->errors [ "url_prefix_ $ code " ] ) ? 'babble-error ' : '0 ' ;
213
+ if ( ! empty ( $ this ->errors [ "url_prefix_ $ code " ] ) ) {
214
+ $ lang ->url_prefix_error = 'babble-error ' ;
215
+ } else {
216
+ $ lang ->url_prefix_error = '0 ' ;
217
+ }
218
+
167
219
// Flag the active languages
168
220
$ lang ->active = false ;
169
- if ( in_array ( $ code , $ this ->active_langs ) )
221
+ if ( in_array ( $ code , $ this ->active_langs ) ) {
170
222
$ lang ->active = true ;
223
+ }
171
224
172
225
}
173
226
$ vars = array ();
@@ -204,12 +257,13 @@ public function get_active_langs() {
204
257
* Given a lang object or lang code, this checks whether the
205
258
* language is public or not.
206
259
*
207
- * @param string $lang_code A language code
260
+ * @param string|object $lang_code A language code or a language object
208
261
* @return boolean True if public
209
262
**/
210
263
public function is_public_lang ( $ lang_code ) {
211
- if ( ! is_string ( $ lang_code ) )
212
- throw new exception ( 'Please provide a lang_code for the is_public_lang method. ' );
264
+ if ( is_object ( $ lang_code ) and ! empty ( $ lang_code ->lang ) ) {
265
+ $ lang_code = $ lang_code ->lang ;
266
+ }
213
267
return in_array ( $ lang_code , $ this ->public_langs );
214
268
}
215
269
@@ -330,8 +384,19 @@ protected function maybe_process_languages() {
330
384
$ url_prefixes = array ();
331
385
foreach ( $ this ->available_langs as $ code => $ lang ) {
332
386
$ lang_pref = new stdClass ;
333
- $ lang_pref ->display_name = @ $ _POST [ 'display_name_ ' . $ code ];
334
- $ lang_pref ->url_prefix = @ $ _POST [ 'url_prefix_ ' . $ code ];
387
+
388
+ if ( ! empty ( $ _POST [ 'display_name_ ' . $ code ] ) ) {
389
+ $ lang_pref ->display_name = $ _POST [ 'display_name_ ' . $ code ];
390
+ } else {
391
+ $ lang_pref ->display_name = $ lang ->name ;
392
+ }
393
+
394
+ if ( ! empty ( $ _POST [ 'url_prefix_ ' . $ code ] ) ) {
395
+ $ lang_pref ->url_prefix = $ _POST [ 'url_prefix_ ' . $ code ];
396
+ } else {
397
+ $ lang_pref ->url_prefix = $ lang ->url_prefix ;
398
+ }
399
+
335
400
// Check we don't have more than one language using the same url prefix
336
401
if ( array_key_exists ( $ lang_pref ->url_prefix , $ url_prefixes ) ) {
337
402
$ lang_1 = $ this ->format_code_lang ( $ code );
@@ -351,8 +416,11 @@ protected function maybe_process_languages() {
351
416
if ( ! $ this ->errors ) {
352
417
$ langs = $ this ->merge_lang_sets ( $ this ->available_langs , $ this ->lang_prefs );
353
418
$ active_langs = array ();
354
- foreach ( (array ) @ $ _POST [ 'active_langs ' ] as $ code )
355
- $ active_langs [ $ langs [ $ code ]->url_prefix ] = $ code ;
419
+ if ( ! empty ( $ _POST [ 'active_langs ' ] ) && is_array ( $ _POST [ 'active_langs ' ] ) ) {
420
+ foreach ( $ _POST [ 'active_langs ' ] as $ code ) {
421
+ $ active_langs [ $ langs [ $ code ]->url_prefix ] = $ code ;
422
+ }
423
+ }
356
424
if ( count ( $ active_langs ) < 2 ) {
357
425
$ this ->set_admin_error ( __ ( 'You must set at least two languages as active. ' , 'babble ' ) );
358
426
} else {
@@ -365,8 +433,11 @@ protected function maybe_process_languages() {
365
433
$ this ->set_admin_error ( __ ( 'You must set at least your default language as public. ' , 'babble ' ) );
366
434
} else {
367
435
$ public_langs = (array ) $ _POST [ 'public_langs ' ];
368
- if ( ! in_array ( @ $ _POST [ 'default_lang ' ], $ public_langs ) )
436
+ if ( empty ( $ _POST [ 'default_lang ' ] ) ) {
437
+ $ this ->set_admin_error ( __ ( 'You must choose a default language. ' , 'babble ' ) );
438
+ } else if ( ! in_array ( $ _POST [ 'default_lang ' ], $ public_langs ) ) {
369
439
$ this ->set_admin_error ( __ ( 'You must set your default language as public. ' , 'babble ' ) );
440
+ }
370
441
}
371
442
}
372
443
// Finish up, redirecting if we're all OK
@@ -375,7 +446,7 @@ protected function maybe_process_languages() {
375
446
$ this ->update_option ( 'public_langs ' , $ public_langs );
376
447
377
448
// First the default language
378
- $ default_lang = @ $ _POST [ 'default_lang ' ];
449
+ $ default_lang = $ _POST [ 'default_lang ' ];
379
450
$ this ->update_option ( 'default_lang ' , $ default_lang );
380
451
// Now the prefs
381
452
$ this ->update_option ( 'lang_prefs ' , $ lang_prefs );
@@ -409,7 +480,7 @@ protected function parse_available_languages() {
409
480
'name ' => $ this ->format_code_lang ( $ prefix ),
410
481
'code ' => $ lang_code ,
411
482
'url_prefix ' => $ prefix ,
412
- 'text_direction ' => $ this -> is_rtl ( $ lang_code ),
483
+ 'text_direction ' => ( self :: is_rtl ( $ lang_code ) ? ' rtl ' : ' ltr ' ),
413
484
);
414
485
// Cast to an object, in case we want to start using actual classes
415
486
// at some point in the future.
@@ -428,21 +499,13 @@ protected function parse_available_languages() {
428
499
}
429
500
430
501
/**
431
- * Parse (DON'T require or include) the [lang_code].php locale file in the languages
432
- * directory to work if the specified language is right to left. (We can't include or
433
- * require because it may contain function names which clash with other locale files.)
502
+ * Is the given language a right-to-left language?
434
503
*
435
- * @param string $lang The language code to retrieve RTL info for
504
+ * @param string $lang_code The language code to retrieve RTL info for
436
505
* @return bool True if the language is RTL
437
506
**/
438
- protected function is_rtl ( $ lang ) {
439
- $ locale_file = WP_LANG_DIR . "/ $ lang.php " ;
440
- if ( ( 0 === validate_file ( $ lang ) ) && is_readable ( $ locale_file ) ) {
441
- $ locale_file_code = file_get_contents ( $ locale_file );
442
- // Regex to find something looking like: $text_direction = 'rtl';
443
- return ( (bool ) preg_match ( '/\$text_direction\s?=\s?[ \'|"]rtl[ \'|"]\s?;/i ' , $ locale_file_code ) ) ? 'rtl ' : 'ltr ' ;
444
- }
445
- return 'ltr ' ;
507
+ public static function is_rtl ( $ lang_code ) {
508
+ return in_array ( $ lang_code , self ::$ rtl_languages , true );
446
509
}
447
510
448
511
/**
0 commit comments