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 を返します

次のステップ

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

requiresTranslation