# General Translation Platform: Translating strings
URL: https://generaltranslation.com/en-US/docs/platform/core/guides/translating-strings.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: How to translate strings and structured content from code with the generaltranslation library.

You can use [translate](/docs/platform/core/reference/gt-class-methods/translation/translate) and [translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) to translate strings directly from code.

Use string translation when your app, script, or service needs translated content at runtime instead of translating a whole file.

## Before you start [#before-start]

Make sure you've completed the [Quickstart](/docs/platform/core/quickstart) to install `generaltranslation` and initialize the [GT](/docs/platform/core/reference/gt-class/constructor) class.

## Translate one string [#translate-one-string]

Call [translate](/docs/platform/core/reference/gt-class-methods/translation/translate) with the source string and target locale. 

If your [GT](/docs/platform/core/reference/gt-class/constructor) instance has a [targetLocale](/docs/platform/core/reference/types/gt-constructor-params), you can omit the target locale from the method call. This is useful when a script or service is translating many strings into the same locale.

```typescript title="src/index.ts"
const result = await gt.translate('Hello, world!', 'es');

if (result.success) {
  console.log(result.translation); // "¡Hola, mundo!"
} else {
  console.error(`Translation failed: ${result.error}`);
}
```

(See the [translate](/docs/platform/core/reference/gt-class-methods/translation/translate) method for more information).

## Translate many strings [#translate-many-strings]

Call [translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) to efficiently translate multiple content items in a single API request. It's optimized for batch processing and provides better performance than multiple individual [translate](/docs/platform/core/reference/gt-class-methods/translation/translate) calls.

```typescript title="src/index.ts"
const results = await gt.translateMany(
  ['Hello, world!', 'Welcome to our app', 'Click here to continue'],
  'es'
);
```

(See the [translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) method for more information).

## Add context with source metadata [#add-context-source]

Use metadata when a string needs extra context.

```typescript title="src/index.ts"
const result = await gt.translate(
  {
    source: 'Cells',
    metadata: {
      context: 'Spreadsheet column header',
    },
  },
  'es'
);
```

Metadata is useful for ambiguous words, UI labels, product-specific terms, and strings that are difficult to understand without surrounding context.

## Next steps

- /docs/platform/core/guides/translating-files
- /docs/platform/core/guides/locale-codes

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
