@gravity-ui/i18n-cli
v1.3.2
Published
- Provides i18n configuration for the project
Downloads
675
Readme
@gravity-ui/i18n-cli
Provides i18n configuration for the project
Provides the
i18n-clicommand for managing language files in the project.
Project Setup
Install the package:
npm i -D @gravity-ui/i18n-cliCreatethe config file
i18n.config.tsin 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.tsxRun for all files in a directory
npx i18n-cli create-keys src/ui/ComponentDirectoryfind-unused
Recursively search for unused keys starting from the specified directory.
Report on unused keys
npx i18n-cli find-unused src/ui/SomeComponentDelete unused keys
npx i18n-cli find-unused -d src/ui/SomeComponentConfiguration 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 to the module with intl library instance
path: 'src/ui/shared/i18n.ts',
// TypeScript alias of the module with intl library instance
alias: '@shared/i18n';
}
}serverIntlModule
Server-side library instance location i18n-node in the project.
{
serverIntlModule: {
// Path to the module containing the intl library instance
path: 'src/server/utils/i18n.ts',
// TypeScript alias of the module containing the intl library instance
alias: undefined,
// Regular expressions that determine if a module belongs to the server-side
pathMatchers: [/src\/server\/.+$/]
}
}