Language, Direction and Internationalization
What lang and dir tell the browser and assistive technology, how :lang() and text-transform react, and when dir=auto and bdi keep mixed-direction text correct.
HTML Security: sandbox, noopener and EscapingAnatomy of an HTML Document
Language, Direction and Internationalization
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `lang` declares the language of the page or of one element. Browsers use it for font choice, hyphenation, quotes and case rules, and screen readers use it to pick a pronunciation.
- Language changes real behaviour: `text-transform: uppercase` turned "istanbul" into "ISTANBUL" under `lang="en"` and "İSTANBUL" under `lang="tr"` in a test.
- `:lang(fr)` matches `fr-CA` too, so one selector can cover regional variants.
- `dir="rtl"` flips inline flow and flex row order, and `text-align: start` follows it. `dir="auto"` picks a direction from the first strong character of the content.
- Set `lang` on the `html` element always, and on any inline run in another language. A missing `lang` is a basic accessibility failure that axe and the HTML validator both flag.
Text isn’t universal
HTML text looks like a string of letters. But reading a page involves choices that depend on which language it is in: which font has the right glyphs, where a long word may break, which quotation marks to use, how to uppercase, which way the line runs, and how a screen reader should pronounce it. A browser can guess from the characters, and sometimes wrongly, so HTML lets you say it outright. Two attributes carry most of the load: lang and dir.
Think of lang as the language tag on a package of text, read by everything downstream: the browser’s rendering, the search engine, the translation tool and the screen reader.
lang
Set it once on the root, and again wherever the language changes:
<html lang="en">
<p>The French say <q lang="fr">bonne chance</q>.</p>
It inherits down the tree. The value is a BCP 47 tag: en, fr-CA, zh-Hant. I tested its effects in Chromium 159, Firefox 159 and a WebKit build, with identical results:
- Case rules.
text-transform: uppercaseon “istanbul” gave “ISTANBUL” in English and “İSTANBUL” underlang="tr". - CSS selection.
:lang(fr)matched an element withlang="fr-CA", and did not match English text. - Inheritance. An element’s
.langreflected its ownlangvalue (“fr”) while the document’s stayed “en”.
Other uses (hyphenation, font selection, screen reader voice) rely on engines and assistive technology I did not test. The accessibility benefit is the main one in practice: a screen reader reading French with an English voice is hard to understand. The W3C’s short video in further viewing is aimed at this. The axe run in testing HTML accessibility and the validator both flagged a missing lang.
dir
dir accepts ltr, rtl and auto. It is for languages like Arabic and Hebrew that run right to left:
- With
dir="rtl", a flex row of two spans reversed: the first item sat at x=273 in a 300px box, and the second to its left at 248, where in LTR they were at 0 and 27. text-aligncomputed asstartin both directions, so it follows the direction. Preferstartandendtoleftandright.:dir(rtl)matched the element in the test.dir="auto"read the first strong character: “שלום world” resolved tortl, while “hello שלום” resolved toltr.
That last one is for user-generated content, where you can’t know the direction. The <bdi> element does the same for an inline run, isolating a username from its surroundings so a mixed-direction name doesn’t scramble the punctuation next to it. I used bdi in the test markup but did not measure its layout, so treat the behaviour as documented, not verified here.
For layout, stylesheets should use logical properties (margin-inline-start, padding-inline) so flipping dir flips the design. That is covered in writing modes and logical properties.
Beyond attributes
Translating strings, formatting dates and numbers, plural rules and managing locale files belong to application code. The frontend roadmap’s internationalization and localization lesson covers that side. HTML’s part is small but not optional: declare the language, set the direction, and let the browser do the rest.
What the video covers
Elika J. Etemad’s “i18n: A Brief Primer of Web Internationalization” at dotCSS 2018 (about 17 minutes). The description says she does a rapid review of internationalization and localization, the web platform features that help, and how to use them. The talk is from 2018 and from a CSS conference, so it leans on CSS, and newer features like :dir() may not appear. I could not read a transcript. The test results above are mine, not from the video, and W3C’s three-minute lang video is the runner-up.
Where this fits
This follows HTML security and builds on the anatomy of a document, where lang first appears. HTML validation and debugging is next.
Check yourself
Where should the `lang` attribute go, and what do you do for a French quote on an English page?
On `<html lang="en">` for the page, and `<q lang="fr">…</q>` or similar on the inline French. This lets screen readers switch pronunciation and lets CSS use `:lang(fr)`.
Why does uppercasing "istanbul" give different results by language?
Turkish has a dotted capital İ. In a test, `text-transform: uppercase` produced "İSTANBUL" for `lang="tr"` and "ISTANBUL" for English. The browser applies language-specific case rules based on `lang`.
A forum shows usernames in a list. One is Arabic, and the surrounding text is English. What attribute protects the layout?
Wrap the unknown text in `<bdi>`, or use `dir="auto"`. Either lets the browser pick the direction from its content instead of inheriting the surrounding direction, so punctuation and numbers don't jump to the wrong end.
You write `margin-left` in a stylesheet for a layout that must work in Arabic. What breaks?
The spacing stays on the physical left when `dir="rtl"` flips the content. Use logical properties like `margin-inline-start` so the layout follows the direction, as covered in the CSS writing-modes lesson.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.