GT ClassMethodsLocales

determineLocale

GT の determineLocale メソッドのAPIリファレンス

概要

determineLocale メソッドは、ユーザーの嗜好に基づいて承認済みの対応ロケールの一覧から最適なロケールを選定します。 完全一致がない場合に最も適切なロケールを見つけるためのロケールネゴシエーションを実装しています。

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

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

// Language fallback
// 言語のフォールバック

// 複数の優先設定
console.log(gt.determineLocale(['fr-CA', 'es-MX', 'en-US'])); // 'es-ES' (closest Spanish)

// 該当なし
console.log(gt.determineLocale('it-IT')); // undefined

リファレンス

パラメータ

Prop

Type

戻り値

string | undefined - 最も適合する locale。該当がない場合は undefined


ユーザーのlocaleネゴシエーション

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' が選ばれます

注意事項

  • 承認済みの 対応ロケール から最初の完全一致を返します
  • 正確な地域指定がない場合は言語一致にフォールバックします
  • 入力配列の優先順位を順守します
  • 適切な一致が見つからない場合は undefined を返します
  • Webアプリケーションでの locale 交渉を実装するうえで不可欠です

次のステップ

このガイドはどうでしたか?

determineLocale