|
| 1 | +import { fileURLToPath } from 'url'; |
| 2 | +import { resolve } from 'path'; |
| 3 | +import fs from 'fs/promises'; |
| 4 | + |
| 5 | +// There is an issue in the Unicode CLDR database (v36): |
| 6 | +// |
| 7 | +// - Polish has 4 different plural types: "one", "few", "many", "other" |
| 8 | +// - However, some units, say "short/digital-kilobyte", only have "other" defined |
| 9 | +// - When we localize 1024 (number) into kilobytes, it use the "one" type |
| 10 | +// - Since "short/digital-kilobyte/one" is not defined in the database, `globalize` throw exception |
| 11 | +// |
| 12 | +// In all of our supported languages, we also observed the same issue in Portuguese as well. |
| 13 | +// |
| 14 | +// As a hotfix, we are patching the Unicode CLDR database for all `[long/short/narrow]/digital-*` rules to make sure it include all plurals needed for that language. |
| 15 | +// |
| 16 | +// For a long term fix, we should move forward to a newer version of CLDR database, which is outlined in https://github.com/rxaviers/cldr-data-npm/issues/78. |
| 17 | + |
| 18 | +let FORBIDDEN_PROPERTY_NAMES; |
| 19 | + |
| 20 | +function getForbiddenPropertyNames() { |
| 21 | + return ( |
| 22 | + FORBIDDEN_PROPERTY_NAMES || |
| 23 | + (FORBIDDEN_PROPERTY_NAMES = Object.freeze( |
| 24 | + Array.from( |
| 25 | + new Set([ |
| 26 | + // As-of writing, `Object.prototype` includes: |
| 27 | + // __defineGetter__ |
| 28 | + // __defineSetter__ |
| 29 | + // __lookupGetter__ |
| 30 | + // __lookupSetter |
| 31 | + // __proto__ |
| 32 | + // constructor |
| 33 | + // hasOwnProperty |
| 34 | + // isPrototypeOf |
| 35 | + // propertyIsEnumerable |
| 36 | + // toLocaleString |
| 37 | + // toString |
| 38 | + // valueOf |
| 39 | + ...Object.getOwnPropertyNames(Object.prototype), |
| 40 | + |
| 41 | + 'prototype' |
| 42 | + ]) |
| 43 | + ) |
| 44 | + )) |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function isForbiddenPropertyName(propertyName) { |
| 49 | + return getForbiddenPropertyNames().includes(propertyName); |
| 50 | +} |
| 51 | + |
| 52 | +function toDist(filename) { |
| 53 | + if (filename.includes('..')) { |
| 54 | + throw new Error('Filename cannot contains "..".'); |
| 55 | + } |
| 56 | + |
| 57 | + return resolve(fileURLToPath(import.meta.url), '../../dist/', filename); |
| 58 | +} |
| 59 | + |
| 60 | +// ESLint "wrap-iife" rule is conflicting with Prettier. |
| 61 | +// eslint-disable-next-line wrap-iife |
| 62 | +(async function () { |
| 63 | + const plurals = JSON.parse(await fs.readFile(toDist('supplemental/plurals.json'), 'utf8')); |
| 64 | + |
| 65 | + const languagePlurals = new Map(); |
| 66 | + |
| 67 | + Object.entries(plurals.supplemental['plurals-type-cardinal']).forEach(([language, pluralsTypeCardinal]) => { |
| 68 | + const plurals = ['other']; |
| 69 | + |
| 70 | + languagePlurals.set(language, plurals); |
| 71 | + |
| 72 | + if (!(`pluralRule-count-other` in pluralsTypeCardinal)) { |
| 73 | + throw new Error(`Language ${language} does not have plural type "other".`); |
| 74 | + } |
| 75 | + |
| 76 | + ['zero', 'one', 'two', 'few', 'many'].forEach(pluralType => { |
| 77 | + `pluralRule-count-${pluralType}` in pluralsTypeCardinal && plurals.push(pluralType); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + const patchedLanguages = []; |
| 82 | + |
| 83 | + await Promise.all( |
| 84 | + Array.from(languagePlurals.entries()).map(async ([language, supportedPluralTypes]) => { |
| 85 | + if (!/^[\w-]+$/u.test(language) && isForbiddenPropertyName(language)) { |
| 86 | + throw new Error(`Invalid language code "${language}".`); |
| 87 | + } |
| 88 | + |
| 89 | + let units; |
| 90 | + |
| 91 | + try { |
| 92 | + units = JSON.parse(await fs.readFile(toDist(`main/${language}/units.json`), 'utf8')); |
| 93 | + } catch (err) { |
| 94 | + if (err.code === 'ENOENT') { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + throw err; |
| 99 | + } |
| 100 | + |
| 101 | + let numFound = 0; |
| 102 | + |
| 103 | + ['long', 'short', 'narrow'].forEach(form => { |
| 104 | + Object.entries(units.main[language].units[form]).forEach(([unitName, entry]) => { |
| 105 | + if (!unitName.startsWith('digital-')) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if ('unitPattern-count-other' in entry) { |
| 110 | + const { 'unitPattern-count-other': other } = entry; |
| 111 | + |
| 112 | + supportedPluralTypes.forEach(pluralType => { |
| 113 | + const name = `unitPattern-count-${pluralType}`; |
| 114 | + |
| 115 | + if (!(name in entry)) { |
| 116 | + entry[name] = other; |
| 117 | + numFound++; |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + if (numFound) { |
| 125 | + patchedLanguages.push(`${language} (${numFound} issues)`); |
| 126 | + |
| 127 | + // eslint-disable-next-line no-magic-numbers |
| 128 | + await fs.writeFile(toDist(`main/${language}/units.json`), JSON.stringify(units, null, 2)); |
| 129 | + } |
| 130 | + }) |
| 131 | + ); |
| 132 | + |
| 133 | + // We are display output in CLI. |
| 134 | + // eslint-disable-next-line no-console |
| 135 | + console.log(`Patched ${patchedLanguages.length} languages: ${patchedLanguages.join(', ')}.`); |
| 136 | +})(); |
0 commit comments