How to find a language direction?

The function is_rtl() checks the blog language direction if it is LTR or RTL.

public function is_rtl() {
    return 'rtl' == $this->text_direction;
}

But I couldn’t find where or who fills in the text_direction data.
If I change the blog language to Arabic then text_direction becomes RTL. Where does it get the language direction information from?

The language list data is retrieved from the trasnaltions api https://api.wordpress.org/translations/core/1.0/ but that doesn’t include the language direction!

Is there another API for this? How can I get the direction of a language that is not set as the blog language?

1 Answer
1

The text_direction property of the WP_Locale class defaults to 'ltr' as per it’s declaration, then may be modified in the class’s init() initialization function in wp-includes/locale.php (near line #207 in WP 4.5) with the following logic:

// Set text direction.
if ( isset( $GLOBALS['text_direction'] ) )
    $this->text_direction = $GLOBALS['text_direction'];
/* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
  $this->text_direction = 'rtl';

if ( 'rtl' === $this->text_direction && strpos( $GLOBALS['wp_version'], '-src' ) ) {
  $this->text_direction = 'ltr';
  add_action( 'all_admin_notices', array( $this, 'rtl_src_admin_notice' ) );
}

Using the initialization logic above, WordPress changes the text direction to the value of the $text_direction global, if it exists. Failing that, it applies the _x() function to retrieve the localized value of the string 'ltr' with the context of 'text direction'.

So, more often than not, the text direction is set by the translation files for the selected language. Here you can see the text direction translation for Arabic in the web-based interface for core translation.

After determining the language to use, WordPress uses the load_default_textdomain() function to provide access to the configured locale’s language data through the $l10n global, which the localization functions then rely upon for translations.

Determining the language direction for non-default languages by the same means that WordPress does requires a multi-language installation. Many of the methods detailed in the linked Codex entry switch out the active locale on the fly, so the active language’s text direction is always available by _x( 'ltr', 'text-direction' );.

Alternately, if you need to get the direction of inactive languages, you could install the translation file for every language whose text direction you wish to acquire, then create instances of the Mo class and invoke import_from_file() to keep language data separated, after which a call to $mo->translate( 'ltr', 'text_direction' ); would return the language’s text direction, if set.

Depending on why you need to acquire the text direction of unloaded languages, it would likely be much more efficient and less resource-intensive to compile a static list of language directions before-hand.

Leave a Comment