# General Translation Platform: determineLocale URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/locales/determine-locale.mdx --- title: determineLocale description: 承認済みロケールの一覧から最適な一致を持つロケールを見つけます。determineLocale の API リファレンス。 --- ユーザー設定に基づいて、[GT](/docs/platform/core/reference/gt-class/constructor) インスタンス上で、承認済みロケールの一覧から最適なロケールを判定します。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] * **まず完全一致。** 承認済みロケールの中から、最初に見つかった完全一致を返します。 * **言語フォールバック。** 完全に一致するリージョンがない場合は、言語が一致するものにフォールバックします。 * **優先順。** 入力配列の順序が優先されるため、優先度の低い完全一致よりも、優先度の高い言語一致が選ばれます。 * **一致なし。** 適切な一致が見つからない場合は `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 アプリケーションでロケールネゴシエーションを実装する際に不可欠です。