i18next
The i18next preset supports i18next without a framework binding. For React, see react-i18next.
Detection
The preset applies when the project depends on i18next. The react-i18next preset includes this one. If both apply, x18n adds the shared plugins one time.
import { i18next } from '@x18n/cli/presets'
import { defineConfig } from '@x18n/core'
export default defineConfig({
presets: [i18next()],
})Namespaces
The preset sets the namespace separator to : and the default namespace to translation, as i18next does. With a resource pattern that has {namespace}, keys have the form namespace:path:
import { defineConfig } from '@x18n/core'
export default defineConfig({
resources: [{ pattern: 'public/locales/{locale}/{namespace}.json' }],
})A key without a namespace, such as t('title'), is in the translation namespace. Set namespace.default to change it.
With {namespaces}, a namespace can sit in folders. The preset sets namespace.fromPath to slash, so public/locales/en/pages/home.json is the namespace pages/home, as in t('pages/home:title').
Supported syntax
import i18next from 'i18next'
i18next.t('common:save')
i18next.t('save', { ns: 'common' })
t('pages.home.title')
const tc = i18next.getFixedT('de', 'common', 'buttons')
tc('save') // common:buttons.save
function label(t: TFunction) {
return t('app.title')
}
t($ => $.app.title)t,i18next.tandi18n.tare translate calls. Thensoption in the second argument sets the namespace.getFixedT(lng, ns, keyPrefix)returns atwith a namespace and a key prefix.- A parameter or prop with the type
TFunctionis a translate function. - The selector form
t($ => $.a.b)reads the keya.b.
Derived keys
The preset adds the derived key rule {key}_*. When t('item') is used, item_one, item_other and item_male count as used. That covers the plural and context suffixes of i18next.
The translate prompt tells the agent the plural categories of the target locale, for example "i18next plural keys end in _one,_other for de; write one key per category."
Placeholders
- Interpolation:
{{name}},{{name, format}}and{{- raw}}. - Nesting:
$t(key). The nested key counts as used.
Extraction templates
| Kinds | Template | Imports |
|---|---|---|
js-string, js-template | i18next.t('{key}'{args}) | import i18next from 'i18next' |