GT CLI Tool

2.14.17 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates the typical GT workflow programmatically: initializing a project, extracting translatable strings from source files, and then performing an AI-powered translation to a target language.

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();

view raw JSON →