xPulse
πŸ‡¬πŸ‡§ EN

API Reference

`i18n.init()`

Called automatically by the service loader. No manual call needed.


`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:

  1. req.lang β€” set by route middleware (URL prefix /en/...)
  2. Accept-Language header β€” first supported language
  3. i18n.default from xpulse.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:

  1. Entry in the requested language
  2. Entry in the default language (i18n.default)
  3. 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:

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)
en/api.md 2026-04-22