getLanguageInfo & RTL_LOCALES
Returns a language code's display name and text direction; RTL_LOCALES lists the four codes the engine treats as right-to-left.
import { getLanguageInfo, RTL_LOCALES } from '@salla.sa/twilight-theme-engine/i18n';In plain words
Arabic is written from right to left and English from left to right, and a page says which with the HTML dir attribute. getLanguageInfo('ar') returns the plain object { code: 'ar', name: 'العربية', dir: 'rtl' }, ready for dir and lang.
It is an ordinary function, not a hook, so you can call it anywhere. RTL_LOCALES is the list it checks: ['ar', 'fa', 'he', 'ur'].
Signature
function getLanguageInfo(locale: string): {
readonly code: string; // the argument, unchanged
readonly name: string; // 'العربية' for ar, 'English' for en, otherwise locale.toUpperCase()
readonly dir: 'rtl' | 'ltr'; // 'rtl' only when RTL_LOCALES includes locale
};
const RTL_LOCALES: string[]; // ['ar', 'fa', 'he', 'ur']Try it live
getLanguageInfo() returns the display name and text direction the engine uses for a language code.Try this: pick fr: its name is just "FR". Then ar-SA and AR: neither is in RTL_LOCALES, so both read left-to-right.getLanguageInfo("ar")
{
"code": "ar",
"name": "العربية",
"dir": "rtl"
}العربية: this paragraph has dir="rtl", so its text starts on the right.
RTL_LOCALES = ["ar","fa","he","ur"]import { getLanguageInfo } from '@salla.sa/twilight-theme-engine/i18n';
export function LanguageBadge() {
const { name, dir } = getLanguageInfo('ar'); // name: 'العربية', dir: 'rtl'
return (
<span className="language-badge" lang="ar" dir={dir}>
{name}
</span>
);
}
Example
import { getLanguageInfo } from '@salla.sa/twilight-theme-engine/i18n';
export function LanguageBadge({ code }: { code: string }) {
const { name, dir } = getLanguageInfo(code);
return (
<span className="language-badge" lang={code} dir={dir}>
{name}
</span>
);
}
How it behaves
The engine decides direction with it:
rootBeforeLoadandgetTwilightContext().dir(thedira theme puts on<html>) call it with the URL's locale, andI18nProviderderivesuseTranslation()'sdirection,isRTL,isLTRandlanguageNamefrom it.It validates nothing: any string gets an answer, and
getLanguageInfo('xx')is{ code: 'xx', name: 'XX', dir: 'ltr' }. Check a code from outside withisLocalefirst.For a language menu,
useTwilight().settings.languagesholds the store's own languages with real names, flag image URLs andis_rtl.RTL_LOCALESis a plain, mutablestring[], the same arraygetLanguageInforeads. Treat it as read-only.
Gotchas
nameis a real name only foraranden. Every other code comes back upper-cased:frgivesFR,indgivesIND. Fix: show shoppers the names inuseTwilight().settings.languages, ornew Intl.DisplayNames([locale], { type: 'language' }).of(code).Codes are compared exactly:
getLanguageInfo('AR')andgetLanguageInfo('ar-SA')both returndir: 'ltr', becauseRTL_LOCALES.includessees neither asar. Fix: normalise first,getLanguageInfo(code.split('-')[0].toLowerCase()).
Related
The 39 language codes the engine accepts at the start of a URL, and isLocale(), which tells whether a string is one of them.
useTranslationGives a component the translate function t, the page language, its text direction and the language name.
useTwilightReads everything TwilightProvider knows: store, theme, settings, language, direction, current page, login token and the Salla SDK.