Utility FunctionsLocales

determineLocale

API reference for the determineLocale function

Overview

The determineLocale function selects the best‑matching locale from approved locales based on user preferences, without requiring a GT class instance.


Reference

Parameters

Prop

Type

Returns

string | undefined - Best‑matching locale, or undefined if no match


Examples

Content Negotiation

import { determineLocale } from 'generaltranslation';

const approvedLocales = ['en-US', 'es-ES', 'fr-FR', 'de-DE'];

// Exact match
console.log(determineLocale('en-US', approvedLocales)); // 'en-US'

// Language fallback
console.log(determineLocale('en-GB', approvedLocales)); // 'en-US'

// Multiple preferences
console.log(determineLocale(['fr-CA', 'es-MX'], approvedLocales)); // 'es-ES'

// No match
console.log(determineLocale('it-IT', approvedLocales)); // undefined

Notes

  • Implements intelligent locale negotiation
  • Returns the first exact or language match from the approved list
  • Respects the preference order in the input array
  • Returns undefined when no match is found
  • Essential for locale negotiation in web applications

Next steps

How is this guide?

determineLocale