# General Translation Platform: determineLocale
URL: https://generaltranslation.com/ja/docs/platform/core/reference/utility-functions/locales/determine-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: GT インスタンスなしで最適なロケールを見つけます。determineLocale の API リファレンス。

[`determineLocale`](/docs/platform/core/reference/gt-class-methods/locales/determine-locale) は、General Translation のコアライブラリに含まれるスタンドアロンのユーティリティ関数で、ユーザーの設定に基づいて、承認済みロケールの一覧から最適なロケールを見つけます。GT インスタンスを必要とせずにコンテンツネゴシエーションを実現します。

## 概要 [#overview]

`generaltranslation` から `determineLocale` を直接インポートし、ユーザーの優先ロケールと承認済みロケールの一覧を引数に渡して呼び出します。これには API key も [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスも必要ありません。`approvedLocales` のデフォルト値としてインスタンスに設定された locales を使うインスタンスベースの同等機能が必要な場合は、代わりに [`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスの [`determineLocale`](/docs/platform/core/reference/gt-class-methods/locales/determine-locale) メソッドを使用してください。

```typescript
import { determineLocale } from 'generaltranslation';

const approvedLocales = ['en-US', 'es-ES', 'fr-FR', 'de-DE'];

const best = determineLocale(['fr-CA', 'es-MX'], approvedLocales);
// 戻り値: "fr-FR"
```

シグネチャ:

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

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

* **優先順位。** `locales` は、優先順位順に並べた単一のロケールまたは配列です。各候補は、承認済みロケールの一覧に対して順に照合されます。
* **マッチング。** まず完全一致するロケールを確認し、次に導出された言語-リージョン、言語-スクリプト、および最小化されたコードを確認します。いずれも一致しない場合は、その言語の推定リージョンとスクリプトを確認します。たとえば、`fr-CA` は `fr-FR` に一致し得ますが、`en-GB` のみを含む承認済みリストに対して `en-AU` は一致しません。
* **一致なし。** どの候補も承認済みロケールに一致しない場合は、`undefined` を返します。
* **カスタムマッピング。** ロケールの検証と標準化では、[`customMapping`](/docs/platform/core/reference/types/custom-mapping) が適用されます。

## パラメーター [#parameters]

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

### `locales` [#locales]

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

単一のロケール、または優先度順 (最も優先されるものが先頭) のロケール配列。

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

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

マッチングの対象となるロケールの一覧です。同一言語のロケールのどれが選ばれるかは配列の順序では決まりません。マッチングでは、各ユーザー設定から導出されたリージョンとスクリプトが使用されます。

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

**型** [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) · **任意**

マッチング前にロケールを検証・標準化する際に適用されるカスタムマッピングです。

## 戻り値 [#returns]

**型** `string | undefined`

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

## 例 [#examples]

```typescript
import { determineLocale } from 'generaltranslation';

const approvedLocales = ['en-US', 'es-ES', 'fr-FR', 'de-DE'];

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

// 言語フォールバック (en-GB → en-US)
console.log(determineLocale('en-GB', approvedLocales)); // 'en-US'

// 複数の優先ロケール (fr-CA は es-MX より先に fr-FR に一致する)
console.log(determineLocale(['fr-CA', 'es-MX'], approvedLocales)); // 'fr-FR'

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

## メモ [#notes]

* 完全一致するロケールコードに加えて、推定リージョンや推定スクリプトのフォールバックを含む導出されたロケールコードにも一致します。
* 入力配列の優先順を尊重します。
* approve されたロケール配列の順序をタイブレーカーとしては使用しません。
* 一致するものが見つからない場合は `undefined` を返します。
* Web アプリケーションのロケールネゴシエーションに不可欠です。

## Sitemap

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