Skip to main content

I18n translation with CLI

Logto translate CLI is a powerful tool designed for developers contributing to Logto's i18n efforts. With this CLI, you can focus on writing content in English, and then easily translate it into all supported languages.

For regular Logto users seeking custom translations without contributing to the project, please refer to our localization guide.

info

Started from v1.20.0, the translation command is removed from the @logto/cli, and maintained as a standalone @logto/translation package in the logto monorepo. Accordingly, the way of execution is slightly different.

Migrate from @logto/cli​

Here's a comparison of the old and new CLI execution methods:

Old method (before v1.20.0):

pnpm cli translate <command> [options]

New method:

pnpm translate <command> [options]

Sync keys​

Let's say we made some changes to the translation keys in the en folder of the phrases package. We want to sync these changes to the pt-BR folder. Run the command below to sync translation keys and file structure:

pnpm translate sync-keys --target pt-BR

This command will read all translation keys from the source language folder (en by default) and sync them to the target language folder (pt-BR in this example).

  • If the target language folder doesn't exist, it will be created automatically;
  • If the target language folder already exists, it will be updated with new keys and removed with obsolete keys.

For each translation key:

  • If a key is missing in the target, it will be added with a comment to indicate that the phrase is untranslated (marked with /** UNTRANSLATED */);
  • If a key is missing in the baseline, it will be removed from the target;
  • If a key exists in both the baseline and the target, the value of the target will be used.

To sync keys for all languages in the phrases package, run the command below:

pnpm translate sync-keys --target all

Thus we can focusing on updating the translation keys in the source language folder, and the translation keys in other languages will be updated automatically.

info

This command executes for phrases package by default. You can specify the package name by --package option. For example, use --package phrases-experience to sync keys for the phrases-experience package.

After syncing keys, we can translate the keys using ChatGPT API.

Translate keys using ChatGPT​

Run the command below to translate keys using ChatGPT:

pnpm translate sync

This command will translate all untranslated keys (marked with /** UNTRANSLATED */) across all languages in the phrases and phrases-experience packages. Note an environment variable OPENAI_API_KEY is required to run this command.