determineLocale
API reference for the GT determineLocale method
Overview
The determineLocale method selects the best-matching locale from a list of approved locales based on user preferences.
It performs locale negotiation to find the most suitable locale when exact matches aren’t available.
const gt = new GT({
sourceLocale: 'en-GB',
locales: ['en-GB', 'es-ES', 'fr-FR', 'de-DE']
});
// Exact match
console.log(gt.determineLocale('en-GB')); // 'en-GB'
// Language fallback
console.log(gt.determineLocale('en-AU')); // 'en-GB' (closest English variant)
// Multiple preferences
console.log(gt.determineLocale(['fr-CA', 'es-MX', 'en-GB'])); // 'es-ES' (closest Spanish variant)
// No match
console.log(gt.determineLocale('it-IT')); // undefinedReference
Parameters
Prop
Type
Returns
string | undefined - Best‑matching locale, or undefined if no match is found
Examples
User locale negotiation
const gt = new GT({
sourceLocale: 'en-US',
locales: ['en-US', 'en-GB', 'es-ES', 'fr-FR']
});
// Simulate the browser’s Accept-Language header
const userPreferences = ['fr-CA', 'en-GB', 'en'];
const bestMatch = gt.determineLocale(userPreferences);
console.log(bestMatch); // 'fr-FR' based on preference orderNotes
- Returns the first exact match from approved locales
- Falls back to language matches when the exact region isn’t available
- Respects the preference order in the input array
- Returns undefined when no suitable match is found
- Essential for implementing locale negotiation in web applications
Next steps
- Check translation needs with
requires-translation - Compare languages with
is-same-language
How is this guide?