Directionality
Overview
Section titled “Overview”Some languages read right-to-left (RTL), such as Arabic, Hebrew, and Persian. Babel provides methods to detect text direction for proper UI rendering and layout decisions.
Check RTL Dominance
Section titled “Check RTL Dominance”Determine if text is predominantly right-to-left:
use Cline\Babel\Babel;
// Arabic textBabel::from('مرحبا بالعالم')->isRtl(); // true
// Hebrew textBabel::from('שלום עולם')->isRtl(); // true
// English textBabel::from('Hello World')->isRtl(); // false
// Mixed (depends on dominant direction)Babel::from('Hello مرحبا World')->isRtl(); // false (more LTR chars)Check for RTL Presence
Section titled “Check for RTL Presence”Check if any RTL characters exist (regardless of dominance):
// Pure RTLBabel::from('مرحبا')->containsRtl(); // true
// Mixed contentBabel::from('Hello مرحبا')->containsRtl(); // true
// Pure LTRBabel::from('Hello World')->containsRtl(); // false
// Numbers only (neutral)Babel::from('12345')->containsRtl(); // falseGet Text Direction
Section titled “Get Text Direction”Get the dominant direction as a string value:
// Left-to-rightBabel::from('Hello World')->direction(); // "ltr"
// Right-to-leftBabel::from('مرحبا بالعالم')->direction(); // "rtl"
// Mixed directionsBabel::from('Hello مرحبا World مرحبا')->direction(); // "mixed"
// Neutral (numbers, punctuation only)Babel::from('12345')->direction(); // "neutral"Babel::from('!!!')->direction(); // "neutral"
// EmptyBabel::from('')->direction(); // "neutral"Babel::from(null)->direction(); // "neutral"Return Values
Section titled “Return Values”The direction() method returns one of four values:
| Value | Description |
|---|---|
ltr | Predominantly left-to-right text |
rtl | Predominantly right-to-left text |
mixed | Significant characters in both directions |
neutral | Only neutral characters (numbers, punctuation, whitespace) |
Use Cases
Section titled “Use Cases”HTML Direction Attribute
Section titled “HTML Direction Attribute”function getHtmlDir(string $content): string{ return Babel::from($content)->direction() === 'rtl' ? 'rtl' : 'ltr';}
// Usage$dir = getHtmlDir($userComment);echo "<p dir=\"{$dir}\">{$userComment}</p>";Dynamic Layout
Section titled “Dynamic Layout”function getTextAlignment(string $text): string{ $direction = Babel::from($text)->direction();
return match ($direction) { 'rtl' => 'right', 'ltr' => 'left', default => 'left', // Default for mixed/neutral };}Bidirectional Text Warning
Section titled “Bidirectional Text Warning”function hasBidiContent(string $text): bool{ return Babel::from($text)->direction() === 'mixed';}
// Alert users about potential rendering issuesif (hasBidiContent($message)) { $warning = 'This message contains mixed-direction text.';}Form Input Validation
Section titled “Form Input Validation”function validateArabicInput(string $input): bool{ $babel = Babel::from($input);
// Must be Arabic and RTL dominant return $babel->containsArabic() && $babel->isRtl();}RTL Scripts
Section titled “RTL Scripts”The following Unicode scripts are considered right-to-left:
- Arabic
- Hebrew
- Syriac
- Thaana (Maldivian)
- N’Ko
- Samaritan
- Mandaic
- Numbers and punctuation are considered neutral and don’t affect direction
- Common characters (spaces, basic punctuation) are also neutral
- The
mixeddirection is returned when both RTL and LTR characters are present in significant amounts - Empty strings and null values return
neutral