@gravity-ui/i18n-cli
Project Setup
-
Install the package:
npm i -D @gravity-ui/i18n-cli
-
Createthe config file i18n.config.ts in the project root:
import {defineConfig} from '@gravity-ui/i18n-cli/config';
export default defineConfig({
allowedLocales: ['ru', 'en', 'ru-kz', 'en-kz'] as const,
fallbackLocales: {
'ru-kz': 'ru',
'en-kz': 'en',
},
defaultFallback: 'en',
clientIntlModule: {
path: 'src/shared/i18n/intl.ts',
alias: '@shared/i18n',
},
});
-
Import the config in the i18n library instance (i18n-react and i18n-node)
import {createIntl} from '@gravity-ui/i18n-react';
import i18nConfig from '../../../i18n.config';
const {allowedLocales, fallbackLocales, defaultFallback} = i18nConfig;
export const intl = createIntl({
allowedLocales,
fallbackLocales,
defaultFallback,
});
Commands
create-keys
Checks the provided file/directory for calls to missing keys and adds them to the i18n.ts file.
Run for a single file
npx i18n-cli create-keys src/ui/SomeComponent/SomeComponent.tsx
Run for all files in a directory
npx i18n-cli create-keys src/ui/ComponentDirectory
find-unused
Recursively search for unused keys starting from the specified directory.
Report on unused keys
npx i18n-cli find-unused src/ui/SomeComponent
Delete unused keys
npx i18n-cli find-unused -d src/ui/SomeComponent
Configuration options
allowedLocales
Type: string[]
Allowed locales in the project.
{
allowedLocales: ['ru', 'en'] as const,
}
fallbackLocales
Fallbacks for given locales. More details can be found in i18n-core.
defaultFallback
Default fallback. Used if no higher priority fallback is found. More in i18n-core.
clientIntlModule
Location of the library instance for the client-side i18n-react in the project.
{
clientIntlModule: {
path: 'src/ui/shared/i18n.ts',
alias: '@shared/i18n';
}
}
serverIntlModule
Server-side library instance location i18n-node in the project.
{
serverIntlModule: {
path: 'src/server/utils/i18n.ts',
alias: undefined,
pathMatchers: [/src\/server\/.+$/]
}
}