standardiseLocale
API reference for the GT standardiseLocale method
Overview
The standardizeLocale method standardises a BCP-47 locale code to ensure correct formatting and casing.
It converts locale codes to their canonical form, making them suitable for use with internationalisation APIs and ensuring consistency across your application.
Reference
Parameters
Prop
Type
Parameter description
| Parameter | Description |
|---|---|
locale | The BCP‑47 locale code to standardise. If not provided, the instance’s targetLocale is used. |
Returns
string - The standardised BCP-47 locale code, or empty string if the input is invalid
Throws
Error– if no locale is provided and the instance has notargetLocaleconfigured
Behaviour
Common formatting corrections applied:
en_US→en-US(underscore to hyphen)zh_cn→zh-CN(underscore to hyphen, casing)EN-gb→en-GB(language lowercase, region uppercase)Fr-ca→fr-CA(proper casing throughout)ja_jp→ja-JP(underscore and casing)
Examples
const gt = new GT({
sourceLocale: 'en',
targetLocale: 'es-ES'
});
// Standardise various locale formats
console.log(gt.standardizeLocale('en_us')); // "en-US"
console.log(gt.standardizeLocale('zh_cn')); // "zh-CN"
console.log(gt.standardizeLocale('EN-gb')); // "en-GB"
console.log(gt.standardizeLocale('fr-ca')); // "fr-CA"
// Already standardised locales pass through unchanged
console.log(gt.standardizeLocale('es-ES')); // "es-ES"
console.log(gt.standardizeLocale('ja-JP')); // "ja-JP"Notes
- Converts underscores to hyphens and normalises casing
- Returns an empty string for invalid locale codes
- Language codes are lowercase; region codes are uppercase
- Preserves locale extensions and variants when present
Next steps
- Validate standardised locales with
isValidLocale - Resolve canonical locales with
resolveCanonicalLocale - Get locale properties with
getLocaleProperties
How is this guide?