gravity-ui-i18n
The gravity-ui-i18n preset supports @gravity-ui/i18n.
Detection
The preset applies when the project depends on @gravity-ui/i18n.
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 folderpage.homeholds the keysetpage.home.src/**/{namespace}/i18n/{locale}.json: ani18nfolder next to a component. The component folder names the keyset, sosrc/components/Header/i18n/en.jsonholds the keysetHeader.
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
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.titlei18n.i18n(keyset, key)andi18nInstance.i18n(keyset, key)are translate calls.i18n.keyset(keyset)andi18nInstance.keyset(keyset)return a translate function for the keyset.i18n.bind(null, keyset)andi18nInstance.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.