---
title: "i18next"
description: "The preset for i18next. It reads namespaces, getFixedT, typed TFunction parameters and plural keys."
canonical_url: "https://x18n.dev/presets/i18next"
---
# i18next

> The preset for i18next. It reads namespaces, getFixedT, typed TFunction parameters and plural keys.

The `i18next` preset supports [i18next](https://www.i18next.com) without a framework binding. For React, see [react-i18next](https://x18n.dev/presets/react-i18next).

## Detection

The preset applies when the project depends on `i18next`. The [`react-i18next`](https://x18n.dev/presets/react-i18next) preset includes this one. If both apply, x18n adds the shared plugins one time.

```ts [x18n.config.ts]
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`:

```ts [x18n.config.ts]
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. The preset reads `defaultNS` and `fallbackNS` from the i18next init code (a file such as `src/i18n.ts`). A key that is not in its own namespace is then looked up in the fallback namespaces, in order. Set `namespace.default` and `namespace.fallback` to override them:

```ts [x18n.config.ts]
import { defineConfig } from '@x18n/core'

export default defineConfig({
  namespace: {
    default: 'app',
    fallback: ['common']
  },
})
```

To use another separator, such as `t('common|save')` with the i18next option `nsSeparator: '|'`, set `namespace.separator`. Completion writes keys with the same separator.

## Keys with dots

With the i18next option `keySeparator: false`, a key such as `t('Welcome back.')` is literal: the `.` does not split it. The preset reads `keySeparator` and `nsSeparator` from the init code and sets `keySeparator: false`, and `namespace.separator: false` for `nsSeparator: false`, so `t('Note: x')` is a key and not the namespace `Note`. A string, such as `keySeparator: '_'`, becomes the key separator. Set `keySeparator` in the config to override 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')`.

## i18next-cli config

When the project root has an [i18next-cli](https://github.com/i18next/i18next-cli) config (`i18next.config.ts`, `.mts`, `.js`, `.mjs` or `.cjs`), the preset reads it. x18n does not run the file: it reads the object of `export default defineConfig({ ... })` (or `module.exports`) and skips values that are not literals, such as `process.env.X` or an `output` function.

| i18next-cli                                                                   | x18n                                                             |
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| `locales`                                                                     | `locales`                                                        |
| `extract.primaryLanguage`, else the first locale                              | `sourceLocale`                                                   |
| `extract.output`, such as `public/locales/{{language}}/{{namespace}}.json`    | a resource pattern, `public/locales/{locale}/{namespaces}.json`  |
| `extract.defaultNS`, `extract.fallbackNS`, `extract.nsSeparator`              | `namespace.default`, `namespace.fallback`, `namespace.separator` |
| `extract.keySeparator`, `extract.nsSeparator: false`                          | `keySeparator`, `namespace.separator: false`                     |
| `extract.functions`, `extract.transComponents`, `extract.useTranslationNames` | more translate calls. Wildcards such as `*.t` are skipped.       |
| `extract.ignore`                                                              | `usage.exclude`                                                  |

These values replace what the preset finds in the init code. The x18n config wins over both.

## Supported syntax

```ts
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')
}

function typeLabel(t: TFunction<'shared'>) {
  return t('type.bug') // shared:type.bug
}

t($ => $.app.title)
```

- `t`, `i18next.t` and `i18n.t` are translate calls. The `ns` option in the second argument sets the namespace.
- `getFixedT(lng, ns, keyPrefix)` returns a `t` with a namespace and a key prefix.
- A parameter or prop with the type `TFunction` is a translate function. Its first type argument is the namespace: `TFunction<'shared'>`, or the first of `TFunction<['shared', 'common']>`. Props work when typed inline (`{ t }: { t: TFunction<'shared'> }`) or with an interface or type of the same file.
- The selector form `t($ => $.a.b)` reads the key `a.b`.
- In Astro components, such as with astro-i18next, the preset reads the calls in the `---` frontmatter, in `<script>` and in `{...}` template expressions.

## Derived keys

The preset adds the derived key rules `{key}_ordinal_{ordinal}`, `{key}_{plural}` and `{key}_*`. When `t('item')` is used, `item_one`, `item_other` and `item_male` count as used, and `t('item')` is not reported missing. Suffixes stack, so context with plural (`item_male_one`) and ordinal plurals (`item_ordinal_one`) also belong to `item`. Hovers and inline values show the `_other` form, or another form when the locale has no `_other`.

Each locale needs the plural categories of its own language, from `Intl.PluralRules`. With `item_one` and `item_other` in English:

- Japanese needs only `item_other`, and `item_one` is not reported missing there.
- Russian needs `item_one`, `item_few`, `item_many` and `item_other`. The `missing-translation` check reports `item_few` and `item_many` at the English `item_other`, and its fix copies that value.
- A form that a language does not have, such as `item_zero` in English, is optional in every locale.

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


## Sitemap

See the full [sitemap](https://x18n.dev/sitemap.md) for all pages.
