[8.5] Add locale_is_right_to_left#527
Open
alexander-schranz wants to merge 3 commits intosymfony:1.xfrom
Open
Conversation
30b4e10 to
beacebd
Compare
stof
reviewed
May 26, 2025
src/Php85/Php85.php
Outdated
| } | ||
|
|
||
| public static function locale_is_right_to_left(string $locale): bool { | ||
| return (bool) preg_match('/^(?:ar|he|fa|ur|ps|sd|ug|ckb|yi|dv|ku_arab|ku-arab)(?:[_-].*)?$/i', $locale); |
Member
There was a problem hiding this comment.
I don't think this is the right implementation. What is right to left is a script, not a language. And locales might specify a script explicitly which is not the most likely script.
Author
There was a problem hiding this comment.
I see, any suggestions? Sadly the original code does not help here: unicode-org/icu@53dcbe6
- A script is right-to-left according to the CLDR script metadata
- which corresponds to whether the script's letters have Bidi_Class=R or AL.
Member
There was a problem hiding this comment.
This is what Gemini provides, let's take inspiration from this snippet?
function locale_is_right_to_left(string $locale): bool
{
// Define the list of RTL scripts within the function scope.
// These are 4-letter ISO 15924 script codes.
static $rtlScripts = [
'Adlm', 'Arab', 'Armi', 'Hebr', 'Mani', 'Mend', 'Nkoo',
'Phnx', 'Rohg', 'Samr', 'Syrc', 'Thaa',
];
// This is a minimal, hardcoded version of the CLDR "likelySubtags" data.
// It maps a language code to its most likely SCRIPT code.
// This list is NOT exhaustive and is the primary weakness of this approach.
static $languageToLikelyRtlScript = [
'ar' => 'Arab', // Arabic
'fa' => 'Arab', // Persian (Farsi)
'ur' => 'Arab', // Urdu
'ps' => 'Arab', // Pashto
'sd' => 'Arab', // Sindhi
'ug' => 'Arab', // Uyghur
'ckb' => 'Arab', // Sorani Kurdish
'he' => 'Hebr', // Hebrew
'yi' => 'Hebr', // Yiddish
'dv' => 'Thaa', // Dhivehi
'nqo' => 'Nkoo', // N'Ko
];
if (empty($locale)) {
return false;
}
// Normalize separators and split the locale into parts.
$localeParts = preg_split('/[_-]/', $locale);
$language = strtolower($localeParts[0] ?? '');
$script = null;
// Look for an explicit script subtag (always 4 letters).
foreach ($localeParts as $part) {
if (strlen($part) === 4 && ctype_alpha($part)) {
// Capitalize the first letter for standard format (e.g., "Arab", "Latn").
$script = ucfirst(strtolower($part));
break;
}
}
// If no explicit script was found, try to infer it from our map.
if ($script === null) {
$script = $languageToLikelyScript[$language] ?? null;
}
// If we couldn't determine a script, we can't determine direction.
if ($script === null) {
// Fallback for languages where the code itself is a strong indicator
if (in_array($language, ['ar', 'he', 'fa', 'ur', 'ps', 'sd', 'ug', 'ckb', 'yi', 'dv'])) {
return true;
}
return false;
}
// Check if the determined script is in our list of RTL scripts.
return in_array($script, $rtlScripts, true);
}
}
Member
|
our |
locale_is_right_to_left
locale_is_right_to_leftlocale_is_right_to_left
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See https://php.watch/versions/8.5/locale_is_right_to_left-Locale-isRightToleft