Localization

Supplying alternative versions of a page in different locales is a form of SEO that allows for a global audience.

t9n Translation Text
l10n Localization Icons · Currency · Time · Alignment
i18n Internationalization Dictionaries · Formatters · Timestamps · Language
g11n Globalization Payment · Laws · Agents

MDN Web Docs — Localization Internationalization

Document Language

index.html
<!DOCTYPE html>
<html lang="en">

The language of the page should be specified here

  • Must be in en or en-US format
<head>
<link rel="alternate" hreflang="fr" href="/fr/home"/>
<link rel="alternate" hreflang="es" href="/es/home"/>
...
 

Translated page variants

</head>
</html>

Routing

URL schemes

/fr/page path
fr.domain.com/page subdomain
domain.fr/page domain

Server

server.js
const { language, languages } = parse(request.headers.get('Accept-Language'))

The user's prefered language is sent to the server through the HTTP Header Accept-Language

  • The format of the header is en;q=1, fr;q=0.9, *;q=0.5
  • Node does not have a built-in parser for this header — use negotiator

MDN Web Docs — Accept-Language header Content-Language header

Client

client.js
const { language, languages } = navigator

The user's prefered language can be retrieved from the Navigator API

MDN Web Docs — language property languages property

Dictionaries

Layout

  • src
    • locales
      • index
        • en.yml
        • fr.yml
        • es.yml
    • pages
      • index.template.html

Translation script

translate.ts
const
const dictionary: any
dictionary
= await import(`@/locales/${
const path: string
path
}/${
const language: string
language
}.yml`)

Load the appropriate dictionary

  • path — the path of the document
  • language — the locale code
function
function tr(chunks: TemplateStringsArray, ...args: any[]): any
tr
(
chunks: TemplateStringsArray
chunks
:
interface TemplateStringsArray
TemplateStringsArray
, ...
args: any[]
args
: any[]) {
const
const key: string
key
=
var String: StringConstructor

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
.
StringConstructor.raw(template: {
raw: readonly string[] | ArrayLike<string>;
}, ...substitutions: any[]): string

String.raw is usually used as a tag function of a Tagged Template String. When called as such, the first argument will be a well formed template call site object and the rest parameter will contain the substitution values. It can also be called directly, for example, to interleave strings and values from your own tag function, and in this case the only thing it needs from the first argument is the raw property.

@param ― template A well-formed template string call site representation.

@param ― substitutions A set of substitution values.

raw
(
chunks: TemplateStringsArray
chunks
, ...
args: any[]
args
)
const
const value: string | undefined
value
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.get<string>(obj: {
[s: string]: string;
}, path: string): string | undefined
get
<string>(
const dictionary: any
dictionary
,
const key: string
key
)
if(
const value: string | undefined
value
== null) {
var console: Console
console
.
Console.error(...data: any[]): void

The console.error() static method outputs a message to the console at the 'error' log level.

MDN Reference

error
('Missing translation for key',
const key: string
key
)
return
function fallback(key: string): any
fallback
(
const key: string
key
)

Render a fallback component in case of a missing key or throw

}
return
function parse(key: string): any
parse
(
const value: string
value
)
 

Potentially parse the value to allow markdown or custom components

}

Sample usage

index.template.html
<body>
<h1>{tr`title`}</h1>
<p>{tr`description`}</p>
<a href='https://zombiecraft.net'>{tr`cta`}</a>
</body>

Language Selector

language-switcher.html
<details class="language-dropdown">
<summary aria-label="Select Language">English</summary>
<ul>
<li><a href="/en/" hreflang="en" lang="en">English</a></li>
<li><a href="/fr/" hreflang="fr" lang="fr">Français</a></li>
<li><a href="/es/" hreflang="es" lang="es">Español</a></li>
</ul>
</details>

JS Internationalization

Number new Intl.NumberFormat('en', options).format(number)
Date & Time new Intl.DateTimeFormat('en', options).format(date)
Relative Time new Intl.RelativeTimeFormat('en', options).format(number, unit)
Duration new Intl.DurationFormat('en', options).format(duration)
List new Intl.ListFormat('en', options).format(list)
Ordinal new Intl.PluralRules('en', options).select(number)
Plural new Intl.PluralRules('en', options).select(number)

MDN Web Docs — Intl

Decimal

