Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.pick ignores quality? #34

Open
mrkurt opened this issue Dec 5, 2018 · 8 comments
Open

.pick ignores quality? #34

mrkurt opened this issue Dec 5, 2018 · 8 comments

Comments

@mrkurt
Copy link

mrkurt commented Dec 5, 2018

I'm trying to use incoming HTTP headers to serve up the most appropriate translation of a site, and it seems like .pick is exactly what I need, but it's not respecting the quality value of headers. When I run this:

parser.pick(["en", "nl"], "en-GB,nl;q=0.5"

I would expect it to return en, but it's giving me nl. Is this on purpose or would you like a PR to fix it?

@matteofigus
Copy link
Contributor

matteofigus commented Dec 7, 2018

This is correct. In the pick, you should use the full languages you support, like ['en-GB', 'nl-NL']. In that scenario, the picked would be en-GB. If you want to have just en check the loose option in the README.

@mrkurt
Copy link
Author

mrkurt commented Dec 7, 2018

Ah! loose actually returns the same, presumably because nl is an exact match?

@Bramzor
Copy link

Bramzor commented Jul 13, 2019

I would advise to put full locales and nog languages in the array of options. Together with loose, you will in that case always get the full locale and will solve your issue:

parser.pick(["en-US", "nl-BE"], "en-GB,nl;q=0.5"

Should in this case indeed return en-US as language matches. Using split you can extract the language from the locale.

@bamapookie
Copy link

I added some tests for the readme examples and corrected the readme based on the results here: #35

@FMCorz
Copy link

FMCorz commented Feb 11, 2021

I ran into specificity issues as well.

acceptLanguageParser.pick(['en-GB', 'es', 'en'], 'en')

The expected result, to me, would be en as it's a perfect match, but instead if returns en-GB.

@alex-piccione
Copy link

alex-piccione commented Jan 28, 2022

parser.pick(["en", "nl"], "en-GB,nl;q=0.5"

I think It is correct to return "nl" for this scenario.
"nl" and "en-GB" have the same priority (q=0.5), so why a non-matching locale should be preferred to a matching one?
The user expects "nl" or "en-GB", why give him "en" instead of something that is what they asks for? ("en" is also much more like en-US than en-GB in terms of date-time formats).
Typical Accept-Language values are:

  • fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
  • en-US,en;q=0.5

"en-GB,nl" is like the first example replacing "fr" with "nl", so why it should provide "fr" if "en" or "de" are accepted (also within the same q in your scenario)?
In that scenario, the missing "en" seems to indicate it really doesn't want it.

@bamapookie
Copy link

bamapookie commented Jan 28, 2022

I believe in this case, en-GB has a weight of 1. When the ;q= is missing, the q value is 1.

See here: https://developer.mozilla.org/en-US/docs/Glossary/Quality_values

Since this is a strict search, though, nl wins because that value is in the supported language array.

@danjunger
Copy link

danjunger commented Feb 24, 2022

pick does ignore quality though. if you look at the source, there's literally nothing in there that even looks at the quality number.

// en-GB: 0.2, en-US: 0.4
// this returns en-GB because it's the first match
parser.pick(['en-GB','fr-FR','en-US'], 'zh-CN,zh;q=0.8,en;q=0.6,en-US;q=0.4,en-GB;q=0.2')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants