GT ClassMethodsLocales
requiresTranslation
GT の requiresTranslation メソッドのAPIリファレンス
概要
requiresTranslation メソッドは、ソースとターゲットのロケールに基づいて翻訳の要否を判定します。
ロケールコードを比較し、承認済みのロケール一覧を考慮して、ソースコンテンツを翻訳する必要があるかを判断します。
リファレンス
パラメータ
Prop
Type
パラメーターの説明
| Parameter | 説明 | 
|---|---|
| sourceLocale | ソースのロケールコード。未指定の場合はインスタンスの sourceLocaleを使用します | 
| targetLocale | ターゲットのロケールコード。未指定の場合はインスタンスの targetLocaleを使用します | 
| approvedLocales | 承認済みターゲットロケールの配列。未指定の場合はインスタンスの locales配列を使用します | 
| customMapping | ロケール解決用の任意のカスタムマッピング | 
戻り値
boolean - 翻訳が必要な場合は true、不要な場合は false
スローされる例外
- Error- source の locale が指定されておらず、かつインスタンスに- sourceLocaleが設定されていない場合
- Error- target の locale が指定されておらず、かつインスタンスに- targetLocaleが設定されていない場合
例
const gt = new GT({
  sourceLocale: 'en-US',
  targetLocale: 'es-ES',
  locales: ['en-US', 'es-ES', 'fr-FR', 'de-DE']
});
// 異なる言語は翻訳が必要
console.log(gt.requiresTranslation('en-US', 'es-ES')); // true
console.log(gt.requiresTranslation('en-US', 'fr-FR')); // true
// 同じ言語は翻訳不要
console.log(gt.requiresTranslation('en-US', 'en-US')); // false
console.log(gt.requiresTranslation('es-ES', 'es-ES')); // false
// 同じ言語の異なる方言は翻訳不要
console.log(gt.requiresTranslation('en-US', 'en-GB')); // false
console.log(gt.requiresTranslation('es-ES', 'es-MX')); // false
// ターゲットが対応ロケールに含まれていない場合
console.log(gt.requiresTranslation('en-US', 'it-IT')); // false (it-ITは対応ロケールに含まれていない)補足
- 完全一致だけでなく、locale の言語系統も考慮します
- 承認済みの locale リストに従います
- 承認済みの locales(指定されている場合)にターゲットの locale が含まれない場合は falseを返します
次のステップ
- isSameLanguageで locale の関係を確認する
- determineLocaleで最適な locale を特定する
- isValidLocaleで 対応ロケール を検証する
このガイドはどうでしたか?

