@matchlighter/nodal_i18n
An fast I18n library with advanced interpolation and inheritance capabilities.
Language Files
Language data is expressed as a plain Javascript object, and
can thus be transmitted to your frontend via JSON or can be imported from JSON or YAML files using tools such as Webpack.
Documentation of the language file format will primarily be written in YAML for its conciseness and better human-readability vs JSON.
Example Language file:
title: Simple Application
base_tab:
tooltip: $t(`${path[0:-1]}.title`)
header_tabs:
home:
title: Home
tooltip: Return to "$t(`title`)" Home page
about:
title: About Us
pricing.title: Pricing
$base:
tooltip: $t(`header_tabs.${path[1]}.title`)
$base: $t(`base_tab`)
contact:
$extends: $t(`base_tab`)
$extends:
- $t(`base_tab`)
title: Contact Us
$missing:
title: $path[-2]
$missing: $base
Interpolations
The interpolations syntax implements a small-but-powerful Domain-Specific-Language that can make your language files quite dynamic.
Values in the language file/data are interpreted as plain strings until a $
character is encountered.
Examples:
- Simple Interpolation
hello.planet: Hello $planet
t('hello.planet', { planet: 'Earth' })
t('hello.planet', { planet: 'Mars' })
- Lookup Interpolation
planets:
mars: Mars
pluto: Pluto (Not Actually a Planet)
hello:
planet: Hello $t(`planets.$planet`)
t('hello.planet', { planet: 'mars' })
t('hello.planet', { planet: 'pluto' })