decimal-format.jsbasic
const formatter = new Intl.NumberFormat('en', {
style: 'decimal',
notation: 'standard'
})
console.log(formatter.format(10_000))
Step 1 of 2
decimal-format.jsadvanced
const formatter = new Intl.NumberFormat('en', {
style: 'decimal',
notation: 'standard',
compactDisplay: 'short',
signDisplay: 'auto',
Error: minimumIntegerDigits: 1, Must be between 1 .. 21
// rounding
roundingMode: 'halfExpand',
roundingIncrement: 1,
// rounding fraction digits
Error: minimumFractionDigits: 0, Must be between 0 .. 100
Error: maximumFractionDigits: 3, Must be between 0 .. 100
// rounding significant digits
Error: minimumSignificantDigits: 1, Must be between 1 .. 21
Error: maximumSignificantDigits: 21, Must be between 1 .. 21
// advanced rounding settings
roundingPriority: 'auto',
trailingZeroDisplay: 'auto'
})
console.log(formatter.format(10))
console.log(formatter.formatRange(0, 10))
console.log(formatter.formatToParts(10))
console.log(formatter.formatRangeToParts(0, 10))
Step 2 of 2

Currency

currency-format.js
const formatter = new Intl.NumberFormat('en', {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol'
})
console.log(formatter.format(10_000))
Step 1 of 2
currency-format.js
const formatter = new Intl.NumberFormat('en', {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol'
})
console.log(formatter.format(10_000))
console.log(formatter.formatRange(5_000, 10_000))
console.log(formatter.formatToParts(10_000))
console.log(formatter.formatRangeToParts(5_000, 10_000))
Step 2 of 2

Unit

unit-format.js
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
Error: unit: 'megabyte (Info: Can be any of: "bit" "kilobit" "megabit" "gigabit" "terabit" "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte" "day" "week" "month" "year" "hour" "minute" "second" "millisecond" "microsecond" "nanosecond" "kilometer" "meter" "centimeter" "millimeter" "inch" "foot" "yard" "mile" · "acre" "hectare" "gram" "kilogram" · "liter" "milliliter" "pound" "ounce" "gallon" "celsius" "fahrenheit" · "degree" "percent" "stone" "fluid-ounce" "mile-scandinavian" Can also be a compound unit `${unit}-per-${unit}`)InfoCan be any of:
  • "bit" "kilobit" "megabit" "gigabit" "terabit"
  • "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte"
  • "day" "week" "month" "year"
  • "hour" "minute" "second" "millisecond" "microsecond" "nanosecond"
  • "kilometer" "meter" "centimeter" "millimeter"
  • "inch" "foot" "yard" "mile" · "acre" "hectare"
  • "gram" "kilogram" · "liter" "milliliter"
  • "pound" "ounce" "gallon"
  • "celsius" "fahrenheit" · "degree" "percent"
  • "stone" "fluid-ounce" "mile-scandinavian"
Can also be a compound unit `${unit}-per-${unit}`
', Invalid argument
unitDisplay: 'short'
})
console.log(formatter.format(10))
  • Line 3, info: Can be any of:
    • "bit" "kilobit" "megabit" "gigabit" "terabit"
    • "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte"
    • "day" "week" "month" "year"
    • "hour" "minute" "second" "millisecond" "microsecond" "nanosecond"
    • "kilometer" "meter" "centimeter" "millimeter"
    • "inch" "foot" "yard" "mile" · "acre" "hectare"
    • "gram" "kilogram" · "liter" "milliliter"
    • "pound" "ounce" "gallon"
    • "celsius" "fahrenheit" · "degree" "percent"
    • "stone" "fluid-ounce" "mile-scandinavian"
    Can also be a compound unit `${unit}-per-${unit}`
