Overview

Overview of the generaltranslation library

Introduction

The generaltranslation library serves as GT's core i18n library housing utility functions and classes for translation and formatting. It is most oftenly used with framework packages like gt-next and gt-react, but can be used as a standalone library.

index.ts
import { GT } from 'generaltranslation';

const gt = new GT({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  sourceLocale: 'en',
  targetLocale: 'es',
});

// Translate content
const result = await gt.translate('Hello, world!', 'es');
// "¡Hola, mundo!"

// Format numbers, dates, currencies
const formattedPrice = gt.formatCurrency(29.99, 'USD');
const formattedDate = gt.formatDateTime(new Date());
// "$29.99"
// "9/25/2025"

// Work with locales
const localeProps = gt.getLocaleProperties('fr-CA');
const isValid = gt.isValidLocale('de');
// { language: "fr", region: "CA", ... }
// true

Installation

npm install generaltranslation
yarn add generaltranslation
bun add generaltranslation
pnpm add generaltranslation

Examples

There are two major types of translation: string translation and file translation.

Setup

To enable translation, you need to provide a project ID and API key. Check out the constructor method for more information.

const gt = new GT({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  targetLocale: 'es',
});

String Translation

Check out the translate method for more information.

try {
  const result = await gt.translate('Hello, world!');
  console.log(result); // "¡Hola, mundo!"
} catch (error) {
  console.error('Translation failed:', error.message);
}

File Translation

Files are translated as jobs. You launch a job by uploading a file. Uploading many files will launch many jobs.

Check out the uploadSourceFiles and checkFileTranslations methods for more information.

// Files to upload
const files = [
  {
    source: {
      fileName: 'src/components/Button.tsx',
      fileFormat: 'TSX',
      locale: 'en',
      content: '...',
    },
  },
];

// Upload source files
await gt.uploadSourceFiles(files);

Table of Contents

GT Class

Main class for translation and locale functionality:

  • Constructor - Initialize GT instance with configuration
  • setConfig - Update GT instance configuration

Translation Methods

Formatting Methods

Locale Methods

Utility Functions

Formatting Functions

Locale Functions

Types and Interfaces

TypeScript definitions:


Next Steps

For framework-specific usage, see the Next.js or React documentation.

How is this guide?

Overview