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 605 forks source link

TypeError: Cannot read property '0' of undefined #851

Closed sshaw closed 5 years ago

sshaw commented 5 years ago

Versions:

/tmp/globalize-test >node -v
v9.5.0
/tmp/globalize-test >npm ls
/private/tmp/globalize-test
└─┬ globalize@1.4.0
  └── cldrjs@0.5.0

Code:

const Globalize = require("globalize");
Globalize.loadMessages({
  pt: {
    greetings: {
      hello: "Olá",
      bye: "Tchau"
    }
  }
});

Globalize( "pt" ).formatMessage( "greetings/hello" );

I get the following:

/tmp/globalize-test >node i18n-test.js
/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:185
            language = maxLanguageId[ 0 ],
                                    ^

TypeError: Cannot read property '0' of undefined
    at coreRemoveLikelySubtags (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:185:28)
    at /private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:300:17
    at Array.forEach (<anonymous>)
    at arrayForEach (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:272:17)
    at bundleLookup (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:296:4)
    at Cldr.init (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:620:12)
    at Cldr.init (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr/event.js:554:13)
    at Cldr.init (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr/supplemental.js:92:13)
    at new Cldr (/private/tmp/globalize-test/node_modules/cldrjs/dist/cldr.js:545:8)
    at alwaysCldr (/private/tmp/globalize-test/node_modules/globalize/dist/globalize.js:319:55)
sshaw commented 5 years ago

It's the Globalize( "pt" ) or Globalize.locale( "pt" ) call that causes this.

sshaw commented 5 years ago

Problem is lack of CLDR data (though this is still a bug, IMHO). Once one calls:

Globalize.load( require( "cldr-data" ).entireSupplemental() );

(or similar) this error is not encountered.

theodesp commented 5 years ago

You only need to load the cldr/supplemental/likelySubtags to fix that

rxaviers commented 5 years ago

You only need to load the cldr/supplemental/likelySubtags to fix that

:+1:

The action item is to improve https://github.com/rxaviers/cldrjs error handling.

rxaviers commented 5 years ago

I'm closing this issue. PR in cldrjs is welcome. Thanks