# gt-node: General Translation Node.js SDK: tx
URL: https://generaltranslation.com/ja/docs/node/reference/functions/tx.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: General Translation を使用して、ランタイムで文字列をオンデマンド翻訳します。tx の API リファレンス。

ランタイムで文字列を翻訳します。build-time 翻訳を返す [`getGT`](/docs/node/reference/functions/get-gt) とは異なり、`tx` はコンテンツをオンデマンド翻訳のために送信するため、ランタイムでしか判明しない文字列も翻訳できます。

## 概要 [#overview]

翻訳する文字列を指定して、[`withGT`](/docs/node/reference/functions/with-gt) スコープ内で `tx` を `await` します。翻訳後の文字列に解決されます。

```ts
import { tx } from 'gt-node';

const translated = await tx('Hello, world!');
```

シグネチャ:

```ts
tx(content: string, options?: RuntimeTranslationOptions): Promise<string>
```

*注: `tx` はオンデマンドで翻訳を行うため、ネットワークリクエストが発生し、ビルド時の翻訳に比べて遅延が生じます。ビルド時に判明している文字列には [`getGT`](/docs/node/reference/functions/get-gt) を使用し、コンテンツが動的な場合や事前にわからない場合にのみ `tx` を使用してください。*

## 仕組み [#how-it-works]

* **ランタイム翻訳。** 翻訳はキャッシュミス時にネットワークリクエスト経由でランタイムに行われるため、ビルド時翻訳と比べて遅延があります。`tx` は動的コンテンツにのみ使ってください。
* **ICU 補間なし。** [`getGT`](/docs/node/reference/functions/get-gt) とは異なり、`tx` は `{variable}` プレースホルダーを補間せず、文字列をプレーンテキストとして扱います。変数の値が翻訳対象として送信されるコンテンツに含まれるよう、JavaScript のテンプレートリテラルで埋め込んでください。
* **フォールバック。** 翻訳が不要な場合は、元の文字列を返します。

## パラメータ [#parameters]

| パラメータ                 | 説明                  | Type                        | Optional | Default |
| --------------------- | ------------------- | --------------------------- | -------- | ------- |
| [`content`](#content) | 翻訳する文字列。            | `string`                    | いいえ      | —       |
| [`options`](#options) | 翻訳動作をカスタマイズするオプション。 | `RuntimeTranslationOptions` | はい       | —       |

### `content` [#content]

**型** `string` · **必須**

翻訳対象の文字列です。動的な値を埋め込む場合は、`{variable}` プレースホルダーではなくテンプレートリテラルを使用してください。

### `options` [#options]

**型** `RuntimeTranslationOptions` · **任意**

ランタイム翻訳をカスタマイズするためのオプション:

* `$context?: string` — 翻訳の曖昧さを解消するための追加コンテキスト。
* `$locale?: string` — [`withGT`](/docs/node/reference/functions/with-gt) で設定されたロケールを上書きします。
* `$maxChars?: number` — 翻訳サービスに要求する正の整数の最大文字数。必要に応じて、返される文字列はこの長さに切り詰められます。
* `$requiresReview?: boolean` — 使用前に翻訳の承認が必要かどうか。

*注: `RuntimeTranslationOptions` は [`getGT`](/docs/node/reference/functions/get-gt) のインラインオプションに似ていますが、`$id` は含まれず、`$format` のデフォルト値は ICU ではなく plain-string (非 ICU) です。*

## 戻り値 [#returns]

**型** `Promise<string>`

翻訳された文字列を返します。翻訳が不要な場合は、元の文字列を返します。

## 例 [#examples]

```ts title="handler.js"
// 基本的な使い方
import { withGT, tx } from 'gt-node';

function handleRequest(locale) {
  return withGT(locale, async () => {
    return await tx('Processing complete');
  });
}
```

```ts title="handler.js"
// 変数を使用する場合 — {プレースホルダー}ではなく、テンプレートリテラルで値を埋め込む
import { withGT, tx } from 'gt-node';

function handleStatus(locale, status) {
  return withGT(locale, async () => {
    return await tx(`Current status: ${status}`);
  });
}
```

```ts title="handler.js"
// 翻訳の曖昧さを解消するためのコンテキスト付き
const translated = await tx('Spring', {
  $context: 'the season, not a coil',
});
```

```ts title="handler.js"
// ロケールを指定する — withGT のロケールを上書き
const translated = await tx('Hello, world!', { $locale: 'fr' });
```

## メモ [#notes]

* `tx` は非同期で、Promise を返します。結果は必ず `await` してください。
* 翻訳はネットワークリクエスト経由でランタイムに行われるため、ビルド時の翻訳に比べて遅延があります。
* `tx` は ICU の `{variable}` 補間をサポートしていません。変数を埋め込むにはテンプレートリテラルを使用してください。
* ICU の変数補間を含む静的な文字列には、[`getGT`](/docs/node/reference/functions/get-gt) または [`getMessages`](/docs/node/reference/functions/get-messages) と組み合わせた [`msg`](/docs/node/reference/functions/msg) を使用してください。

## Sitemap

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