GT ClassMethodsLocales

requiresTranslation

API Reference for the GT requiresTranslation method

Overview

The requiresTranslation method determines whether translation is needed based on source and target locales. It checks if the source content needs to be translated by comparing locale codes and considering approved locale list.


Reference

Parameters

Prop

Type

Parameters Description

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

boolean - true if 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

  • Considers locale language families, not just exact matches
  • Respects approved locale lists
  • Returns false when target locale is not in approved locales (if provided)

Next Steps

How is this guide?

requiresTranslation