florrain / locale

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

Match country-specific language codes when separator is different #24

Closed mlmorg closed 10 years ago

mlmorg commented 10 years ago

My last PR (#23) introduced a regression whereby two locales with the same language and country codes but a different separator ("-" vs. "_") would not correctly match. An example of how this can happen:

var userLocales = new locale.Locales(['en_US']);
var supportedLocales = new locale.Locales(['en', 'en-US']);
var bestLocale = userLocales.best(supportedLocales).toString(); // returns `en` instead of `en-US`

This happened because the best() method was matching locales based on their toString() values, which now returns the original code (instead of one always separated with an "_").

This PR fixes that bug by introducing a normalized attribute on the Locale specifically for use when matching locales using best(). In addition, I altered the index() method on Locales to map the locale code to the array index at which that Locale object is located in the supported locales array passed in to best() so that we could return the original Locale object passed in.

Let me know if you have any questions and sorry for the regression.

jed commented 10 years ago

no worries, bumped and pushed.

mlmorg commented 10 years ago

Thanks!