Sub topic 10
Keeping html lang and dir in sync
This chapter assumes you have react-i18next set up, know how to call i18n.changeLanguage, and can read BCP 47 tags such as en or ar-EG. Changing the text on screen is only half of a locale switch. The <html> element also has to describe the new language.
Why the two attributes matter
The lang attribute names the language of the page content. Screen readers use it to pick a pronunciation voice. Browsers use it for hyphenation, spell checking, quotation marks and font selection. Translation tools also read it. If lang="en" stays on a page that now shows German, a screen reader pronounces German words with English rules.
The dir attribute sets the base text direction. It is ltr for left-to-right scripts and rtl for scripts such as Arabic and Hebrew. Browsers use it to align text, order inline content and mirror form controls.
Your static index.html sets these once. i18next changes the language at runtime, so your code has to update the attributes each time.
Updating the attributes on a language change
The language switcher from the previous chapter did this inside its change handler:
async function change(code: string) {
await i18n.changeLanguage(code);
document.documentElement.lang = code;
document.documentElement.dir = i18n.dir(code);
}i18n.dir(code) returns 'ltr' or 'rtl' for a language code, so you don't need to keep your own list of right-to-left languages.
This works, but only for changes made through that component. The language can also change at startup, when a language detector picks one, or when other code calls changeLanguage. A handler in one component misses those cases.
Subscribing to the language instead
i18next emits a languageChanged event whenever the active language changes, whatever caused it. Subscribing to it puts the update in one place.
import i18n from './i18n';
function syncHtmlAttributes(lng: string) {
const root = document.documentElement;
root.lang = lng;
root.dir = i18n.dir(lng);
}
i18n.on('languageChanged', syncHtmlAttributes);
syncHtmlAttributes(i18n.resolvedLanguage ?? i18n.language);The last line matters. If initialisation finished before you registered the listener, the event has already fired, so you apply the current value once by hand.
Common errors
- Setting
langbut notdir. Arabic text then renders in a left-to-right layout. - Setting the attributes before
changeLanguageresolves. Catalogs may still be loading, so the page briefly mismatches. Await the promise or use the event. - Putting
diron<body>or a wrapperdiv. It works for most content, but the root element is the right place, and modals rendered in portals inherit from it. - Marking mixed-language content wrongly. If a French quotation appears in an English page, give that element its own
lang="fr"instead of changing the root.
Exercise
A user opens your app in a browser set to Arabic. A language detector selects ar during startup, and no one clicks the switcher. Why does a handler inside the switcher's onChange fail to set dir="rtl", and what fixes it?
Show answer
The onChange handler runs only when the user picks a language in that component. Startup detection calls changeLanguage without it, so the attributes are never updated. Subscribe to the languageChanged event with i18n.on, and also apply the current language once after registering, in case initialisation has already finished.