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

Unit formatting megabyte in Polish short form failed because cldr-data@36.0.1 has inconsistent data #932

Open compulim opened 2 years ago

compulim commented 2 years ago

Following packages are installed (latest as of now):

When using the following code to format megabytes in short form in Polish, it failed:

var Globalize = require('globalize');

Globalize.load(require('cldr-data').entireSupplemental());
Globalize.load(require('cldr-data').entireMainFor('pl'));
Globalize.loadTimeZone(require('iana-tz-data'));

console.log(Globalize('pl').unitFormatter('megabyte', { form: 'short' })(1));

The error is:

/home/johndoe/repos/experiment-globalize/node_modules/globalize/dist/globalize.js:73
        message = message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( name ) {
                          ^

TypeError: Cannot read properties of undefined (reading 'replace')
    at formatMessage (/home/johndoe/repos/experiment-globalize/node_modules/globalize/dist/globalize.js:73:20)
    at unitFormat (/home/johndoe/repos/experiment-globalize/node_modules/globalize/dist/globalize/unit.js:90:9)
    at unitFormatter (/home/johndoe/repos/experiment-globalize/node_modules/globalize/dist/globalize/unit.js:101:10)
    at Object.<anonymous> (/home/johndoe/repos/experiment-globalize/src/index.js:7:73)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

This is because the data in cldr-data@36.0.1 is inconsistent. Under node_modules/cldr-data/main/pl/units.json, the short.digital-megabyte only contains strings for plural form of "other". It is lacking strings for "one", "few" and "many".

Excerpt from pl/units.json showing the short form of digital-megabyte only has string for "other" plural form:

"digital-megabyte": {
  "displayName": "MB",
  "unitPattern-count-other": "{0} MB"
},

Excerpt from supplemental/plurals.json showing Polish has 4 plural forms of "one", "few", "many" and "other":

"pl": {
  "pluralRule-count-one": "i = 1 and v = 0 @integer 1",
  "pluralRule-count-few": "v = 0 and i % 10 = 2..4 and i % 100 != 12..14 @integer 2~4, 22~24, 32~34, 42~44, 52~54, 62, 102, 1002, …",
  "pluralRule-count-many": "v = 0 and i != 1 and i % 10 = 0..1 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 12..14 @integer 0, 5~19, 100, 1000, 10000, 100000, 1000000, …",
  "pluralRule-count-other": "   @decimal 0.0~1.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …"
},

However, when formatting 1.1, it can format it into "1,1 MB". When formatting 1 into long form, it can format it into "1 megabajt".

rxaviers commented 2 years ago

Thanks for filing the issue. I accept a PR to fallback to other (default) when the other forms aren't available.