tinganho / node-accept-language

BCP47 language negotiation
MIT License
85 stars 15 forks source link

Doesn't choose matching language if region doesn't match #37

Closed gebbber closed 3 years ago

gebbber commented 3 years ago
const acceptLanguage = require('accept-language');

const avail = ['en', 'fr-CA'];
const langPref = 'fr';

acceptLanguage.languages(avail);
const chosenLang = acceptLanguage.get(langPref);

console.log(chosenLang); 

// Output: 'en'
tinganho commented 3 years ago

This is expected: https://github.com/tinganho/node-accept-language/blob/master/Tests/Test.ts#L96

You can think if someone requesting 'fr' that he wants a more generic 'fr'. 'fr-CA' is more specific and should fail(or take the default one).

We follow this standard: https://tools.ietf.org/html/rfc4647#section-3.4

Note one requesting for en-US will resolve to en, but one requesting for en will not resolve to en-US. One solution is to include fr in available languages.

We could probably make the lookup rule work in both directions in the future, since some people expect it that way. Though, I'm very constrained in time right now.