-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
x/text/language: Match detects wrong language #49176
Comments
cc @mpvl |
I tried to look into this. I wasn't able to get to the root cause today, but here's what I have found so far: Some background Context
For the working as expected case with
For the buggy case with
|
Strangely enough, I think this is working as intended. @mpvl should definitely check my work though 😅 Here is a playground with my examples: https://play.golang.org/p/D4ucCTJ9LfY Key assumptions the algorithm is making:1️⃣ Most people do not speak multiple languages equally. A heavy emphasis is put on finding the first exact language match and "pinning" that language. Docs about assumption 1.
2️⃣ If a person lists the dialects they know and the languages are not continuous, then it is assumed they speak all of those languages equally well and it will not give extra preference to the first exact language match it finds. For example, if your list of desired language tags are:
❓ You may be wondering: "But what about those q values I set???" (At least that is what I was wondering.) It turns out those values are only for ordering the desired language tags in |
Thanks for your reply & investigation.
|
Hi @eyudkin, 🇫🇷 🇬🇧 UI
I agree that this sounds like a very bad user experience and an unexpected bug. 🔢 q values
Go does take q values into account, but just not in the way that I (and maybe you?) assumed when using them initially. According to rfc7231 section 5.3.5:
The q values are for ordering the preferred language tags correctly, which is what go does when you use ✏️ RFC GuidanceI found this quote also in rfc7231 section 5.3.5 that seems very related to your example:
However, I am guessing that you are the server and you do not control the client and so you cannot change which desired language tags are being sent by the user. 🥾 Next StepsWhile I agree that the logic is not initially intuitive for this case, it is how the algorithm is purposefully written. I am not an expert in this area, just some one who was interested in digging into this case and learning more. While you can argue with @mpvl about changing the algorithm, I doubt that will happen quickly or at all. I think the best and quickest fix is to change your code to detect the language once so that there is not a mismatch between the frontend/backend. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Im trying to resolve a locale language from the "accept-language" http header:
en-GB;q=1.0, fr-DE;q=0.9, fr-CA;q=0.8, en-DE;q=0.7
by using
golang.org/x/text/language
:What did you expect to see?
en-u-rg-gbzzzz
What did you see instead?
fr-u-rg-dezzzz
Other comments
Actually this is reproducible at playground: https://play.golang.org/p/puiT36mYjiU
Please notice that english lang has been detected correctly if we remove
en-DE
from the end of the string:en-GB;q=1.0, fr-DE;q=0.9, fr-CA;q=0.8
(I dont understand how adding ofher low-priority language affects french lang detection but it works)Realizations in other programming languages
I wrote similar code for other languages to compare the outputs and both of them detected "en" instead of "fr" so probably we have a bug in
golang.org/x/text/language
package.javascript
java
The text was updated successfully, but these errors were encountered: