Skip to content

Function: negotiateLocale() ​

negotiateLocale(requested, supported, defaultLocale): string

Choose the supported language for the requested ones. Each requested language is tried in turn, preferred first, and the first match wins: the same tag (case-insensitive), then its language alone (en-GB matches en), then another tag of its language (pt-PT matches pt-BR, the first one in the order of supported). The invalid requested tags are skipped.

Parameters ​

requested ​

string[]

The requested language tags, preferred first (from parseAcceptLanguage or environmentLocales).

supported ​

string[]

The supported language tags.

defaultLocale ​

string

The language returned when none of the requested ones matches.

Returns ​

string

The matching tag as written in supported, or defaultLocale.

Example ​

typescript
import {negotiateLocale} from 'lakutata/com/i18n'

console.log(negotiateLocale(['pt-PT', 'en'], ['en', 'pt-BR'], 'en')) //pt-BR
console.log(negotiateLocale(['de'], ['en', 'zh-CN'], 'en')) //en

Released under the MIT License.