GT ClassMethodsLocales

isValidLocale

API Reference for the GT isValidLocale method

Overview

The isValidLocale method validates whether a locale code is properly formatted and recognized as a valid BCP-47 locale. It checks locale structure, language recognition, and regional/script validity using the Intl APIs with support for custom locale mappings.


Reference

Parameters

Prop

Type

Parameters Description

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

boolean - true 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 unrecognized locale codes

Next Steps

How is this guide?

isValidLocale