GT CLI Tool
GT (General Translation) is an AI-powered command-line interface (CLI) tool designed for streamlining internationalization (i18n) and localization (l10n) workflows in JavaScript and TypeScript projects. Currently at version 2.14.17, the project exhibits an active development cadence, frequently releasing patch updates across its core `gt` package and related ecosystem tools like `gt-node`, `@generaltranslation/react-core-linter`, and `gt-tanstack-start`. Its primary differentiator lies in leveraging artificial intelligence for translation processes, aiming to automate and enhance the accuracy of localization efforts. The tool handles various aspects of i18n, from extracting translatable strings to managing and applying translations, supporting integration with different frontend frameworks and build systems, providing a robust, developer-centric approach to managing multilingual applications.
Common errors
-
Error: API key is missing or invalid. Please provide a valid key.
cause Attempting to run a translation command without a configured or valid General Translation API key.fixSet the `GT_API_KEY` environment variable or pass `api-key` flag/argument with a valid key. Register for one on the General Translation website. -
Error: No translatable strings found in the project.
cause The `gt extract` command failed to identify any strings marked for translation in the configured source files.fixVerify that your source files contain strings marked with `gt-i18n-key` comments or follow other configured extraction patterns. Ensure the `--project-root` and `--source-locale` settings are correct. -
Error: Command 'translate' requires a source locale.
cause The `gt translate` command was executed without specifying the source locale for the strings to be translated.fixAdd the `--source-locale <locale_code>` argument to your `translate` command, e.g., `--source-locale en`.
Warnings
- gotcha GT relies on an API key for its AI-powered translation services. Running `gt translate` or related commands without a valid key will result in errors or failed operations. Ensure `GT_API_KEY` is set in your environment or passed via command-line flags.
- gotcha The `gt` package is part of a larger ecosystem (e.g., `gt-react`, `gt-node`, `gtx-cli`). Ensure consistent versioning across related General Translation packages within your project to avoid compatibility issues, especially when consuming libraries that depend on `gt`.
- gotcha GT's string extraction capabilities depend on specific comment directives (e.g., `gt-i18n-key`). If these directives are missing or incorrectly formatted, strings may not be detected for translation, leading to incomplete localization files.
- breaking While recent updates are patch-level, earlier major versions (e.g., transition from `v1` to `v2`) introduced significant changes to configuration file formats and API interfaces. Always review release notes when upgrading across major versions.
Install
-
npm install gt -
yarn add gt -
pnpm add gt
Imports
- main
import gt from 'gt';
import { main } from 'gt'; - cli
const cli = require('gt').cli;import { cli } from 'gt'; - GTConfig
import { GTConfig } from 'gt';import type { GTConfig } from 'gt';
Quickstart
import { main } from 'gt';
import * as path from 'path';
import * as fs from 'fs';
async function demonstrateGTWorkflow() {
const apiKey = process.env.GT_API_KEY ?? '';
if (!apiKey) {
console.warn('GT_API_KEY environment variable is not set. Please set it for full functionality.');
// For demonstration, we might proceed, but real translation will fail without it.
}
// Ensure a temporary project root for isolated demonstration
const tempProjectRoot = path.join(__dirname, '.temp-gt-project');
fs.mkdirSync(tempProjectRoot, { recursive: true });
process.chdir(tempProjectRoot); // Change working directory for CLI commands
console.log('--- GT Quickstart Demonstration ---');
try {
// 1. Initialize GT configuration (simulating `gt init`)
console.log('Initializing GT project...');
await main(['init']);
// 2. Create a dummy source file for extraction
const exampleFilePath = path.join(tempProjectRoot, 'src', 'app.ts');
fs.mkdirSync(path.dirname(exampleFilePath), { recursive: true });
fs.writeFileSync(exampleFilePath, `
// Example file with translatable strings
const greeting = 'Hello, world!'; // gt-i18n-key: helloWorld
const welcomeMsg = 'Welcome to our application.'; // gt-i18n-key: welcomeMessage
console.log(greeting, welcomeMsg);
`);
// 3. Extract translatable strings (simulating `gt extract`)
console.log('Extracting translatable strings...');
await main(['extract']);
// 4. Translate strings to a target language (e.g., French)
// This step requires an API key to function correctly.
console.log('Translating strings to French...');
await main(['translate', '--source-locale', 'en', '--target-locales', 'fr', ...(apiKey ? ['--api-key', apiKey] : [])]);
console.log('\nGT workflow demonstrated successfully in:', tempProjectRoot);
console.log('Check the `.gt` and `translations` directories for generated files.');
} catch (error) {
console.error('\nGT demonstration failed:', error.message || error);
} finally {
// Cleanup temporary directory
process.chdir(__dirname); // Change back to original directory
if (fs.existsSync(tempProjectRoot)) {
fs.rmSync(tempProjectRoot, { recursive: true, force: true });
console.log('Cleaned up temporary project directory:', tempProjectRoot);
}
}
}
demonstrateGTWorkflow();