GT ClassMethodsLocales

requiresTranslation

API reference for the GT requiresTranslation method

Overview

The requiresTranslation method determines whether translation is needed based on the source and target locales. It checks whether the source content needs translating by comparing locale codes and taking the approved locale list into account.


Reference

Parameters

Prop

Type

Parameter descriptions

ParameterDescription
sourceLocaleThe source locale code. If not provided, uses the instance’s sourceLocale.
targetLocaleThe target locale code. If not provided, uses the instance’s targetLocale.
approvedLocalesArray of approved target locales. If not provided, uses the instance’s locales array.
customMappingOptional custom mapping for locale resolution.

Returns

booleantrue if a translation is required, false otherwise

Throws

  • Error – if no source locale is provided and the instance has no sourceLocale configured
  • Error – if no target locale is provided and the instance has no targetLocale configured

Examples

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

// Different languages require translation
console.log(gt.requiresTranslation('en-US', 'es-ES')); // true
console.log(gt.requiresTranslation('en-US', 'fr-FR')); // true

// Same languages don't require translation
console.log(gt.requiresTranslation('en-US', 'en-US')); // false
console.log(gt.requiresTranslation('es-ES', 'es-ES')); // false

// Different dialects of same language don't require translation
console.log(gt.requiresTranslation('en-US', 'en-GB')); // false
console.log(gt.requiresTranslation('es-ES', 'es-MX')); // false

// Target not in approved locales
console.log(gt.requiresTranslation('en-US', 'it-IT')); // false (it-IT not in approved locales)

Notes

  • Takes locale language families into account, not just exact matches
  • Respects approved locale lists
  • Returns false when the target locale isn’t in the approved locales (if provided)

Next steps

How is this guide?

requiresTranslation