# General Translation Platform: determineLocale
URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/locales/determine-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 承認済みロケールの一覧から最適な一致を持つロケールを見つけます。determineLocale の API リファレンス。

General Translation は、完全一致がない場合に最も適したロケールを見つけるため、ロケールネゴシエーションを実装しています。

## 概要 [#overview]

[`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスで `determineLocale` を呼び出し、優先順に 1 つ以上の希望ロケールを指定します。戻り値は、最適に一致する承認済みロケールで、一致するものがない場合は `undefined` です。

```typescript
const gt = new GT({
  sourceLocale: 'en-US',
  locales: ['en-US', 'es-ES', 'fr-FR', 'de-DE'],
});

// 完全一致
console.log(gt.determineLocale('en-US')); // 'en-US'

// 言語フォールバック
console.log(gt.determineLocale('en-GB')); // 'en-US' (推定リージョンによるフォールバック)

// 複数の優先設定（優先順が適用される）
console.log(gt.determineLocale(['fr-CA', 'es-MX', 'en-US'])); // 'fr-FR' (第1優先設定に最も近い)

// 一致なし
console.log(gt.determineLocale('it-IT')); // undefined
```

シグネチャ:

```typescript
determineLocale(
  locales: string | string[],
  approvedLocales?: string[],
  customMapping?: CustomMapping
): string | undefined
```

*注: `determineLocale` はローカルで実行されるため、APIキーは不要です。`approvedLocales` と `customMapping` を省略した場合は、インスタンスの `locales` と `customMapping` が使用されます。`GT` インスタンスなしでマッチングする場合は、スタンドアロンの [`determineLocale`](/docs/platform/core/reference/utility-functions/locales/determine-locale) を参照してください。*

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

* **まず完全一致。** 現在のユーザー設定に対して、完全一致する承認済みロケールを返します。
* **導出ロケールの照合。** 言語とリージョン、言語とスクリプト、および最小化された形式を確認してから、その言語で想定されるリージョンとスクリプトを使用します。
* **優先順。** 入力配列の順序が優先されるため、優先度の低い完全一致よりも、優先度の高い言語一致が選ばれます。
* **承認済みロケール。** `approvedLocales` を、結果として選択可能なロケールの集合として扱います。その配列の順序は、同一言語の候補間の選択には影響しません。
* **一致なし。** 適切な一致が見つからない場合は `undefined` を返します。

## パラメータ [#parameters]

| パラメータ                                  | 説明                          | 型                                                                     | 任意  | デフォルト                |
| -------------------------------------- | --------------------------- | --------------------------------------------------------------------- | --- | -------------------- |
| [`locales`](#locales)                  | 優先順で指定する、単一のロケールまたはロケールの配列。 | `string \| string[]`                                                  | いいえ | —                    |
| [`approvedLocales`](#approved-locales) | マッチングの対象となるロケール。            | `string[]`                                                            | はい  | `this.locales`       |
| [`customMapping`](#custom-mapping)     | 解決に使用するカスタムロケールマッピング。       | [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) | はい  | `this.customMapping` |

### `locales` [#locales]

**型** `string | string[]` · **必須**

単一のロケール、または優先順位順のロケール配列 (たとえば、ブラウザーの `Accept-Language` リスト) 。

### `approvedLocales` [#approved-locales]

**型** `string[]` · **任意** · **デフォルト** `this.locales`

マッチングの対象となるロケールです。省略した場合は、このインスタンスの `locales` が使用されます。同一言語のロケールのうちどれが選ばれるかは、配列の順序では決まりません。

### `customMapping` [#custom-mapping]

**Type** [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) · **省略可能** · **デフォルト** `this.customMapping`

解決時に使用するカスタムのロケールマッピングです。省略した場合は、このインスタンスの `customMapping` が使用されます。

## 戻り値 [#returns]

**Type** `string | undefined`

最も適切に一致するロケール。一致するものが見つからない場合は `undefined`。

## 例 [#examples]

```typescript
// ユーザーのロケールネゴシエーション
const gt = new GT({
  sourceLocale: 'en-US',
  locales: ['en-US', 'en-GB', 'es-ES', 'fr-FR'],
});

// ブラウザの Accept-Language ヘッダーをシミュレートする
const userPreferences = ['fr-CA', 'en-GB', 'en'];
const bestMatch = gt.determineLocale(userPreferences);
console.log(bestMatch); // 優先順位に基づき 'fr-FR' が返される
```

## メモ [#notes]

* 各ユーザー設定について、完全一致および導出されたロケールコードを確認します。
* 同一言語の方言との任意の一致ではなく、推定リージョンおよび推定スクリプトへのフォールバックを使用します。
* 入力配列内の優先順を維持します。
* 承認済みロケール配列の順序をタイブレークには使用しません。
* 適切な一致が見つからない場合は、`undefined` を返します。
* Web アプリケーションでロケールネゴシエーションを実装する際に不可欠です。

## Sitemap

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