Skip to content

Commit 96ad102

Browse files
committed
Merge pull request #295 from Automattic/fix/216-rtl
Introduce a static RTL language list
2 parents a371da3 + 88c1ce8 commit 96ad102

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

class-languages.php

+42-13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,43 @@ class Babble_Languages extends Babble_Plugin {
6666
**/
6767
protected $errors;
6868

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+
69106
/**
70107
* Setup any add_action or add_filter calls. Initiate properties.
71108
*
@@ -443,7 +480,7 @@ protected function parse_available_languages() {
443480
'name' => $this->format_code_lang( $prefix ),
444481
'code' => $lang_code,
445482
'url_prefix' => $prefix,
446-
'text_direction' => $this->is_rtl( $lang_code ),
483+
'text_direction' => ( self::is_rtl( $lang_code ) ? 'rtl' : 'ltr' ),
447484
);
448485
// Cast to an object, in case we want to start using actual classes
449486
// at some point in the future.
@@ -462,21 +499,13 @@ protected function parse_available_languages() {
462499
}
463500

464501
/**
465-
* Parse (DON'T require or include) the [lang_code].php locale file in the languages
466-
* directory to work if the specified language is right to left. (We can't include or
467-
* require because it may contain function names which clash with other locale files.)
502+
* Is the given language a right-to-left language?
468503
*
469-
* @param string $lang The language code to retrieve RTL info for
504+
* @param string $lang_code The language code to retrieve RTL info for
470505
* @return bool True if the language is RTL
471506
**/
472-
protected function is_rtl( $lang ) {
473-
$locale_file = WP_LANG_DIR . "/$lang.php";
474-
if ( ( 0 === validate_file( $lang ) ) && is_readable( $locale_file ) ) {
475-
$locale_file_code = file_get_contents( $locale_file );
476-
// Regex to find something looking like: $text_direction = 'rtl';
477-
return ( (bool) preg_match( '/\$text_direction\s?=\s?[\'|"]rtl[\'|"]\s?;/i', $locale_file_code ) ) ? 'rtl' : 'ltr';
478-
}
479-
return 'ltr';
507+
public static function is_rtl( $lang_code ) {
508+
return in_array( $lang_code, self::$rtl_languages, true );
480509
}
481510

482511
/**

tests/test-languages.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class Test_Languages extends Babble_UnitTestCase {
4+
5+
public function setUp() {
6+
$this->install_languages();
7+
8+
parent::setUp();
9+
}
10+
11+
public function test_post_translations() {
12+
13+
$this->assertFalse( Babble_Languages::is_rtl( 'en_US' ) );
14+
$this->assertFalse( Babble_Languages::is_rtl( 'fr_FR' ) );
15+
$this->assertFalse( Babble_Languages::is_rtl( 'invalid' ) );
16+
$this->assertTrue( Babble_Languages::is_rtl( 'ar' ) );
17+
18+
}
19+
20+
}

0 commit comments

Comments
 (0)