GT ClassMethodsLocales

isValidLocale

API reference for the GT isValidLocale method

Overview

The isValidLocale method verifies whether a locale code is correctly formatted and recognised as a valid BCP‑47 locale. It checks locale structure, language recognition, and region/script validity using the Intl APIs, with support for custom locale mappings.


Reference

Parameters

Prop

Type

Parameter descriptions

ParameterDescription
localeBCP‑47 locale code to validate. If not provided, uses the instance’s targetLocale.
customMappingOptional custom mapping to check for additional valid locales. If not provided, uses the instance’s customMapping.

Returns

booleantrue if the locale is valid, false otherwise

Throws

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

Examples

const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'es'
});

const isValid = gt.isValidLocale('en-US');
console.log(isValid); // true

const isInvalid = gt.isValidLocale('invalid-locale');
console.log(isInvalid); // false

Notes

  • Performs comprehensive BCP‑47 locale validation using browser Intl APIs
  • Custom mapping locales always validate as true (bypass standard validation)
  • Supports private‑use language codes (qaa–qtz)
  • Returns false for malformed or unrecognised locale codes

Next steps

How is this guide?

isValidLocale