Skip to content

Directionality

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.

Determine if text is predominantly right-to-left:

use Cline\Babel\Babel;
// Arabic text
Babel::from('مرحبا بالعالم')->isRtl(); // true
// Hebrew text
Babel::from('שלום עולם')->isRtl(); // true
// English text
Babel::from('Hello World')->isRtl(); // false
// Mixed (depends on dominant direction)
Babel::from('Hello مرحبا World')->isRtl(); // false (more LTR chars)

Check if any RTL characters exist (regardless of dominance):

// Pure RTL
Babel::from('مرحبا')->containsRtl(); // true
// Mixed content
Babel::from('Hello مرحبا')->containsRtl(); // true
// Pure LTR
Babel::from('Hello World')->containsRtl(); // false
// Numbers only (neutral)
Babel::from('12345')->containsRtl(); // false

Get the dominant direction as a string value:

// Left-to-right
Babel::from('Hello World')->direction(); // "ltr"
// Right-to-left
Babel::from('مرحبا بالعالم')->direction(); // "rtl"
// Mixed directions
Babel::from('Hello مرحبا World مرحبا')->direction(); // "mixed"
// Neutral (numbers, punctuation only)
Babel::from('12345')->direction(); // "neutral"
Babel::from('!!!')->direction(); // "neutral"
// Empty
Babel::from('')->direction(); // "neutral"
Babel::from(null)->direction(); // "neutral"

The direction() method returns one of four values:

ValueDescription
ltrPredominantly left-to-right text
rtlPredominantly right-to-left text
mixedSignificant characters in both directions
neutralOnly neutral characters (numbers, punctuation, whitespace)
function getHtmlDir(string $content): string
{
return Babel::from($content)->direction() === 'rtl' ? 'rtl' : 'ltr';
}
// Usage
$dir = getHtmlDir($userComment);
echo "<p dir=\"{$dir}\">{$userComment}</p>";
function getTextAlignment(string $text): string
{
$direction = Babel::from($text)->direction();
return match ($direction) {
'rtl' => 'right',
'ltr' => 'left',
default => 'left', // Default for mixed/neutral
};
}
function hasBidiContent(string $text): bool
{
return Babel::from($text)->direction() === 'mixed';
}
// Alert users about potential rendering issues
if (hasBidiContent($message)) {
$warning = 'This message contains mixed-direction text.';
}
function validateArabicInput(string $input): bool
{
$babel = Babel::from($input);
// Must be Arabic and RTL dominant
return $babel->containsArabic() && $babel->isRtl();
}

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 mixed direction is returned when both RTL and LTR characters are present in significant amounts
  • Empty strings and null values return neutral