GT ClassMethodsTranslation

translateMany

API Reference for the GT translateMany method for batch translations

Overview

The translateMany method efficiently translates multiple content items in a single API request. It's optimized for batch processing and provides better performance than multiple individual translate calls.

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

const result = await gt.translateMany([
  { source: 'Hello, world!' },
  { source: 'Welcome to our app' },
  { source: 'Click here to continue' }
], { targetLocale: 'es' });

Reference

Parameters

Prop

Type

Parameters Description

ParameterDescription
sourcesArray of Entry objects containing source content and optional per-item metadata
globalMetadataGlobal metadata applied to all entries, including the required targetLocale

Entry Object Structure

Each entry in the sources array can contain:

interface Entry {
  source: Content;           // The content to translate
  targetLocale?: string;     // Override global target locale
  context?: string;          // Translation context for this entry
  tags?: string[];          // Tags for categorization
  // ... other EntryMetadata properties
}

Returns

Promise<TranslateManyResult>

The result contains translated entries and any error information:

interface TranslateManyResult {
  translations: Array<TranslationResult | TranslationError>;
  metadata: {
    totalRequests: number;
    successCount: number;
    errorCount: number;
    processingTime: number;
  };
}

Behavior

Global vs Per-Item Metadata

  • Global metadata is applied to all entries as defaults
  • Per-item metadata overrides global settings for specific entries
  • Target locale can be overridden per entry

Error Handling Strategy

  • Individual translation failures don't stop the entire batch
  • Each result indicates success or failure independently
  • Partial success scenarios are fully supported

Locale Resolution

  • Global target locale is used as default for all entries
  • Per-entry target locales override the global setting
  • All locales are resolved through custom mapping if configured

Examples

const menuItems = await gt.translateMany([
  { source: 'Home' },
  { source: 'About' },
  { source: 'Products' },
  { source: 'Contact' }
], {
  targetLocale: 'fr',
  context: 'Navigation menu items'
});

menuItems.translations.forEach((result, index) => {
  if ('translation' in result) {
    console.log(`Item ${index}: ${result.translation}`);
  } else {
    console.error(`Item ${index} failed: ${result.error}`);
  }
});

Notes

  • Translates multiple items in a single API request
  • Translation failures in one entry don't affect others
  • Results maintain the same order as input entries
  • Global metadata is merged with per-entry metadata (per-entry takes precedence)

Next Steps

How is this guide?

translateMany