Step 1 of 2
unit-format.js
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
Error: unit: 'megabyte (Info: Can be any of: "bit" "kilobit" "megabit" "gigabit" "terabit" "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte" "day" "week" "month" "year" "hour" "minute" "second" "millisecond" "microsecond" "nanosecond" "kilometer" "meter" "centimeter" "millimeter" "inch" "foot" "yard" "mile" · "acre" "hectare" "gram" "kilogram" · "liter" "milliliter" "pound" "ounce" "gallon" "celsius" "fahrenheit" · "degree" "percent" "stone" "fluid-ounce" "mile-scandinavian" Can also be a compound unit `${unit}-per-${unit}`)InfoCan be any of:
  • "bit" "kilobit" "megabit" "gigabit" "terabit"
  • "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte"
  • "day" "week" "month" "year"
  • "hour" "minute" "second" "millisecond" "microsecond" "nanosecond"
  • "kilometer" "meter" "centimeter" "millimeter"
  • "inch" "foot" "yard" "mile" · "acre" "hectare"
  • "gram" "kilogram" · "liter" "milliliter"
  • "pound" "ounce" "gallon"
  • "celsius" "fahrenheit" · "degree" "percent"
  • "stone" "fluid-ounce" "mile-scandinavian"
Can also be a compound unit `${unit}-per-${unit}`
', Invalid argument
unitDisplay: 'short'
})
console.log(formatter.format(10))
console.log(formatter.formatRange(5, 10))
console.log(formatter.formatToParts(10))
console.log(formatter.formatRangeToParts(5, 10))
  • Line 3, info: Can be any of:
    • "bit" "kilobit" "megabit" "gigabit" "terabit"
    • "byte" "kilobyte" "megabyte" "gigabyte" "terabyte" "petabyte"
    • "day" "week" "month" "year"
    • "hour" "minute" "second" "millisecond" "microsecond" "nanosecond"
    • "kilometer" "meter" "centimeter" "millimeter"
    • "inch" "foot" "yard" "mile" · "acre" "hectare"
    • "gram" "kilogram" · "liter" "milliliter"
    • "pound" "ounce" "gallon"
    • "celsius" "fahrenheit" · "degree" "percent"
    • "stone" "fluid-ounce" "mile-scandinavian"
    Can also be a compound unit `${unit}-per-${unit}`
Step 2 of 2

Date & Time

date-time-format.jsbasic
const formatter = new Intl.DateTimeFormat('en', {
dateStyle: 'medium',
timeStyle: 'medium'
})
console.log(formatter.format(new Date('2026-11-13')))
Step 1 of 2
date-time-format.jsadvanced
const formatter = new Intl.DateTimeFormat('en', {
// date
year: 'numeric',
month: 'numeric',
weekday: 'short',
day: 'numeric',
//time
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
// extra
fractionalSecondDigits: undefined,
timeZoneName: undefined
})
const date = new Date('2026-11-13')
const now = new Date()
console.log(formatter.format(date))
console.log(formatter.formatRange(now, date))
console.log(formatter.formatToParts(date))
console.log(formatter.formatRangeToParts(now, date))
Step 2 of 2

Relative Time

relative-time-format.jsbasic
const formatter = new Intl.RelativeTimeFormat('en', {
style: 'long',
numeric: 'always'
})
console.log(formatter.format(-1, 'day'))
Step 1 of 2
relative-time-format.jsadvanced
const formatter = new Intl.RelativeTimeFormat('en', {
style: 'long',
numeric: 'always'
})
const value = -1
const unit = 'day'
console.log(formatter.format(value, unit))
console.log(formatter.formatToParts(value, unit))
Step 2 of 2

Duration

duration-format.jsbasic
const formatter = new Intl.DurationFormat('en', {
style: 'short'
})
console.log(formatter.format(
Temporal.Duration.from({
// date
years: 1,
months: 2,
weeks: 3,
days: 4,
// time
hours: 5,
minutes: 6,
seconds: 7,
// subsecond
milliseconds: 123,
microseconds: 456,
nanoseconds: 789
})
))
Step 1 of 2
duration-format.jsadvanced
const formatter = new Intl.DurationFormat('en', {
// date
years: 'short',
months: 'short',
weeks: 'short',
days: 'short',
// time
hours: 'numeric',
minutes: 'numeric',
seconds: 'numeric',
// subsecond
milliseconds: 'numeric',
microseconds: 'numeric',
nanoseconds: 'numeric',
// display
yearsDisplay: 'auto',
monthsDisplay: 'auto',
weeksDisplay: 'auto',
daysDisplay: 'auto',
hoursDisplay: 'auto',
minutesDisplay: 'auto',
secondsDisplay: 'auto',
millisecondsDisplay: 'auto',
microsecondsDisplay: 'auto',
nanosecondsDisplay: 'auto'
})
const duration = Temporal.Duration.from({
// date
years: 1,
months: 2,
weeks: 3,
days: 4,
// time
hours: 5,
minutes: 6,
seconds: 7,
// subsecond
milliseconds: 123,
microseconds: 456,
nanoseconds: 789
})
console.log(formatter.format(duration))
console.log(formatter.formatToParts(duration))
Step 2 of 2

