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
<!DOCTYPE html> <head> </head></html>Routing
URL schemes
/fr/page |
path |
fr.domain.com/page |
subdomain |
domain.fr/page |
domain |
Server
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
MDN Web Docs — language property languages property
Dictionaries
Layout
- src
- locales
- index
- en.yml
- fr.yml
- es.yml
- pages
- index.template.html
Translation script
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.
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.
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
<body> <h1>{tr`title`}</h1> <p>{tr`description`}</p></body>title: "Zombiecraft"description: "Survive the zombie apocalypse with exclusive guns, unique upgrades, and immersive maps in this Minecraft: Java Edition server!"cta: "Play"<body> <h1>Zombiecraft</h1> <p>Survive the zombie apocalypse with exclusive guns, unique upgrades, and immersive maps in this Minecraft: Java Edition server!</p></body>Language Selector
<summary aria-label="Select Language">English</summary> </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) |
Decimal
}) // rounding // rounding fraction digits // rounding significant digits // advanced rounding settings})Currency
})})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}` ',Error Invalid argument})- 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}` -
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}` ',Error Invalid argument})- 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}` -
Date & Time
})Relative Time
})})Duration
}) // date // time // subsecond})List
})})Ordinal
type: 'ordinal'})for(var i=1; i<=5; i++) { console.log(formatter.formatOrdinal(i))}Plural
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
Writing modes
| left to right | English, French, … |
| right to left | Arabic, Hebrew, … |
| top to bottom | Chinese, Vietnamese, Korean, & Japanese |
element {}MDN Web Docs — CSS writing modes
HTML Internationalization
Global attributes
<!DOCTYPE html> <body> </body></html>