# General Translation Platform: 文字列の翻訳
URL: https://generaltranslation.com/ja/docs/platform/core/guides/translating-strings.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: generaltranslationライブラリを使用して、コードから文字列と構造化コンテンツを翻訳する方法。

[translate](/docs/platform/core/reference/gt-class-methods/translation/translate) と [translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) を使うと、コードから直接文字列を翻訳できます。

アプリ、スクリプト、またはサービスで、ファイル全体を翻訳するのではなく、Runtime に翻訳済みコンテンツが必要な場合は、文字列の翻訳を使用します。

## 始める前に [#before-start]

[Quickstart](/docs/platform/core/quickstart) を完了し、`generaltranslation` のインストールと [GT](/docs/platform/core/reference/gt-class/constructor) クラスの初期化を済ませておいてください。

## 1 つの文字列を翻訳する [#translate-one-string]

ソース文字列と対象のロケールを指定して、[translate](/docs/platform/core/reference/gt-class-methods/translation/translate) を呼び出します。

[GT](/docs/platform/core/reference/gt-class/constructor) インスタンスに [targetLocale](/docs/platform/core/reference/types/gt-constructor-params) が設定されている場合は、メソッド呼び出しで対象のロケールを省略できます。これは、スクリプトやサービスが多数の文字列を同じロケールに翻訳する場合に便利です。

```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}`);
}
```

 (詳しくは、[translate](/docs/platform/core/reference/gt-class-methods/translation/translate)メソッドを参照してください) 。

## 複数の文字列を翻訳する [#translate-many-strings]

複数のコンテンツ項目を1回のAPIリクエストで効率的に翻訳するには、[translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) を呼び出します。これはバッチ処理向けに最適化されており、[translate](/docs/platform/core/reference/gt-class-methods/translation/translate) を個別に複数回呼び出すよりも高いパフォーマンスを発揮します。

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

 (詳しくは、[translateMany](/docs/platform/core/reference/gt-class-methods/translation/translate-many) メソッドを参照してください) 。

## sourceメタデータを使ってコンテキストを追加する [#add-context-source]

文字列に追加のコンテキストが必要な場合は、メタデータを使用します。

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

メタデータは、あいまいな語句やUIラベル、製品固有の用語、前後の文脈がないと理解しにくい文字列に役立ちます。

## 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.