List

list-format.jsbasic
const formatter = new Intl.ListFormat('en', {
type: 'conjunction',
style: 'long'
})
console.log(formatter.format([
'Portal',
'Half Life',
'Death Stranding'
]))
Step 1 of 2
list-format.jsadvanced
const formatter = new Intl.ListFormat('en', {
type: 'conjunction',
style: 'long'
})
const list = ['Portal', 'Half Life', 'Death Stranding']
console.log(formatter.format(list))
console.log(formatter.formatToParts(list))
Step 2 of 2

Ordinal

ordinal-format.js
const formatter = new Intl.PluralRules('en', {
type: 'ordinal'
})
Intl.PluralRules.prototype.formatOrdinal = function(value) {
const suffixes = {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th'
}
const pluralRule = this.select(value)
if(!Object.hasOwn(suffixes, pluralRule))
throw new Error(`Plural rule "${pluralRule}" definition is missing`)
const suffix = suffixes[pluralRule]
return value + suffix
}
for(var i=1; i<=5; i++) {
console.log(formatter.formatOrdinal(i))
}

Plural

plural-format.js
const formatter = new Intl.PluralRules('en')
Intl.PluralRules.prototype.format = function(value, singular, plural = `${singular}s`) {
const texts = {
one: singular,
other: plural
}
const pluralRule = this.select(value)
if(!Object.hasOwn(texts, pluralRule))
throw new Error(`Plural rule "${pluralRule}" definition is missing`)
const text = texts[pluralRule]
return text
}
console.log(formatter.format(10, 'thing'))

CSS Internationalization

Logical properties

inline horizontal · x · width
block vertical · y · height
start left · top
end right · bottom

MDN Web Docs — CSS logical properties and values

Inline properties

Logical property Physical properties
margin-inline margin-left · margin-right
padding-inline padding-left · padding-right
border-inline border-left · border-right
inset-inline left · right
inline-size width
min-inline-size min-width
max-inline-size max-width
overflow-inline overflow-x

Block properties

Logical property Physical properties
margin-block margin-top · margin-bottom
padding-block padding-top · padding-bottom
border-block border-top · border-bottom
inset-block top · bottom
block-size height
min-block-size min-height
max-block-size max-height
overflow-block overflow-y

Flow values

Logical value Physical value
start left · top
end right · bottom
inline-start left
inline-end end
block-start top
block-end bottom

MDN Web Docs — CSS Flow relative values

Language selector

style.css
:lang(en) { ... }
:lang(fr) { ... }
:lang(es) { ... }

Matches the HTML <element lang=""> attribute

Writing modes

left to right English, French, …
right to left Arabic, Hebrew, …
top to bottom Chinese, Vietnamese, Korean, & Japanese
style.css
element {
direction: ltr;
writing-mode: horizontal-tb;
text-orientation: mixed;
text-combine-upright: none;
unicode-bidi: normal;
}
This section is currently undocumented
Let @Stephcraft know on Discord you'd like this section Documented
Join Discord

MDN Web Docs — CSS writing modes

HTML Internationalization

Global attributes

index.html
<!DOCTYPE html>
<html lang="en" dir="auto" translate="yes">

Localization attributes

  • lang The language of the page should be specified here
    • Must be in en or en-US format
  • dir The horizontal text direction
  • translate Whether auto translation should be enabled
<body>
<p lang="fr" dir="auto" translate="yes">Bonjour!</p>

These localization attributes can be applied to any element

</body>
</html>

Bidirectional text elements

<bdi> <bdo> bidirectional · Arabic, Hebrew, …
This section is currently undocumented
Let @Stephcraft know on Discord you'd like this section Documented
Join Discord

Ruby annotation elements

<ruby> <rp> <rt> ruby notation · Japanese, Chinese & Korean
This section is currently undocumented
Let @Stephcraft know on Discord you'd like this section Documented
Join Discord

Resources

Frameworks

Astro Docs — Internationalization
Next.js Docs — Internationalization
TanStack Router Docs — Internationalization