Translate

How to translate your project

Usage

Run this in your CI pipeline before you build your app for production.

npx gtx-cli translate

Note: This command requires a production API key. Get one on the platform.

Overview

The gtx-cli translate command translates your project. It traverses your project’s file tree and translates any content wrapped in a <T> component or using the useGT functions.

Additionally, it includes content from the dictionary file (if one is provided).

This command is the primary way of using the General Translation API and related services.

For production use only

This command is intended for production builds and should not be used in development. Before running this command, please ensure you are on the branch that will be used for production. Remember to also specify your production API key (GT_API_KEY) and Project ID (GT_PROJECT_ID) in your environment variables.

Usage

There are three ways to use the translate command. Methods 1 and 2 require a production API key:

We recommend running the setup wizard, npx gtx-cli configure, to help configure your project before running the translate command. Depending on how your project is configured, the behaviour of the translate command may vary.

Method 1: Translate your project's JSON files.

If you are using other i18n libraries such as next-intl, react-i18next, or next-i18next, you can use this method to translate your project's JSON files.

Translations will be saved to your codebase automatically.

To use the CLI tool to translate your project's JSON files, modify your gt.config.json file to include json in the files property. See the CLI config docs for more details.

npx gtx-cli translate

The CLI tool will automatically detect your i18n library by reading your package.json file, and will translate your JSON files while respecting your i18n library's syntax.

Method 2: Translate your GT project.

If your project uses gt-next or gt-react, you can use this method to generate translations for your project.

npx gtx-cli translate

By default, translations are saved to the GT CDN.

However, if you’d like to save translations to your codebase, add the gt property to the files object in the gt.config.json file.

gt-next and gt-react support serving translations locally, as well as using General Translation’s public CDN. We recommend using the CDN for lower latency, better performance, and smaller bundle sizes.

See the CLI config docs for more details.

Method 3: Validate your project's <T> components and dictionary file.

This method is useful for validating your project's <T> components and dictionary file.

This ensures that your project is correctly configured and that the translations will be valid and accurate.

No translations will be generated if the --dry-run flag is provided.

npx gtx-cli translate --dry-run

Flags

ParameterDescriptionTypeOptionalDefault
--api-keySpecify a production API keystringtrue
--project-idSpecify the project IDstringtrue
--version-idSpecify a version ID (by default, a hash of the content)stringtrue
--config <path>Specify a path to the GT config filestringtrue"gt.config.json"
--tsconfig, --jsconfig <path>Specify a path to the TS or JS config filestringtrue
--src <paths>Space-separated list of glob patterns to match source files. Should be relative to the root directory.[string]true[ 'src/**/*.{js,jsx,ts,tsx}', 'app/**/*.{js,jsx,ts,tsx}', 'pages/**/*.{js,jsx,ts,tsx}', 'components/**/*.{js,jsx,ts,tsx}', ]
--dictionary <path>Specify a path to the dictionary filestringtrue
--inlineInclude inline <T> tags in addition to the dictionarybooleantruetrue
--timeoutThe timeout for the translation request in secondsnumbertrue600
--new, --locales <locales>Locales to translate your project into[string]true
--default-locale <locale>The source locale for the projectstringtrueen
--ignore-errorsIgnore errors and force translation for valid contentflagtruefalse
--dry-runRun the command in dry-run modeflagtruefalse
--forceForce a retranslation of your projectflagtruefalse
--force-downloadForce a download of all translations for the projectflagtruefalse

All of these parameters are optional.

Do not add your API key to the gt.config.json file! You should set it as an environment variable instead. The CLI will automatically read GT_API_KEY if it is set.

There are a few key parameters:

ParameterDescription
--dry-runThis flag causes the CLI to parse and validate your project, but it will not communicate with the GT API. This is useful for validating your codebase.
--api-keyUnless you are using --dry-run, you must provide a production API key.
--project-idSimilarly, unless you are using --dry-run, you must provide a project ID.
--new, --locales <locales>Locales to translate your project into. These will be appended to the locales specified in your gt.config.json file.
--forceThis flag forces a retranslation of your project and overwrites all existing translations.
--force-downloadThis flag forces a download of all translations and overwrites any changes you have made to the translations locally.

Configuration file

When you run the CLI tool for the first time, it will attempt to create a gt.config.json file in the root of your project. This file contains metadata about your project that is used to translate your content.

Read more about the gt.config.json file here.

Important tips

Content Sources

The translate command will recursively search for translatable content in your project.

By default, it searches in the following directories:

  • ./src
  • ./app
  • ./pages
  • ./components

You can specify alternative directories to search using the --src flag, or by modifying the src property in your gt.config.json file.

Overwriting translations

By default, the CLI tool won’t overwrite any changes you’ve made to translations locally unless the source content has changed.

If you’d like to retranslate project content that has already been translated, you can use the --force flag.

Using --force will overwrite all existing translations and won’t preserve any changes you’ve made to the translations locally.

If you’ve already fetched the latest project translations and would like to download them again, you can use the --force-download flag.

Using --force-download will overwrite any changes you’ve made to the translations locally and will fetch the latest translations. It won’t retranslate any content.

Dictionary File

The translate command will automatically detect the dictionary file in your project.

By default, it looks for a file named dictionary.[json|ts|js] in the following directories:

  • ./src
  • ./

You can specify an alternative dictionary file by using the --dictionary flag, or by modifying the dictionary property in your gt.config.json file.

How is this guide?

Translate