PHP 8.5 Introduces Native RTL Detection with locale_is_right_to_left() and Locale::isRightToLeft()
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:
- The global function:
locale_is_right_to_left() - The static method:
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.
🧠 Why RTL Detection Matters
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.
✨ Example of Bidirectional Rendering
A bidirectional page might mix:
- LTR: English (
"Hello") - RTL: Arabic (
"مرحبا"), Hebrew ("שלום"), Urdu ("ہیلو")
Incorrectly rendered, such content can become unreadable. These new functions solve that elegantly.
🆕 What’s New in PHP 8.5?
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.
🔧 Basic Usage Examples
<?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
✅ What You Get
- True for RTL scripts like
'ar','he_IL','fa-IR' - False for LTR scripts or invalid/empty strings
💡 Practical Example: Dynamic 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!
❌ Handling Empty or Invalid Locales
These APIs are smartly built with fallbacks:
- Empty string → returns
false - Invalid locale → returns
false - Missing argument → throws an
ArgumentCountError
This ensures graceful degradation and easy debugging.
🛠️ Polyfill for Older PHP Versions (< 8.5)
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.
🔄 No Backward Compatibility Issues
Good news—these additions are fully backward compatible:
- The
locale_is_right_to_left()function won’t conflict unless you’ve already defined it. Locale::isRightToLeft()is notfinal, so subclassing remains intact.
You can start using them in existing projects right away.
✅ Conclusion
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!
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.