---
title: "react-intl"
description: "The preset for react-intl (FormatJS). It reads formatMessage, FormattedMessage, defineMessage and defineMessages."
canonical_url: "https://x18n.dev/presets/react-intl"
---
# react-intl

> The preset for react-intl (FormatJS). It reads formatMessage, FormattedMessage, defineMessage and defineMessages.

The `react-intl` preset supports [react-intl](https://formatjs.github.io/docs/react-intl) and the FormatJS packages.

## Detection

The preset applies when the project depends on `react-intl`, or on any `@formatjs/*` package.

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

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

## Supported syntax

react-intl puts the key in the `id` of a message descriptor:

```tsx
import { defineMessages, FormattedMessage, useIntl } from 'react-intl'

const messages = defineMessages({
  title: { id: 'app.title', defaultMessage: 'My App' },
})

function Header() {
  const intl = useIntl()
  return (
    <header>
      <h1>{intl.formatMessage(messages.title)}</h1>
      <p>{intl.formatMessage({ id: 'app.greeting' }, { name: 'Ada' })}</p>
      <FormattedMessage id="nav.home" />
    </header>
  )
}
```

- `intl.formatMessage`, `formatMessage` and `defineMessage` read `id` from their first argument.
- `<FormattedMessage id="...">` is a usage of its `id`.
- `defineMessages` reads the `id` of each property.

## Placeholders

ICU MessageFormat, as for [next-intl](https://x18n.dev/presets/next-intl#placeholders).

## Extraction templates

| Kinds                      | Template                                             | Imports                                         |
| -------------------------- | ---------------------------------------------------- | ----------------------------------------------- |
| `jsx-text`                 | `<FormattedMessage id="{key}" />`                    | `import { FormattedMessage } from 'react-intl'` |
| `jsx-attribute`            | `{attr}={intl.formatMessage({ id: '{key}' }{args})}` |                                                 |
| `js-string`, `js-template` | `intl.formatMessage({ id: '{key}' }{args})`          |                                                 |


## Sitemap

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