x/text/language: Match detects wrong language #49176
What version of Go are you using (
The text was updated successfully, but these errors were encountered:
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.
🇫🇷 🇬🇧 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 Guidance
I 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 Steps
While 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.