globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Globalize cannot be instantiated after an error #903

Closed morwoen closed 4 years ago

morwoen commented 4 years ago

What happened

At the Localisation & Globalisation team at my company, we've decided to release an internal "test" locale (we called it xx-XX) to provide pseudo localisation for all other teams across. Since this locale is fictional there is no CLDR data for it. Our approach was to take es-ES and alter the data so that it acts as the new xx-XX locale.

During the process we've missed the likelySubtags object, meaning there was a mismatch

"main": {
  "xx-XX": {...}
},
"supplemental": {
  "likelySubtags": {
    "und": "es",
    "es-ES": "es"
  }
}

The error

Passing this data into a Globalize.load() causes no errors, however, instantiating a new instance new Globalize(locale) throws TypeError: Cannot read property '0' of undefined. Note that what locale is above doesn't matter. Locales with already loaded valid data can no longer be used.

We've traced the error to this file in CLDRjs. The error is thrown on line 23. It looks like it stores the data in a global variable and never clears the state.

Expected outcome

An error to be thrown only once by the Globalize.load() call. Alternatively, errors to be throws only when Globalize is instantiated with the locale which data cannot be made sense of.

Impact

The service that observed the error is using Globalize in its backend. This means that after an internal user (xx-XX is only an internal locale, no end-user can request it) tested the new locale end-users using actual locales started seeing errors due to the recurring exception thrown for valid locales.

PS. We are happy to contribute a fix, we want to first get the opinion/proposition of the maintainers :)

morwoen commented 4 years ago

Closing in favour of https://github.com/rxaviers/cldrjs/issues/69. That would need to be fixed first and then bumped in Globalize.