# gt-node: General Translation Node.js SDK: getRequestLocale
URL: https://generaltranslation.com/ja/docs/node/reference/functions/get-request-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: リクエストの Accept-Language ヘッダーから、対応しているロケールの中で最適なものを検出します。General Translation の getRequestLocale の API リファレンス。

リクエストの `Accept-Language` ヘッダーから優先ロケールを抽出し、設定済みの locales と照合します。[`withGT`](/docs/node/reference/functions/with-gt) に渡すロケールを選択するために使用します。

## 概要 [#overview]

リクエストオブジェクトを渡して `getRequestLocale` を呼び出します。一致する最適な対応しているロケールを返し、一致するものが見つからない場合は `defaultLocale` を返します。

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

const locale = getRequestLocale(req); // 'fr'
```

シグネチャ:

```ts
getRequestLocale(request: { headers: Record<string, string | string[] | undefined> }): string
```

*注: `getRequestLocale` が対応しているロケールを認識できるよう、使用前に [`initializeGT`](/docs/node/reference/functions/initialize-gt) を呼び出してください。*

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

* **ヘッダーの解析。** リクエストの `Accept-Language` ヘッダー を解析し、設定された `locales` の中から最適な一致を判定します。
* **フォールバック。** 対応しているロケールに一致しない場合は、設定された `defaultLocale` を返します。
* **フレームワーク非依存。** Express や Fastify を含め、ヘッダーを `Record<string, string | string[] | undefined>` として扱えるあらゆるフレームワークで動作します。

## パラメータ [#parameters]

| パラメータ                 | 説明                                       | 型                                                              | 任意  | デフォルト |
| --------------------- | ---------------------------------------- | -------------------------------------------------------------- | --- | ----- |
| [`request`](#request) | リクエストヘッダーを含む `headers` プロパティを持つオブジェクトです。 | `{ headers: Record<string, string \| string[] \| undefined> }` | いいえ | —     |

### `request` [#request]

**型** `{ headers: Record<string, string | string[] | undefined> }` · **必須**

リクエストヘッダーを含む `headers` プロパティを持つオブジェクトです。Express、Fastify、およびヘッダーを `Record<string, string | string[] | undefined>` として公開している任意のフレームワークで利用できます。

## 戻り値 [#returns]

**Type** `string`

設定された locales の中で最も適切に一致する BCP 47 ロケールコード、または一致するものが見つからない場合は `defaultLocale`。

## 例 [#examples]

```ts title="middleware/locale.js"
// Expressミドルウェア
import { withGT, getRequestLocale } from 'gt-node';

export function localeMiddleware(req, res, next) {
  const locale = getRequestLocale(req);
  withGT(locale, () => next());
}
```

```ts title="middleware/locale.js"
// 他のストラテジーと組み合わせる — getRequestLocale をフォールバックとして使用する
import { withGT, getRequestLocale } from 'gt-node';

export function localeMiddleware(req, res, next) {
  const locale =
    req.query.lang ||
    req.cookies?.locale ||
    getRequestLocale(req);

  withGT(locale, () => next());
}
```

## Sitemap

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