lingui

The preset for Lingui. It reads .po catalogs, the t macro, Trans, msg, defineMessage and useLingui.

The lingui preset supports Lingui.

Detection

The preset applies when the project depends on @lingui/core, @lingui/react or @lingui/macro.

x18n.config.ts
import { lingui } from '@x18n/cli/presets'
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: [lingui()],
})

Locale files

If your config has no resources, the preset uses src/locales/{locale}/messages.po and locales/{locale}/messages.po. The po format reads and writes these gettext catalogs. Each msgid is a key, and its msgstr is the value:

src/locales/en/messages.po
msgid "Hello"
msgstr "Hello"

msgid "welcome"
msgstr "Welcome"

Supported syntax

Lingui keys are often the source message itself. The macros and calls below are usages of Hello and welcome:

src/a.tsx
import { t } from '@lingui/core/macro'
import { Trans } from '@lingui/react/macro'

const a = t`Hello`
const b = <Trans id="welcome" />
const c = <Trans>Hello</Trans>
  • The tagged template t`...` and the text of <Trans>...</Trans> are usages.
  • i18n._, i18n.t, t and _ are translate calls with the key as the first argument.
  • t, msg, defineMessage, i18n._ and <Trans> also take the key from an id property.
  • useLingui() returns t and _.

Keys for extracted strings

The preset sets extract.keygen to source, so x18n extract uses the text itself as the key. Set extract.keygen in the config to change it.

Placeholders

ICU MessageFormat, as for next-intl.

Extraction templates

KindsTemplateImports
jsx-text<Trans>{key}</Trans>import { Trans } from '@lingui/react/macro'
js-string, js-templatet`{key}`import { t } from '@lingui/core/macro'