Every page needs a valid language code so screen readers pronounce text correctly. Examples for bilingual public portals.
Screen readers, braille software, translation tools and caption displays all need to know what language the page is written in. WCAG 2.1 success criterion 3.1.1 asks that the default human language of each web page can be programmatically determined. For HTML that means a valid lang attribute on the html element. For PDF it means the language set in the document properties.
Without it, a screen reader falls back to the user's default voice. Spanish text is read with English pronunciation rules and becomes nearly unintelligible. A person with dyslexia using text-to-speech, or someone relying on a braille translation table, has the same problem.
Many public agencies publish the same service in English and Spanish, and sometimes in several more languages. The common failure is a Spanish page generated from an English template whose lang="en" never changes. The page looks perfect to a sighted reader and sounds wrong to everyone using speech. Language is also easy for a scanner to confirm and easy for a person to miss. PDFs need a language too; see documents and PDFs.
<!DOCTYPE html>
<html>
<head><title>Programe su cita</title></head>
<body>…Programe su cita en el DMV…</body>
</html><!DOCTYPE html>
<html lang="es">
<head><title>Programe su cita</title></head>
<body>…Programe su cita en el DMV…</body>
</html><html lang="english">
<html lang="en_US">
<html lang="us"><html lang="en">
<html lang="en-US">
<!-- subtag after the hyphen is a region; "us" is not a language --><!-- layout.html used for every locale -->
<html lang="en">
<body>{{ content }}</body>
</html><!-- layout.html -->
<html lang="{{ page.locale }}"> <!-- "en", "es", "vi", "zh-Hans" -->
<body>{{ content }}</body>
</html>function setLocale(code) {
loadStrings(code);
render();
}function setLocale(code) {
loadStrings(code);
document.documentElement.lang = code;
render();
}Native apps take their language from the device locale and from the localized resources you ship, so the equivalent work is providing full translations rather than adding an attribute. For PDFs, set the document language in the file properties (the /Lang entry in the catalog) when you export.
html lang on one page in each language.zh-Hant-TW.lang. They cannot tell you the page is Spanish while the attribute says English, so spot-check translated pages by reading.body or on a wrapper div, not on html.lang="".lang at all.lang on html using the page's actual language.