Skip to content

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
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

Pick Polski, Português or 한국어 and watch the code line: 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

Switch the language: the text turns Polish, Portuguese or Korean, but the code line still says English, so a screen reader keeps reading with English sounds.

Don't: The language sits on a div around the app

Rezerwacja stolika

Dwie osoby, piątek, 19:00

The view's div says 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.

Try each value on a Polish page. A template's 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 says lang="en", the Polish one lang="pl", the Portuguese one lang="pt" and the Korean one lang="ko".
  • With the Next.js App Router and a locale segment, read the locale from the route params in app/[locale]/layout.tsx and pass it to html. With the Pages Router's built-in i18n config, Next.js sets the attribute per locale; a custom _document.tsx must not hard code one.
  • With react-i18next, Lingui or your own context, set document.documentElement.lang whenever the language changes, for example in the languageChanged handler 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 and create-next-app both 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 as en_US or sv_SE, is converted first. A region part is allowed but not required; en is enough.
  • Use the language code, not a country code: sv is Swedish, se is Northern Sami, and uk is 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 html says lang="en" and each French line is marked as STR-006a says. Had French carried most of the page, html would name French.
  • Our own advice, not part of 3.1.1: for a right-to-left language, set dir="rtl" on html at the same moment. Tailwind's rtl: variant and CSS logical properties follow the attribute. Radix based shadcn parts do not read it: wrap the app in Radix's DirectionProvider, or give each part a dir prop, 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 no lang, or with lang="".
  • Write lang="en_US", lang="english" or lang="cn".
  • Switch every string to Portuguese when the person picks Português, and leave lang="en" on html.
  • Put lang={locale} on a div around the app instead of on html. The title and everything else in head stay outside it. So does a shadcn Dialog, Sheet, Popover, DropdownMenu, Select or Tooltip, which renders in a portal at the end of body, and a Sonner Toaster mounted outside the wrapper, as root layouts often do. All of them fall back to whatever html says.
  • Rely on the HTTP Content-Language header 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. lang names the language of this page, not of the pages it links to. A link's target language is what hreflang is for.
  • XHTML. xml:lang is needed only for pages served as XML. An HTML page needs only lang.

Verdict

Requirement. Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work

Stances on STR-006
SourceRoleStancePage
WCAG 2.2LawRequires3.1.1 Language of Page
Material DesignJudge No position
Apple Human Interface GuidelinesJudge No position
Carbon Design SystemJudge No position
GOV.UK Design SystemExpert witness SupportsPage template
Nielsen Norman GroupExpert 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