gravity-ui-i18n

The preset for @gravity-ui/i18n. It reads i18n.i18n(keyset, key) calls, keyset functions and keyset folders.

The gravity-ui-i18n preset supports @gravity-ui/i18n.

Detection

The preset applies when the project depends on @gravity-ui/i18n.

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

export default defineConfig({
  presets: ['gravity-ui-i18n'],
})

Locale files

If your config has no resources, the preset uses these patterns:

  • src/i18n-keysets/{namespaces}/{locale}.json: one folder per keyset. The folder page.home holds the keyset page.home.
  • src/**/{namespace}/i18n/{locale}.json: an i18n folder next to a component. The component folder names the keyset, so src/components/Header/i18n/en.json holds the keyset Header.

If neither pattern matches a file, x18n guesses the locale directories. A file such as src/i18n/en.json can then hold every keyset as a top-level object.

Keysets

A keyset is the first part of the key. i18n.i18n('page.home', 'title') is a usage of page.home.title, both in src/i18n-keysets/page.home/en.json and in one en.json with { "page.home": { "title": "..." } }.

A plural key is an object with the CLDR forms zero, one, two, few, many and other. A usage of the key counts as a usage of each form.

Supported syntax

src/App.tsx
import { I18N } from '@gravity-ui/i18n'

const i18n = new I18N()

i18n.i18n('common', 'hello') // common.hello

const keyset = i18n.keyset('wizard')
keyset('label_no-access') // wizard.label_no-access

const i18nHome = i18n.bind(null, 'page.home')
i18nHome('title', { name }) // page.home.title
  • i18n.i18n(keyset, key) and i18nInstance.i18n(keyset, key) are translate calls.
  • i18n.keyset(keyset) and i18nInstance.keyset(keyset) return a translate function for the keyset.
  • i18n.bind(null, keyset) and i18nInstance.i18n.bind(i18nInstance, keyset) do the same.

A bare i18n(keyset, key) is not read, because projects also name a keyset function i18n, and then i18n('key', 'x') takes no keyset. A keyset that is no string literal, such as i18n.i18n(getKeyset(), 'title'), makes the usage dynamic.

For an instance with another name, add its callees to usage.calls. A keyset function that a module exports, such as export default i18n.keyset('Button'), is not followed into the modules that import it.

Placeholders

Double braces: {{name}}.

Extraction templates

The preset adds none, as an extracted key needs its keyset as a separate argument. Set extract.templates for your keyset functions.