IntlGen
is a utility that automates the process of translating language files for internationalization (i18n) in your application. It reads a default language file, translates it into multiple target languages, and outputs the translated files into the specified directory structure.
npm install intl-gen
-
Create config file
intl.config.mjs
/** @type {import('intl-gen').Options} */ export const config = { ext: 'json', filename: 'translation', directory: ['locales'], languages: ['en-US', 'id-ID'], baseLanguage: 'en-US', ignoreExists: true, enableSubdirectory: true, override: (code) => `translation_${code}`, }
-
Execute bash command
npx intl-gen -c intl.config.mjs
The Options interface includes the following fields:
- directory (string[]): Array of directories where translation files are stored.
- languages (Language[]): List of languages to translate into, each with a code (ISO 639-1) and title.
- filename (string): Filename of the default language file.
- default_language (string): Language code of the base language to translate from.
- auto_override (boolean, optional): If true, overrides existing translations.
- skip_region (boolean, optional): If true, skips region-based translations.
- exclude (string[], optional): Array of language codes to exclude from translation.
- override_output (ResultCallback, optional): Function to override output filenames.
- locale_directory (boolean, optional): If true, outputs files directly by language directory.
-
Structure Output Directory
project-root/ └── locales/ ├── en/ │ └── translation.json ├── es/ │ └── translation_es.json └── fr/ └── translation_fr.json
-
Default Language File (translation.json)
{ "hello": "Hello", "welcome": "Welcome to our application!" }
-
Translated Output (translation_es.json)
{ "hello": "Hola", "welcome": "¡Bienvenido a nuestra aplicación!" }
note:
It is recommended to check the results again, and correct them manually if they do not match.