GT ClassMethodsLocales
getLocaleDirection
API reference for the GT getLocaleDirection method
Overview
The getLocaleDirection method determines the text direction (left-to-right or right-to-left) for a locale using the Intl.Locale API.
It returns either 'ltr' for left-to-right languages or 'rtl' for right-to-left languages.
const gt = new GT({
sourceLocale: 'en',
targetLocale: 'ar'
});
const direction = gt.getLocaleDirection('ar');
console.log(direction); // "rtl"
const englishDirection = gt.getLocaleDirection('en-US');
console.log(englishDirection); // "ltr"Reference
Parameters
Prop
Type
Parameters Description
| Parameter | Description |
|---|---|
locale | BCP‑47 locale code to check the text direction for. If not provided, uses the instance’s targetLocale. |
Returns
'ltr' | 'rtl' - Text direction for the locale:
'ltr': Left to right (most languages)'rtl': Right to left (Arabic, Hebrew, Persian, Urdu, etc.)
Throws
Error– if no locale is provided and the instance has notargetLocaleconfigured
Example
const gt = new GT({
sourceLocale: 'en',
targetLocale: 'ar'
});
// Check different language directions
console.log(gt.getLocaleDirection()); // "rtl" (uses targetLocale 'ar')
console.log(gt.getLocaleDirection('en')); // "ltr"
console.log(gt.getLocaleDirection('ar')); // "rtl"
console.log(gt.getLocaleDirection('he')); // "rtl"
console.log(gt.getLocaleDirection('fa')); // "rtl"
console.log(gt.getLocaleDirection('fr')); // "ltr"Notes
- Returns
'ltr'for all left-to-right languages (most languages worldwide) - Returns
'rtl'for right-to-left languages (Arabic, Hebrew, Persian, etc.)
Next steps
- Get locale properties with
get-locale-properties - Validate locales with
is-valid-locale - Get locale emoji with
get-locale-emoji - Explore standalone
get-locale-direction
How is this guide?