Language switcher & RTL
Language switcher UI
Use the ui-languages.json manifest to build a language selector. ai-i18n-tools exports two display helpers — see Runtime helpers → Display helpers for signatures.
Example LanguageSelect component (React)
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
getUILanguageLabel,
getUILanguageLabelNative,
type UiLanguageManifestRow,
} from 'ai-i18n-tools/runtime';
import uiLanguages from './locales/ui-languages.json';
import { loadLocale } from './i18n';
function LanguageSelect({
value,
onChange,
}: {
value: string;
onChange: (code: string) => void;
}) {
const { t, i18n } = useTranslation();
const options = useMemo(
() =>
(uiLanguages as UiLanguageManifestRow[]).map((lang) => ({
code: lang.code,
// Settings/content dropdowns: shows translated name when available
label: getUILanguageLabel(lang, t),
// Header globe menu: shows "English / Deutsch"-style label, no t() call
nativeLabel: getUILanguageLabelNative(lang),
})),
[t]
);
const handleChange = async (code: string) => {
await loadLocale(code);
await i18n.changeLanguage(code);
onChange(code);
};
return (
<select value={value} onChange={(e) => handleChange(e.target.value)}>
{options.map((row) => (
<option key={row.code} value={row.code}>
{row.label}
</option>
))}
</select>
);
}getUILanguageLabel(lang, t) - shows t(englishName) when translated, or englishName / t(englishName) when both differ. Suitable for settings screens.
getUILanguageLabelNative(lang) - shows englishName / label (no t() call on each row). Suitable for header menus where you want the native name visible.
The ui-languages.json manifest is a JSON array of "{ code, label, englishName, direction }" entries (direction is "ltr" or "rtl"). Example:
[
{ "code": "en-GB", "label": "English (UK)", "englishName": "English (UK)", "direction": "ltr" },
{ "code": "pt-BR", "label": "Português (BR)", "englishName": "Portuguese (BR)", "direction": "ltr" },
{ "code": "de", "label": "Deutsch", "englishName": "German", "direction": "ltr" },
{ "code": "fr", "label": "Français", "englishName": "French", "direction": "ltr" },
{ "code": "ar", "label": "العربية", "englishName": "Arabic", "direction": "rtl" }
]The manifest is generated by generate-ui-languages or extract from sourceLocale + targetLocales and the bundled master catalog. It is written to languagesManifestPath (defaults to {ui.flatOutputDir}/ui-languages.json when omitted). If you change locales in config, run generate-ui-languages or extract again to refresh the file.
RTL languages
ai-i18n-tools exports getTextDirection(lng) and applyDirection(lng) — see Runtime helpers → RTL helpers.
import { getTextDirection, applyDirection } from 'ai-i18n-tools/runtime';
getTextDirection('ar') // 'rtl'
getTextDirection('en-GB') // 'ltr'
// Applied automatically via i18n.on('languageChanged', applyDirection) — see Wire i18nextapplyDirection sets document.documentElement.dir (browser) or is a no-op (Node.js). Pass an optional element argument to target a specific element. Wire it in your i18n bootstrap — Wire i18next.
For strings that may contain → arrows, flip them for RTL layouts:
import { flipUiArrowsForRtl } from 'ai-i18n-tools/runtime';
const { i18n } = useTranslation();
const isRtl = getTextDirection(i18n.language) === 'rtl';
const label = flipUiArrowsForRtl(t('Next → Step'), isRtl);