florrain / locale

Browser locale negotiation for node.js
MIT License
257 stars 36 forks source link

Language without country doesn't match language with country #13

Closed stuartf closed 10 years ago

stuartf commented 10 years ago

According to rfc 2616 section 14.4 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

A language-range matches a language-tag if it exactly equals the tag, or if it exactly equals a prefix of the tag such that the first tag character following the prefix is "-"

Thus I would expect

supported = new locale.Locales(["es_ES"]);
new locale.Locales('es').best(supported).toString();

To give me es_ES but it gives me en_US because that's locale.Locale.default

stuartf commented 10 years ago

I wanted to make sure this doesn't break the quality ordering before merging, in node I can do:

var supported = new locale.Locales(["en-US", "en", "ja", "da-DK"]);
new locale.Locales("ja;q=.5, da").best(supported).toString();

and get da_DK which is correct. But when I add a test that does the same thing:

 (next) ->
 http.get port: 8000, headers: "Accept-Language": "ja;q=.8, da", (res) ->
   assert.equal(
     res.headers["content-language"]
     "da_DK"
     "Countryless request falls back to countried language even when there's a lower quality exact ma  tch"
   )

   do next

The test suite suddenly takes 3 minutes to run...

stuartf commented 10 years ago

that's interesting, the test passes within the 2 second mocha timeout on travis, but on my local machine I had to push the timeout up until:

✓ should fallback to a country specific language even when there's a lower quality exact match (120029ms)
stuartf commented 10 years ago

found it, have to explicitly close the connection