resolveCanonicalLocale
API reference for the GT resolveCanonicalLocale method
Overview
In the context of aliasing locales (e.g. cn → zh), the resolveCanonicalLocale method converts alias locale codes to their canonical BCP‑47 locale codes when a custom mapping is configured.
Reference
Parameters
Prop
Type
Parameter descriptions
| Parameter | Description |
|---|---|
locale | The alias locale code to resolve to its canonical form. If not provided, the instance’s targetLocale is used |
customMapping | Optional custom mapping to use instead of the instance’s mapping |
Returns
string - The canonical BCP‑47 locale code if a mapping exists; otherwise, the original locale code
Throws
Error– if no locale is provided and the instance has notargetLocaleconfigured
Examples
const gt = new GT({
sourceLocale: 'en',
customMapping: {
'cn': { code: 'zh', name: 'Mandarin' },
}
});
// Convert alias to canonical locale
const canonical = gt.resolveCanonicalLocale('cn');
console.log(canonical); // "zh"
// Regular BCP-47 codes pass through unchanged
const unchanged = gt.resolveCanonicalLocale('fr-FR');
console.log(unchanged); // "fr-FR"Notes
- Converts alias locale codes to canonical BCP‑47 locale codes
- Returns the original locale if no custom mapping exists
- Works with the
customMappingconfiguration from the GT constructor - No validation is performed on the input or output locale codes
Next steps
- Resolve aliases with
resolveAliasLocale - Validate locales with
isValidLocale - Standardise locale format with
standardizeLocale
How is this guide?