# node: tx URL: https://generaltranslation.com/ja/docs/node/api/strings/tx.mdx --- title: tx description: tx runtime 文字列翻訳関数の API リファレンス --- ## 概要 `tx` 関数は、runtime で文字列を翻訳します。build-time の翻訳を解決する [`getGT`](/docs/node/api/get-gt) とは異なり、`tx` は オンデマンド 翻訳のためにコンテンツを送信するため、runtime でしかわからない文字列も翻訳できます。 ```js import { tx } from 'gt-node'; const translated = await tx('Hello, world!'); ``` **Runtime 翻訳:** `tx` はオンデマンドで翻訳を実行するため、build-time 翻訳に比べてネットワークリクエストが発生し、遅延が生じます。 build-time で判明している文字列には [`getGT`](/docs/node/api/get-gt) を使用し、コンテンツが動的な場合や事前に分からない場合にのみ `tx` を使用してください。 ## リファレンス ### パラメータ | 名前 | 型 | 説明 | | ---------- | ------------------------------------------------------------------------------- | -------------------- | | `content` | `string` | 翻訳する文字列。 | | `options?` | [`RuntimeTranslationOptions`](/docs/next/api/types/runtime-translation-options) | 翻訳の動作をカスタマイズするオプション。 | ### 戻り値 `Promise` — 翻訳された文字列。翻訳が不要な場合は元の文字列。 *** ## 例 ### 基本的な使い方 ```js title="handler.js" import { withGT, tx } from 'gt-node'; function handleRequest(locale) { return withGT(locale, async () => { return await tx('Processing complete'); }); } ``` ### 変数を使う `tx` は runtime に文字列を翻訳するため、template literal を使って変数を文字列に直接埋め込みます。こうすることで、翻訳用に送信されるコンテンツに変数の値も含まれます。 ```js title="handler.js" import { withGT, tx } from 'gt-node'; function handleStatus(locale, status) { return withGT(locale, async () => { return await tx(`Current status: ${status}`); }); } ``` **`tx` は `{variable}` プレースホルダーを補間しません。** ICU メッセージ形式 (`{name}` 構文) を使う [`getGT`](/docs/node/api/get-gt) とは異なり、`tx` は文字列を通常の文字列として扱います。 変数を含めるには、JavaScript のテンプレートリテラル (`` `Hello, ${name}` ``) を使用してください。 ### コンテキスト付き 翻訳の曖昧さを避けるため、`$context` を追加します。 ```js title="handler.js" const translated = await tx('Spring', { $context: 'the season, not a coil', }); ``` ### ロケールを指定する [`withGT`](/docs/node/api/with-gt) で設定したロケールを上書きするには: ```js title="handler.js" const translated = await tx('Hello, world!', { $locale: 'fr' }); ``` *** ## 注意事項 * `tx` は非同期で、Promise を返します。結果は必ず `await` してください。 * 翻訳はネットワークリクエスト経由で runtime に行われるため、build-time の翻訳に比べて遅延があります。 * `tx` は ICU の `{variable}` インターポレーションを **サポートしません**。変数を埋め込むにはテンプレートリテラルを使用してください。 * ICU 変数のインターポレーションを含む静的な文字列には、[`getGT`](/docs/node/api/get-gt) または [`msg`](/docs/node/api/strings/msg) / [`getMessages`](/docs/node/api/get-messages) を使用してください。 ## 次のステップ * build-time の文字列翻訳については [`getGT`](/docs/node/api/get-gt) を参照してください。 * 翻訳の動作をカスタマイズするには [`RuntimeTranslationOptions`](/docs/next/api/types/runtime-translation-options) を参照してください。 * 各翻訳アプローチの比較については [文字列翻訳パターン](/docs/node/guides/strings) を参照してください。