Utility FunctionsLocales

getLocaleDirection

API reference for the getLocaleDirection function

Overview

The getLocaleDirection function determines the text direction (left-to-right or right-to-left) for a locale without requiring a GT class instance. It uses the Intl.Locale API to provide accurate direction detection for any valid BCP‑47 locale code.

This function is most often used to set the dir attribute of an HTML element based on the locale.

import { getLocaleDirection } from 'generaltranslation';

const direction = getLocaleDirection('ar-SA');
console.log(direction); // "rtl"

const englishDirection = getLocaleDirection('en-GB');
console.log(englishDirection); // "ltr"

Reference

Parameters

Prop

Type

Parameter Descriptions

ParameterDescription
localeBCP‑47 locale code used to determine text direction

Returns

'ltr' | 'rtl' - Text direction for the locale:

  • 'ltr': Left to right (most languages including English, Spanish, French, German, Chinese, Japanese, etc.)
  • 'rtl': Right to left (Arabic, Hebrew, Persian, Urdu, and other Semitic/Middle Eastern languages)

Behaviour

Direction Detection Algorithm

The function uses the Intl.Locale API’s textInfo.direction property:

  1. Creates an Intl.Locale object for the provided locale
  2. Accesses the textInfo.direction property for the language’s text direction
  3. Returns 'rtl' for right-to-left languages, 'ltr' for all others
  4. Defaults to 'ltr' if the locale is invalid or an error occurs

RTL Language Recognition

Automatically detects RTL languages, including:

  • Arabic (ar, ar-SA, ar-EG, ar-AE, etc.)
  • Hebrew (he, he-IL)
  • Persian/Farsi (fa, fa-IR)
  • Urdu (ur, ur-PK, ur-IN)
  • Pashto (ps)
  • Sindhi (sd)
  • Kurdish Sorani (ckb)
  • And other RTL scripts

Error Handling

  • Invalid or malformed locale codes default to 'ltr'
  • No exceptions are thrown for invalid input
  • Robust fallback behaviour for edge cases

Examples

Basic direction detection

import { getLocaleDirection } from 'generaltranslation';

// Left‑to‑right languages
console.log(getLocaleDirection('en-US')); // "ltr"
console.log(getLocaleDirection('es-ES')); // "ltr"
console.log(getLocaleDirection('fr-FR')); // "ltr"
console.log(getLocaleDirection('ja-JP')); // "ltr"
console.log(getLocaleDirection('zh-CN')); // "ltr"

// Right‑to‑left languages
console.log(getLocaleDirection('ar-SA')); // "rtl"
console.log(getLocaleDirection('he-IL')); // "rtl"
console.log(getLocaleDirection('fa-IR')); // "rtl"
console.log(getLocaleDirection('ur-PK')); // "rtl"

Notes

  • Returns 'ltr' for all left-to-right languages (most languages worldwide)
  • Returns 'rtl' for right-to-left languages (Arabic, Hebrew, Persian, etc.)
  • Uses the modern Intl.Locale API for accurate detection
  • Works with all BCP-47 locale codes

Next steps

How is this guide?

getLocaleDirection