Kritim Yantra
Apr 25, 2025
If you've ever worked on multilingual web applications, you know how tricky it can be to handle bidirectional text—especially when mixing left-to-right (LTR) and right-to-left (RTL) languages like Arabic, Hebrew, or Urdu.
With the release of PHP 8.5, RTL detection becomes effortless thanks to two powerful new Intl extension features:
locale_is_right_to_left()
Locale::isRightToLeft()
These functions take a single locale string (e.g., 'ar-EG'
, 'he_IL'
) and return a boolean indicating whether the locale’s primary script is RTL. No more hardcoded lists. No more brittle polyfills.
Modern UIs need to adapt dynamically to user locales. From web layouts and email templates to console outputs, ensuring the correct text direction is crucial for accessibility and usability.
A bidirectional page might mix:
"Hello"
)"مرحبا"
), Hebrew ("שלום"
), Urdu ("ہیلو"
)Incorrectly rendered, such content can become unreadable. These new functions solve that elegantly.
PHP 8.5 introduces these two RTL-aware APIs via the Intl extension:
function locale_is_right_to_left(string $locale): bool;
class Locale {
public static function isRightToLeft(string $locale): bool;
}
Both methods share the same behavior and internally rely on ICU's up-to-date Unicode BiDi data. RTL scripts such as Arabic, Hebrew, Farsi, Urdu, Divehi, Kurdish, Pashto, and others are recognized instantly.
<?php
echo locale_is_right_to_left('en_US') ? 'RTL' : 'LTR'; // Outputs: LTR
echo locale_is_right_to_left('ar') ? 'RTL' : 'LTR'; // Outputs: RTL
echo Locale::isRightToLeft('he_IL') ? 'RTL' : 'LTR'; // Outputs: RTL
echo Locale::isRightToLeft('invalid') ? 'RTL' : 'LTR'; // Outputs: LTR
'ar'
, 'he_IL'
, 'fa-IR'
dir
in HTML<?php
$locale = 'fa-IR'; // Farsi
$dir = locale_is_right_to_left($locale) ? 'rtl' : 'ltr';
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($locale) ?>" dir="<?= $dir ?>">
<head>
<meta charset="UTF-8">
<title>My Multilingual App</title>
</head>
<body>
<p><?= $dir === 'rtl' ? 'این متن راستبهچپ است.' : 'This text is left-to-right.' ?></p>
</body>
</html>
✨ This snippet automatically sets the dir
attribute based on the user's locale. No need to maintain a hardcoded list of RTL locales!
These APIs are smartly built with fallbacks:
false
false
ArgumentCountError
This ensures graceful degradation and easy debugging.
Still using an older PHP version? Here's a simple polyfill using a regular expression to match known RTL language codes:
function locale_is_right_to_left(string $locale): bool {
return (bool) preg_match(
'/^(?:ar|he|fa|ur|ps|sd|dv|ku[_-]Arab|ug|yi)(?:[_-].*)?$/i',
$locale
);
}
While not as robust as ICU-based detection, this solution covers most real-world cases.
Good news—these additions are fully backward compatible:
locale_is_right_to_left()
function won’t conflict unless you’ve already defined it.Locale::isRightToLeft()
is not final
, so subclassing remains intact.You can start using them in existing projects right away.
The new locale_is_right_to_left()
and Locale::isRightToLeft()
functions in PHP 8.5 represent a huge win for internationalization. They simplify UI logic, eliminate the need for fragile lists, and rely on authoritative ICU data.
Whether you're building a Laravel app, WordPress plugin, or custom PHP tool—this addition makes your multilingual code cleaner and more maintainable.
💬 Ready to embrace smarter locale handling in PHP 8.5? Start using these functions today and let your app speak every language beautifully!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google