Usage guide
Usage guide for the GT command-line tool
Overview
This guide will walk you through using General Translation’s CLI tool (gtx-cli) to translate your project.
The CLI tool is compatible with any i18n library, whether you’re using gt-next, gt-react, or third-party libraries like next-intl or react-i18next.
The CLI tool connects your project to General Translation’s AI translation service.
There are several ways to use the CLI tool; please navigate to the relevant section for your use case.
Installation
To install the CLI tool, run the following command:
npm i gtx-cliyarn add --dev gtx-clibun add --dev gtx-clipnpm add --save-dev gtx-cliUse cases
Translating GT projects
The CLI tool can be used to translate your projects that use gt-next or gt-react.
This guide assumes you have already set up your codebase to use gt-next or gt-react according to the gt-next or gt-react tutorials.
- First, you will need to configure your project's GT settings.
npx gtx-cli configureThis command will automatically generate an API key and project ID for your project via the dashboard.
However, if you would like to set the API key and project ID manually, follow steps 2 and 3 below.
- Next, you will need to create a project in the General Translation dashboard.
After creating a project, you will need to generate a production API key. Navigate to the "API Keys" page and click the "Create API Key" button.
- Then add your API key and project ID to your environment variables.
GT_API_KEY=<your-api-key>
GT_PROJECT_ID=<your-project-id>- Finally, run the translation command.
npx gtx-cli translateBy default, the CLI tool will publish the translations to the General Translation CDN, so they are ready to use in your project.
If you would like to disable this behaviour (for example, if you load translations from a different source), turn off the CDN setting in the dashboard project settings.
See the API reference for more information on the translate command.
Translating language files (third‑party i18n libraries or standalone GT projects)
The CLI tool allows you to translate language files for third‑party i18n libraries or with a standalone implementation of gt-next or gt-react.
Most i18n libraries rely on JSON files to store translation data. The CLI tool can be used to automatically translate these JSON files into your target languages.
- First, you will need to configure your project’s GT settings.
npx gtx-cli configureThis command will create a gt.config.json file in the root of your project, containing basic configuration settings, such as your project's default locale and supported locales.
This command will automatically generate an API key and project ID for your project via the dashboard.
However, if you would prefer to set the API key and project ID manually, follow steps 2 and 3 below.
- Next, you need to create a project in the General Translation dashboard.
After creating a project, generate a production API key. Navigate to the “API Keys” page and click the “Create API Key” button.
- Then add your API key and project ID to your environment variables.
GT_API_KEY=<your-api-key>
GT_PROJECT_ID=<your-project-id>- Finally, run the translation command.
npx gtx-cli translateBy default, the CLI tool will look for files to translate according to the files property in your gt.config.json file.
It will use the defaultLocale specified in your gt.config.json file as the source language.
Translated files will be saved to the corresponding output location specified in your gt.config.json file.
See the configuration docs for more information on the files property.
Generating language files for GT projects
The CLI tool can be used to generate language files for projects that use gt-next or gt-react.
This is useful if you want to use your own translation provider.
Unlike other i18n libraries, gt-next and gt-react support inline translations, meaning there isn’t a traditional JSON file structure containing keys and values for each language.
To track changes to text, GT libraries store hashes of the original text in the source code.
This internal data structure (containing hashes) is awkward to work with, so the CLI tool provides a handy command to generate language files for your project.
- First, you’ll need to configure your project’s GT settings.
npx gtx-cli configureWhen prompted to save translations on the GT CDN, choose the “No” option.
- Run:
npx gtx-cli generateThis command will generate a source file for your default locale, and for all other locales you’ve configured for your project.
The content of each file is the same, and will be merged with any previous translations in your project.
See the API reference for more information on the generate command.
Notes
gtx-cli translateis used to automatically generate translations for your project.- If you’re using a third‑party i18n library, you can automatically update all your translation JSON files whenever your project changes. Just add the
gtx-cli translatecommand to your build or CD process before the build step to automatically add translations to your project.
Next steps
- See the CLI API reference for more information.
How is this guide?