Check if Locale is RTL using locale_is_right_to_left in PHP 8.5

Check if Locale is RTL using locale_is_right_to_left in PHP 8.5

The Intl extension in PHP provides functionality for performing locale-related operations, including formatting, transliteration, character encoding conversion, and more. Arabic, Hebrew, and several other languages are written from right-to-left (RTL). Although the list of RTL scripts is relatively short, adjusting web layouts, email templates, and console output to the correct text direction is essential for accessibility and usability.

Since PHP 8.5, the Intl extension includes the locale_is_right_to_left function and the Locale::isRightToLeft static method, which allow us to check whether a given locale uses an RTL script. Both accept a locale name as their parameter and return true or false depending on whether the locale uses an RTL script. If an empty string or an invalid locale is provided, they return false.

<?php

var_dump(locale_is_right_to_left('en_US')); // false
var_dump(locale_is_right_to_left('de_DE')); // false
var_dump(locale_is_right_to_left('')); // false
var_dump(locale_is_right_to_left('invalid')); // false
var_dump(locale_is_right_to_left('ar')); // true
var_dump(locale_is_right_to_left('he_IL')); // true
// or
var_dump(Locale::isRightToLeft('en_US')); // false
var_dump(Locale::isRightToLeft('ar')); // true

Leave a Comment

Cancel reply

Your email address will not be published.