---
title: "react-i18next"
description: "The preset for react-i18next and next-i18next. It reads useTranslation, Trans and Translation, plus everything of i18next."
canonical_url: "https://x18n.dev/presets/react-i18next"
---
# react-i18next

> The preset for react-i18next and next-i18next. It reads useTranslation, Trans and Translation, plus everything of i18next.

The `react-i18next` preset supports [react-i18next](https://react.i18next.com) and next-i18next.

## Detection

The preset applies when the project depends on `react-i18next` or `next-i18next`. x18n still tests the presets after it, such as `i18next` and `general`.

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

export default defineConfig({
  presets: ['react-i18next'],
})
```

## What it adds

The preset includes everything of the [`i18next`](https://x18n.dev/presets/i18next) preset: the `:` namespace separator, the default namespace `translation`, the derived keys `{key}_*`, the placeholders and the calls of i18next. If the `i18next` preset also applies, x18n adds these plugins one time.

## Supported syntax

```tsx
import { Trans, Translation, useTranslation } from 'react-i18next'

function Page() {
  const { t } = useTranslation('common', { keyPrefix: 'buttons' })
  return (
    <>
      <button>{t('save')}</button>
      <Trans i18nKey="welcome" ns="pages" />
      <Translation ns="pages">{t => <h1>{t('title')}</h1>}</Translation>
    </>
  )
}
```

- `useTranslation(ns, { keyPrefix })` returns a `t` with a namespace and a key prefix. `t('save')` above is `common:buttons.save`.
- `<Trans i18nKey="..." ns="...">` is a usage of its `i18nKey`.
- `<Translation ns="..." keyPrefix="...">` passes a `t` with the namespace and key prefix to its child function.
- `const { i18n } = useTranslation()` makes `i18n.t` a translate call.
- `withTranslation('ns', { keyPrefix })(Component)` sets the namespace and key prefix for the whole file, because the component gets its `t` through props and is often above the call.

A custom hook that wraps `useTranslation` is a call spec with a fixed namespace:

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

// export const useSharedTranslation = () => useTranslation('shared')
export default defineConfig({
  usage: {
    calls: [
      { callee: 'useSharedTranslation', factory: true, namespace: 'shared' }
    ]
  },
})
```

## Placeholders

The same as for [i18next](https://x18n.dev/presets/i18next#placeholders): `{{name}}` and `$t(key)`.

## Extraction templates

| Kinds                      | Template                            | Imports                                 |
| -------------------------- | ----------------------------------- | --------------------------------------- |
| `jsx-text`                 | `<Trans i18nKey="{key}" />`         | `import { Trans } from 'react-i18next'` |
| `jsx-attribute`            | `{attr}={i18next.t('{key}'{args})}` | `import i18next from 'i18next'`         |
| `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.
