Overview

Overview of the generaltranslation library

Introduction

The generaltranslation library serves as GT’s core i18n library, providing utility functions and classes for translation and formatting. It is most commonly used with framework packages such as gt-next and gt-react, but it can also 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, and currencies
const formattedPrice = gt.formatCurrency(29.99, 'USD');
const formattedDate = gt.formatDateTime(new Date());
// "$29.99"
// "25/09/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 main types of translation: string translation and file translation.

Setup

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

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

String Translation

See 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 start a job by uploading a file. Uploading multiple files will start multiple jobs.

See 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 - Initialise the GT instance with configuration
  • setConfig - Update the 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