# gt-node: General Translation Node.js SDK: getMessages
URL: https://generaltranslation.com/ja/docs/node/reference/functions/get-messages.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 現在のリクエストのロケールに対して、`msg` で登録されたメッセージ用の General Translation リゾルバーを取得します。getMessages の API リファレンス。

事前登録されたメッセージ用のリゾルバーを返す非同期関数です。[`msg`](/docs/node/reference/functions/msg) と組み合わせて使うことで、ビルド時に文字列を登録し、ランタイムでその翻訳を解決できます。

## 概要 [#overview]

モジュールスコープで [`msg`](/docs/node/reference/functions/msg) を使って文字列を登録し、[`withGT`](/docs/node/reference/functions/with-gt) スコープ内で `getMessages` を await すると、同期的な `m(encodedMsg, options?)` リゾルバーを取得できます。

```ts
import { msg, getMessages } from 'gt-node';

// ビルド時（またはモジュールスコープ）に登録
const greeting = msg('Hello, world!');

// ランタイムに解決
const m = await getMessages();
const translated = m(greeting);
```

シグネチャ:

```ts
getMessages(): Promise<MFunctionType>

type MFunctionType = <T extends string | null | undefined>(
  encodedMsg: T,
  options?: GTTranslationOptions
) => T extends string ? string : T;
```

*注: `getMessages` は、使用するロケールを識別できるよう、[`withGT`](/docs/node/reference/functions/with-gt) の callback 内で呼び出す必要があります。*

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

* **2 段階のパターン。** [`msg`](/docs/node/reference/functions/msg) は build-time 翻訳用に string を登録し、`getMessages` は実行時にアクティブなロケール向けの値へ解決します。
* **モジュールスコープの文字列。** このパターンは、モジュールスコープで定義され、リクエストごとに翻訳する必要がある文字列 (定数、列挙型、エラーメッセージなど) に適しています。
* **Null のパススルー。** `null` または `undefined` を `m` に渡すと、それぞれ `null` または `undefined` がそのまま返されます。

## パラメータ [#parameters]

`getMessages` はパラメータを受け取りません。返される `m` リゾルバーは次を受け取ります。

| パラメータ                        | 説明                                                                       | Type                          | Optional | Default |
| ---------------------------- | ------------------------------------------------------------------------ | ----------------------------- | -------- | ------- |
| [`encodedMsg`](#encoded-msg) | [`msg()`](/docs/node/reference/functions/msg) によって返される、エンコードされたメッセージ文字列。 | `string \| null \| undefined` | いいえ      | —       |
| [`options`](#options)        | 解決済みメッセージに補間する変数値と翻訳オプション。                                               | `GTTranslationOptions`        | はい       | `{}`    |

### `encodedMsg` [#encoded-msg]

**型** `string | null | undefined` · **必須**

[`msg`](/docs/node/reference/functions/msg) が返す、エンコードされたメッセージ文字列です。`null`/`undefined` が渡された場合は、`null`/`undefined` を返します。

### `options` [#options]

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

`{key}` 構文を使って、解決済みメッセージに補間する変数値と、`$context`、`$id`、`$locale`、`$maxChars` などの翻訳オプションを指定します。エンコード済みメッセージにすでに補間オプション (`msg('...', { ... })` 由来) が含まれている場合は、そちらが使用され、この引数は無視されます。

## 戻り値 [#returns]

**Type** `Promise<MFunctionType>`

`m` メッセージ解決関数を返します。`m(encodedMsg, options?)` は、入力が `null`/`undefined` の場合はそのまま保持しつつ、デコードおよび補間された翻訳を返します。

## 例 [#examples]

```ts title="messages.js"
// モジュールスコープでメッセージを登録する
import { msg } from 'gt-node';

export const GREETING = msg('Hello, world!');
export const WELCOME = msg('Welcome, {name}!');
```

```ts title="handler.js"
// リクエストのロケールに対してメッセージを解決する
import { withGT, getMessages } from 'gt-node';
import { GREETING, WELCOME } from './messages';

function handleRequest(locale) {
  return withGT(locale, async () => {
    const m = await getMessages();
    return {
      greeting: m(GREETING),
      welcome: m(WELCOME, { name: 'Alice' }),
    };
  });
}
```

```ts title="handler.js"
// 変数を使用する場合 — 値を第2引数として渡す
import { msg, getMessages, withGT } from 'gt-node';

const ORDER_STATUS = msg('Order {orderId} is {status}.');

function getOrderMessage(locale, orderId, status) {
  return withGT(locale, async () => {
    const m = await getMessages();
    return m(ORDER_STATUS, { orderId, status });
  });
}
```

## メモ [#notes]

* [`msg`](/docs/node/reference/functions/msg) は、ビルド時翻訳用の `文字列` を登録し、`getMessages` はそれをランタイムに解決します。
* このパターンは、リクエストごとの翻訳が必要な、モジュールスコープで定義された `文字列` (定数、列挙型、エラーメッセージなど) に適しています。
* `null` または `undefined` が `m` に渡された場合、それぞれ `null` または `undefined` を返します。

## Sitemap

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