LEARNING OBJECTIVES โต
- Format valid IETF BCP 47 language tags (Language-Script-Region) for international documents.
- Implement root language configuration (
<html lang="...">) to satisfy WCAG 2.2 Criterion 3.1.1. - Apply inline language switching (
<span lang="...">) for multilingual phrases (WCAG 3.1.2). - Explain how assistive technologies switch Text-To-Speech (TTS) pronunciation dictionaries dynamically.
- Leverage the CSS
:lang()pseudo-class and enable localized typographic hyphenation (hyphens: auto).
๐ The Mental Model & Story (Intuitive Foundation)
Imagine a world-class international interpreter reading an English novel out loud to a room of listeners.
Suddenly, the text includes a famous French phrase: "C'est la vie".
If the interpreter tries to pronounce those French words using rigid English phonetic rules, it sounds like bizarre nonsense: "Sest lah vye". The audience is confused and jarred.
However, if the text has a tiny language flag next to the phrase (Inline lang="fr" Attribute), the interpreter immediately switches their vocal chords, accent, and pronunciation engine to authentic French: "/sษ la vi/".
+-------------------------------------------------------------------------------+
| SCREEN READER VOICE SYNTHESIS ENGINE |
+-------------------------------------------------------------------------------+
| |
| Markup: <p>Welcome to our <span lang="es">restaurante</span>.</p> |
| |
| 1. Reading "Welcome to our" ===> Loads US English Voice (Alex / Samantha) |
| 2. Hits <span lang="es"> ===> Switches to Spanish Voice (Jorge / Paulina)|
| 3. Pronounces "restaurante" ===> Flawless Spanish Phonetic Accent |
| 4. Closes </span> ===> Reverts back to US English Voice |
| |
+-------------------------------------------------------------------------------+
The lang attribute is the universal phonetic passport of the Web. Without it, screen readers butcher foreign words, browser spellcheckers flag valid international words as typos, translation engines fail, and typography hyphenation breaks.
Technical Deep Dive & Specifications
IETF BCP 47 Language Tag Anatomy
The WHATWG specification requires the lang attribute value to be a valid BCP 47 language tag (managed by the IETF).
A BCP 47 tag is composed of standardized subtags separated by hyphens:
language - [Script] - [REGION] - [variant]
| | | |
Examples: en - US - (English as used in USA)
zh - Hans - CN - (Chinese, Simplified script, China)
pt - BR - (Portuguese as used in Brazil)
es - 419 - (Spanish as used in Latin America)
sr - Latn - RS - (Serbian written in Latin script)
| Subtag Component | Standard | Length / Format | Example Values |
|---|---|---|---|
| Primary Language | ISO 639-1 / 639-2 | 2 or 3 letters (lowercase) | en (English), es (Spanish), ja (Japanese), ar (Arabic), hi (Hindi) |
| Script (Optional) | ISO 15924 | 4 letters (Titlecase) | Hans (Simplified), Hant (Traditional), Latn (Latin), Cyrl (Cyrillic) |
| Region (Optional) | ISO 3166-1 / UN M.49 | 2 letters (UPPERCASE) or 3 digits | US (United States), GB (Great Britain), CA (Canada), 419 (Latin America) |
WCAG 2.2 Compliance Requirements
Accessibility standards place the highest priority on language declaration:
- WCAG 3.1.1: Language of Page (Level A): The default human language of each web page must be programmatically determined via the root
<html lang="...">tag. - WCAG 3.1.2: Language of Parts (Level AA): The human language of each passage or phrase in the content must be programmatically determined via inline
lang="..."attributes.
The CSS :lang() Pseudo-Class & Localized Quotations
The :lang() pseudo-class matches elements based on their effective inherited language. It is aware of hyphenated subcodes (e.g., :lang(en) matches both en-US and en-GB).
A classic production use case is localized typographic quotation marks:
/* English quotes: โ โ */
:lang(en) > q {
quotes: "โ" "โ" "โ" "โ";
}
/* French guillemets: ยซ ยป */
:lang(fr) > q {
quotes: "ยซ " " ยป" "โน " " โบ";
}
/* German quotes: โ โ */
:lang(de) > q {
quotes: "โ" "โ" "โ" "โ";
}
CSS Hyphenation Engine Activation (hyphens: auto)
Modern responsive design requires text to break cleanly across narrow mobile screens. The CSS property hyphens: auto relies 100% on the lang attribute to select the correct linguistic hyphenation dictionary:
p {
text-align: justify;
hyphens: auto; /* Requires valid lang="en-US" or lang="de" on parent element! */
}
If lang is missing, the browser engine cannot safely hyphenate words and falls back to ugly text overflow or wide justification gaps.
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 2 (
<html lang="en-US">): Declares the root language for the entire document as United States English (WCAG 3.1.1). - Lines 20โ22 (
:lang(fr) > q,:lang(de) > q): Automatically injects French guillemets (ยซ ยป) and German low-high quotes (โ โ) for<q>tags without writing extra classes. - Lines 26โ29 (
hyphens: auto): Tells the browser to break long words according to official dictionary hyphenation rules for each respective language. - Lines 50, 57, 65 (
lang="fr",lang="de",lang="ja"): Signals to screen readers to switch phonetic synthesis engines mid-sentence for foreign words (WCAG 3.1.2).
Expected Browser Render Output
+-----------------------------------------------------------------------+
| The International Literary Review |
| |
| As the author stated, โEvery journey begins with an unturned stone.โ |
| |
| Jean-Paul Sartre: ยซ L'existence prรฉcรจde l'essence ยป. They accepted... |
| |
| Goethe once remarked, โEs ist nicht genug zu wissen, man muss...โ |
+-----------------------------------------------------------------------+๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Build an Accessible Multilingual Bistro Menu
You are building an international menu for an upscale culinary restaurant. The menu is authored primarily in English, but contains French dish names, Italian appetizers, and Spanish specials.
Your Task:
- Configure the document root language as British English (
lang="en-GB"). - Wrap all French dish titles and culinary terms in appropriate
lang="fr"tags. - Wrap all Italian menu items in
lang="it"tags. - Wrap the Spanish chef's special in
lang="es"tags. - Add CSS rules using
:lang()to apply italic styling and distinct quotes to each foreign phrase.
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Country Code vs Language Code Confusion: Writing
lang="uk"does not declare United Kingdom Englishโit declares Ukrainian! The correct code for British English islang="en-GB". - Missing Root
langAttribute: Omittinglangon<html>causes browsers to trigger annoying automatic translation popups (e.g. Chrome asking "Translate this page to English?") on pages that are already written in English. - Using Underscores Instead of Hyphens: Writing
lang="en_US"is invalid under BCP 47. BCP 47 subtags must always be separated by hyphens:lang="en-US".
๐ก Pro Tips
- Automatic Hyphenation Performance: Always pair
hyphens: auto;with preciselangdeclarations (e.g.,lang="de"for German) to prevent broken multi-syllable word wraps on mobile devices. - SEO Geotargeting & Indexing: Search engine crawlers (Googlebot, Bingbot) use the
langattribute andContent-Languageheaders to index and serve region-specific search results. - Script Subtags for Asian Languages: For Chinese, specify the script subtag explicitly:
lang="zh-Hans"(Simplified Chinese) vslang="zh-Hant"(Traditional Chinese) rather than ambiguouslang="zh".
๐ Key Takeaways
- The
langattribute specifies the natural language of an element using IETF BCP 47 language tags. - Declaring
<html lang="...">satisfies WCAG 2.2 Criterion 3.1.1 (Language of Page). - Inline
langtags satisfy WCAG 2.2 Criterion 3.1.2 (Language of Parts), enabling screen readers to switch TTS voices dynamically. - The
:lang()CSS pseudo-class allows developers to automate localized quotation marks and typography. - CSS
hyphens: autodepends strictly on a validlangattribute to load the proper linguistic hyphenation dictionary. - --