Pages
Contents
API Reference
`i18n.init()`
Called automatically by the service loader. No manual call needed.
- Wraps
router.register()β all subsequent route registrations get/:lang/variants - Loads translation files from
i18n.paths.translations - Registers the
http:response:beforelistener (FOUC script, optional SEO tags) - Emits
i18n:initβi18n:ready
`i18n.lang(req)`
Returns the active language for a request. Always set.
| import i18n from '@xpulse/i18n'; |
| const lang = i18n.lang(req); // β 'de' | 'en' |
Priority:
req.langβ set by route middleware (URL prefix/en/...)Accept-Languageheader β first supported languagei18n.defaultfromxpulse.json
`i18n.t(key, lang, vars?)`
Returns the translated string.
| i18n.t('tagline', 'en') |
| // β 'Private Tools. No Server. No Compromises.' |
| i18n.t('status.days', 'en', { n: 3, s: 's' }) |
| // β '3 days remaining' |
| i18n.t('unknown.key', 'en') |
| // β 'unknown.key' (key fallback) |
Fallback order:
- Entry in the requested language
- Entry in the default language (
i18n.default) - The key itself as a string
Interpolation: Variables in translation strings are written as {key} and filled via vars:
| { "status.days": "{n} day{s} remaining" } |
| i18n.t('status.days', 'en', { n: 3, s: 's' }) // β '3 days remaining' |
| i18n.t('status.days', 'en', { n: 1, s: '' }) // β '1 day remaining' |
Typical usage in a route handler:
| import i18n from '@xpulse/i18n'; |
| const lang = i18n.lang(req); |
| const tagline = i18n.t('tagline', lang); |
| const status = i18n.t('status.days', lang, { n: 3, s: 's' }); |
Template methods
Available when @xpulse/template is installed.
`{% langSwitcher(path) %}`
Renders a language switcher with links to the /:lang/ variant of the current page.
Sets localStorage.xpulse_lang on click.
| {% langSwitcher(_route.path) %} |
Parameter: current path as string β _route.path is available in every template.
`{% htmlLang(route) %}`
Returns the active language code β for <html lang="...">.
| <html lang="{% htmlLang(_route) %}"> |
Parameter: _route object or path string.
`{% t(key, route, vars?) %}`
Returns the translated string β usable directly in templates.
| {% t('tagline', _route) %} |
| {% t('status.days', _route, {n: 3, s: 's'}) %} |
| {% t('cta', 'en') %} |
The second parameter accepts:
_routeor path string β language is derived from the path- explicit language code
'de'/'en'
If the key is not found β key string as fallback (no empty output).
Events
| Event | When |
|---|---|
i18n:init |
Init starts, router.register() is wrapped |
i18n:ready |
Translation files loaded, ready |
xpulse.json options
| Key | Default | Description |
|---|---|---|
i18n.supported |
["de", "en"] |
Supported languages |
i18n.default |
"de" |
Default language (no URL prefix route) |
i18n.use-seo |
false |
Inject <link rel="canonical"> + <link rel="alternate" hreflang> into <head> |
i18n.exclude |
["/_theme/*", ...] |
Paths that don't get a /:lang/ variant |
i18n.paths.translations |
"src/translations/" |
Path to locale files |
app.url |
"" |
Base URL for absolute SEO links, e.g. "https://xpulse.one" β empty string produces relative paths (invalid for Google hreflang) |