Loading...

HTML essentialsAdvanced

Internationalization: lang, dir & encoding

5 min lesson

Next
  • Internationalization (i18n) makes a page work well for different languages, scripts and reading directions.
  • The lang attribute declares the language of the page or a piece of text, like lang="fr" for French.
  • The dir attribute sets text direction: ltr for left-to-right and rtl for right-to-left languages like Arabic or Hebrew.
  • UTF-8 is the character encoding that can represent almost every character in the world.
  • This builds on Responsive design: i18n also adapts content, but for language and direction, not screen size.
  • The example marks the page as Arabic with rtl direction and one English phrase inside; you will see text flow right-to-left.
  • Next, Best practices & validation brings everything together before you ship.
Keep in mind
  • Always set lang on html; it helps screen readers pronounce words correctly.
  • Use dir="rtl" for right-to-left languages, and wrap LTR quotes in a span with dir="ltr".
  • Use UTF-8 everywhere: in the meta charset and in the saved file.
  • Do not translate text inside images; use real text so it can be translated.
index.html
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Internationalization</title>
</head>
<body>
  <!-- The page is Arabic, so text flows right to left -->
  <h1>مرحبا بالعالم</h1>
  <!-- An English quote inside Arabic text: switch direction back -->
  <p>هذه فقرة عربية، وتحتوي على الاقتباس <span lang="en" dir="ltr">Hello</span>.</p>
</body>
</html>

What happens: A page whose text flows from right to left, showing the Arabic heading "مرحبا بالعالم" and an Arabic paragraph containing the English word "Hello" rendered left-to-right.

Breaking it down

lang on html

<html lang="ar" dir="rtl">

Declares the whole page as Arabic. Screen readers use it to choose the right pronunciation.

dir=rtl

dir="rtl"

Makes text and layout flow right-to-left. Without it, Arabic text appears in the wrong visual order.

inline lang switch

<span lang="en" dir="ltr">Hello</span>

Marks a word as English inside Arabic content. The span switches back to left-to-right for that word.

charset UTF-8

<meta charset="UTF-8">

Tells the browser which encoding to use. Without it, accented letters and non-Latin scripts can appear as garbage.

Language attributes

AttributeValuesEffect
langen, ar, fr, ...The language of the text
dirltr or rtlThe text direction
charsetUTF-8How characters are encoded
span langany codeA language switch inside text

A helpful analogy

Setting lang and dir is like telling a reader 'this book is in French and opens from the right.' The reader knows how to pronounce the words and which way to turn the pages.

Nesting rules

  • 1.Set lang and dir on html, and override them on small spans when the language changes.
  • 2.Always save your file as UTF-8; the meta tag only works if the file matches.

Common mistakes

Leaving lang as en on a page in another language

Avoid

<html lang="en"> with Arabic text

Use instead

<html lang="ar">

Screen readers pronounce the text with English rules, so Arabic words sound wrong or are skipped.

Saving the file in an old encoding

Avoid

Saving as Windows-1252 while meta says UTF-8

Use instead

Save as UTF-8 in your editor

Mismatched encodings turn special characters into symbols like é. Always save and declare UTF-8.

Quick tips

  • •Check your editor's status bar; it usually shows the current file encoding.
  • •Test the page with a screen reader set to the page's language; the pronunciation should sound natural.
Previous
Next