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
| Parameter | Description |
|---|---|
locale | BCP‑47 locale code to validate. If not provided, uses the instance’s targetLocale. |
customMapping | Optional 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 notargetLocaleconfigured
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); // falseNotes
- 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
falsefor malformed or unrecognised locale codes
Next steps
How is this guide?