i18next / i18next-http-middleware

i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.
MIT License
152 stars 28 forks source link

Detection regex fails with Spanish (Latin America) (es-419) #41

Closed Gidgidonihah closed 3 years ago

Gidgidonihah commented 3 years ago

🐛 Bug Report

Given a header that includes es-419 the detection regex will return a language code of es- with a query value of 1.

To Reproduce

Make a request using the Accept-Language header with es-419 lower in the stack and without another locale with query value of 1:

curl localhost:8080 -h "Accept-Language: pt-PT;q=0.9,es-419;q=0.8,es;q=0.7"

The detected language will be es-

Note that if you supply another q=1 value (pt-PT,pt;q=0.9,es-419;q=0.8,es;q=0.7) that value will be correctly picked, but the query value of es- will also (incorrectly) be 1 making the support cascade fail.

Expected behavior

es-419 is detected with a query value of 0.8

Notes

I narrowed it down to the regex on this line:

https://github.com/i18next/i18next-http-middleware/blob/7f5a3eeff9fccb95f09a9ec80c720e3f8f5e0f4d/lib/languageLookups/header.js#L17

I lucked (good or bad) into finding this because I:

  1. was testing fallback with i18next settings of supportedLngs and nonExplicitSupportedLngs
    • In this case testing that I didn't support Portuguese at all and expected to see Italian (it) as the fallback language, but surprise it was Spanish
    • Using the header pt-PT,pt;q=0.9,it-IT;q=0.8,it;q=0.7,en-US;q=0.6,en;q=0.5,es-419;q=04,es;q=0.3
  2. happened to pick Spanish (Latin America) as the specific version of Spanish with which to test.

My Environment

Not particularly relevant, but:

adrai commented 3 years ago

Would you like to send a Pull Request to address this? Remember to add unit tests.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

orlando commented 11 months ago

[!IMPORTANT]
For anyone else looking at this issue, this was fixed on v3.4.1