# gt-node: General Translation Node.js SDK: getTranslations
URL: https://generaltranslation.com/ja/docs/node/reference/functions/get-translations.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 現在のリクエストのロケールに対応する General Translation の 辞書 resolver を取得します。getTranslations の API リファレンス。

id で 辞書 エントリを解決する `t` 関数を返す async 関数です。[`initializeGT`](/docs/node/reference/functions/initialize-gt) で設定した 辞書 と組み合わせて使用することで、リクエストごとに一元管理された key ベースの文言を翻訳できます。

## 概要 [#overview]

[`withGT`](/docs/node/reference/functions/with-gt) のスコープ内で `getTranslations` を await すると、同期的な `t(id, options?)` 関数を取得でき、そこから id を使って辞書エントリを参照できます。

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

const t = await getTranslations();
const title = t('page.title');
```

シグネチャ:

```ts
getTranslations(rootId?: string): Promise<TFunctionType>

type TFunctionType = ((id: string, options?: TranslationVariables) => string) & {
  obj: (id: string) => DictionaryObjectTranslation;
};
```

*注: `getTranslations` は、どのロケールを使うかを判断できるように [`withGT`](/docs/node/reference/functions/with-gt) のコールバック内で呼び出す必要があります。また、[`initializeGT`](/docs/node/reference/functions/initialize-gt) で設定した `dictionary` (または [`loadDictionary`](/docs/react/reference/functions/load-dictionary)) も必要です。*

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

* **辞書の参照。** `t(id)` は辞書内の `id` に対応するエントリを参照し、アクティブなロケールの翻訳を返します。翻訳がない場合はソースエントリにフォールバックします。
* **不明な id は throw されます。** ソース辞書に `id` のエントリがない場合、`t(id)` は `Dictionary entry <id> cannot be found` を throw します。これは `gt-node` で、ソース文字列にフォールバックせずに throw する唯一の翻訳パスです。
* **root によるスコープ。** `rootId` を渡すと、すべての参照を特定のプレフィックス配下にスコープできます。これにより、`getTranslations('page')` を使うと `t('page.title')` ではなく `t('title')` を呼び出せます。
* **補間。** options object で変数を渡すと、`{key}` 構文を使ってエントリに補間できます。
* **オブジェクトのサブツリー。** `t.obj(id)` は辞書のネストされたサブツリーを、翻訳済みの string からなるプレーンなオブジェクトとして返します。

## パラメータ [#parameters]

`getTranslations` は省略可能な `rootId` を受け取ります。返される `t` 関数は以下を受け取ります。

| パラメータ                 | 説明                       | 型                      | 任意  | デフォルト |
| --------------------- | ------------------------ | ---------------------- | --- | ----- |
| [`rootId`](#root-id)  | すべての検索 id に適用されるプレフィックス。 | `string`               | はい  | —     |
| [`id`](#id)           | 解決する辞書エントリの id。          | `string`               | いいえ | —     |
| [`options`](#options) | エントリに埋め込む変数の値。           | `TranslationVariables` | はい  | `{}`  |

### `rootId` [#root-id]

**型** `string` · **任意**

`t` に渡すすべての id に付与されるプレフィックスです。設定すると、`t('title')` は `rootId.title` を参照します。

### `id` [#id]

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

解決対象の辞書エントリの id です。ネストされたエントリにはドット記法を使用します (例: `'page.title'`) 。

### `options` [#options]

**型** `TranslationVariables` · **省略可能**

`{key}` 構文を使って、解決されたエントリに埋め込む変数値を格納するキーと値の対応です。

## 戻り値 [#returns]

**Type** `Promise<TFunctionType>`

この Promise は `t` 関数に解決されます。`t(id, options?)` を呼び出すと翻訳済みの文字列が返され、`t.obj(id)` は翻訳済みのオブジェクトのサブツリーを返します。

## 例 [#examples]

```ts title="server.js"
// 起動時に辞書を設定する
import { initializeGT } from 'gt-node';

initializeGT({
  defaultLocale: 'en',
  locales: ['en', 'es', 'fr'],
  dictionary: {
    page: { title: 'Welcome', greeting: 'Hello, {name}!' },
  },
});
```

```ts title="handler.js"
// リクエストのロケールに対応する辞書エントリを解決する
import { withGT, getTranslations } from 'gt-node';

function handleRequest(locale) {
  return withGT(locale, async () => {
    const t = await getTranslations();
    return {
      title: t('page.title'),
      greeting: t('page.greeting', { name: 'Alice' }),
    };
  });
}
```

```ts title="handler.js"
// rootIdでルックアップをスコープする
const t = await getTranslations('page');
const title = t('title'); // 'page.title'に解決される
```

## メモ [#notes]

* 不明な 辞書 id は例外になります。辞書 の key は、source とコードの間で同期してください。
* inline string には [`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.
