Utility FunctionsLocales

determineLocale

API Reference for the determineLocale function

Overview

The determineLocale function determines 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 first exact or language match from approved list
  • Respects preference order in input array
  • Returns undefined when no match found
  • Essential for web application locale negotiation

Next Steps

How is this guide?

determineLocale