STR-006 Name the page's main language in the lang attribute on the html element, and change it when the interface changes language
- Level
- Rule
- Verdict
- Requirement
- Area
- Page structure
- Status
- proposed, version 1.0.0
Rule
Put a lang attribute on the html element whose value is a valid language tag for the language most of the page is written in. When the interface switches to another language without loading a new page, change the attribute to that language at the same time.
Examples
Live examples built with Justified's components. Try them with the mouse, the keyboard or a screen reader.
Do: The attribute follows the language
Book a table
Two guests, Friday at 19:00
lang on the page's html changes with the interface. The frame only draws the page; this page's own html is left alone.Don't: Every string changes, lang stays en
Book a table
Two guests, Friday at 19:00
Don't: The language sits on a div around the app
Rezerwacja stolika
Dwie osoby, piątek, 19:00
pl, but html still says en. The head and a toast in a portal fall back to English; so would a dialog or a menu.Don't: A wrong or missing value
Rezerwacja stolika
Dwie osoby, piątek, 19:00
The page is in Polish and html names English. Every word is read with English rules.
en, en_US, english, cn, an empty value and no attribute all fail.Why
A screen reader picks its voice and pronunciation from the page's language. When an app written in German says lang="en", every word is read with English rules, and the person hears something close to noise. Braille software, browser translation, spell checkers and font selection lean on the same value. With no attribute at all, each tool falls back to its own default, which may not be the page's language.
This is a requirement. WCAG 2.2, 3.1.1 Language of Page asks that software can tell the default human language of each page (Understanding 3.1.1). In HTML the Working Group's technique is lang on the html element, with a valid tag that matches the main language (H57). A single page app is one web page while it runs, so when its interface turns into another language in place, its main language has changed and the attribute has to follow. That is the criterion read for single page apps, as STR-001 reads 2.4.2 for titles, not something we add. Parts of the page in another language are STR-006a.
Do
- The booking app Larkspur is built in English and also offered in Polish, Portuguese and Korean. Its root layout renders
<html lang={locale}>, so the English view sayslang="en", the Polish onelang="pl", the Portuguese onelang="pt"and the Korean onelang="ko". - With the Next.js App Router and a locale segment, read the locale from the route params in
app/[locale]/layout.tsxand pass it tohtml. With the Pages Router's built-ini18nconfig, Next.js sets the attribute per locale; a custom_document.tsxmust not hard code one. - With react-i18next, Lingui or your own context, set
document.documentElement.langwhenever the language changes, for example in thelanguageChangedhandler or in an effect that depends on the current locale. When the page is rendered on the server, render the right value there too, so the first paint already names the language. - Change the starter template's
lang="en"when the product's interface is in another language. Vite andcreate-next-appboth ship it. - Use hyphens, as language tags do:
en-GB,pt-BR. An id with an underscore from an i18n library or a back end, such asen_USorsv_SE, is converted first. A region part is allowed but not required;enis enough. - Use the language code, not a country code:
svis Swedish,seis Northern Sami, andukis Ukrainian. - A visa form shows every label in English and, beneath it, in French. Both carry the same amount of text and English comes first, so
htmlsayslang="en"and each French line is marked as STR-006a says. Had French carried most of the page,htmlwould name French. - Our own advice, not part of 3.1.1: for a right-to-left language, set
dir="rtl"onhtmlat the same moment. Tailwind'srtl:variant and CSS logical properties follow the attribute. Radix based shadcn parts do not read it: wrap the app in Radix'sDirectionProvider, or give each part adirprop, with the same value. - Our own advice: keep the current locale in one place, so
lang, the page title (STR-001) and date and number formatting cannot drift apart.
Don't
- Ship an app whose interface is in Polish with
<html lang="en">left from the template. - Render
<html>with nolang, or withlang="". - Write
lang="en_US",lang="english"orlang="cn". - Switch every string to Portuguese when the person picks Português, and leave
lang="en"onhtml. - Put
lang={locale}on adivaround the app instead of onhtml. Thetitleand everything else inheadstay outside it. So does a shadcnDialog,Sheet,Popover,DropdownMenu,SelectorTooltip, which renders in a portal at the end ofbody, and a SonnerToastermounted outside the wrapper, as root layouts often do. All of them fall back to whateverhtmlsays. - Rely on the HTTP
Content-Languageheader alone. It does not replace the attribute.
Exceptions
- Parts in another language. This rule covers only the root. A quotation, an entry in a language picker or any other passage in another language is STR-006a.
- Linked pages.
langnames the language of this page, not of the pages it links to. A link's target language is whathreflangis for. - XHTML.
xml:langis needed only for pages served as XML. An HTML page needs onlylang.
Verdict
Requirement. Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work
| Source | Role | Stance | Page |
|---|---|---|---|
| WCAG 2.2 | Law | Requires | 3.1.1 Language of Page |
| Material Design | Judge | No position | |
| Apple Human Interface Guidelines | Judge | No position | |
| Carbon Design System | Judge | No position | |
| GOV.UK Design System | Expert witness | Supports | Page template |
| Nielsen Norman Group | Expert witness | No position |
Each stance links to the page it rests on, read on the date in the source register. This page never repeats what a source says; read the source itself.
History
- Version 1.0.0, : First version.
Last checked against the other rules for conflicts on .
More in Page structure: 7 rules and 1 guideline · All rules
Justified is an independent project. It is not affiliated with, sponsored or endorsed by the organisations whose guidance it cites. Read the full statement.
Apple is a trademark of Apple Inc., registered in the U.S. and other countries and regions.