florrain / locale

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

Can't determine between default or match of same locale #27

Closed freeqaz closed 8 years ago

freeqaz commented 10 years ago

If I do the following:

locale.Locale.default = 'en-US'
locales = new locale.Locales(supportedLocales);
locales.best('en_US')

I get returned 'en-US', as I expect.

The issue here is if I do something like...

locale.Locale.default = 'en-US'
locales = new locale.Locales(supportedLocales);
locales.best('zz-ZZ')

In this case, I get returned 'en-US' but that's because it didn't find a match. There is no way for me to know that I defaulted, unless I attempt to hack around it like so:

locale.Locale.default = 'unknown'
locales = new locale.Locales(supportedLocales);
locales.best('zz-ZZ')

This will return 'unknown' to me. Which I can then use to identify what happened. When coupled with this issue though -> https://github.com/jed/locale/issues/26 Now it's much not possible to understand the flow when the default is changed externally. I've got a PR open that has a possible fix, but I'm not crazy about it. There's a lot of logic for a simple goal. https://github.com/jed/locale/pull/25 Any ideas?

freeqaz commented 10 years ago

A solution that I like is this:

locales = new locale.Locales(supportedLocales, 'unknown');
locales.best('zz-ZZ')

That would return 'unknown' and I wouldn't have to worry about external things modifying my state. It still doesn't help me if I set my default to 'en-US' and pass in 'en-US' though. It's useful to have a variable to tells me what happened exactly.

florrain commented 8 years ago

Released version 0.1.0 that includes the fix for this.