-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbuild-locales.js
132 lines (102 loc) · 4.57 KB
/
build-locales.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const likelySubtags = require("cldr-core/supplemental/likelySubtags.json");
const currencyData = require("cldr-core/supplemental/currencyData.json");
const weekData = require("cldr-core/supplemental/weekData.json");
const fs = require('fs');
const path = require('path');
const jsonNameRegex = /"([$A-Z\_a-z][$A-Z\_a-z0-9\\.]*)":/g;
const LOCALES_PATH = path.join(process.cwd(), 'node_modules', 'cldr-localenames-full', 'main');
module.exports = {};
const EXCLUDE = {
root: true
};
const NO_CURRENCY = {
'es-419': true // latin america. not sure what to use here
};
const toJSObject = module.exports.toJSObject = (obj) => {
return JSON.stringify(obj, null, 4).replace(jsonNameRegex, '$1:');
};
const defaultTemplate = (data) => {
return `const data = ${ toJSObject(data) };
export default data;
`};
const localeInfo = (info) => {
return {
name: info.name,
likelySubtags: info.likelySubtags,
identity: info.identity,
territory: info.territory
};
};
const loadLocale = (name, intl) => {
const numbers = require(`cldr-numbers-full/main/${ name }/numbers.json`);
const currencies = require(`cldr-numbers-full/main/${ name }/currencies.json`);
const calendar = require(`cldr-dates-full/main/${ name }/ca-gregorian.json`);
const timeZoneNames = require(`cldr-dates-full/main/${ name }/timeZoneNames.json`);
const dateFields = require(`cldr-dates-full/main/${ name }/dateFields.json`);
const units = require(`cldr-units-full/main/${ name }/units.json`);
intl.load(numbers, currencies, calendar, timeZoneNames, dateFields, units);
};
module.exports.buildLocales = (intl, { contentTemplate = defaultTemplate, extension = 'js', destFolder = 'locales' }) => {
destFolder = path.join(process.cwd(), destFolder);
if (!fs.existsSync(destFolder)){
fs.mkdirSync(destFolder);
}
intl.load(likelySubtags, currencyData, weekData);
const data = intl.cldr;
const likelySubtagsData = data.supplemental.likelySubtags;
const supplementalCurrency = data.supplemental.currencyData;
const locales = fs.readdirSync(LOCALES_PATH);
for (let idx = 0; idx < locales.length; idx++) {
const name = locales[idx];
if (!EXCLUDE[name]) {
const localePath = path.join(destFolder, name);
loadLocale(name, intl);
intl.firstDay(name);
intl.weekendRange(name);
if (!NO_CURRENCY[name]) {
intl.localeCurrency(name);
}
if (!fs.existsSync(localePath)){
fs.mkdirSync(localePath);
}
const localeData = data[name];
const language = name.split('-')[0];
localeData.likelySubtags = {};
const localeCurrency = localeData.numbers.localeCurrency;
let localeCurrencyData;
if (likelySubtagsData[language]) {
localeData.likelySubtags[language] = likelySubtagsData[language];
}
if (likelySubtagsData[name]) {
localeData.likelySubtags[name] = likelySubtagsData[name];
}
if (supplementalCurrency.fractions[localeCurrency]) {
localeData.currencyData = localeCurrencyData = {
[localeData.numbers.localeCurrency]: supplementalCurrency.fractions[localeCurrency]
};
}
delete localeData.identity.version;
fs.writeFileSync(path.join(localePath, `all.${ extension }`), contentTemplate(localeData));
const currencies = Object.assign(localeInfo(localeData), {
numbers: {
currencies: localeData.numbers.currencies,
localeCurrency: localeData.numbers.localeCurrency
}
});
delete localeData.numbers.currencies;
delete localeData.numbers.localeCurrency;
delete localeData.currencyData;
const numbers = Object.assign(localeInfo(localeData), {
numbers: localeData.numbers,
currencyData: localeCurrencyData
});
const calendar = Object.assign(localeInfo(localeData), {
calendar: localeData.calendar,
firstDay: localeData.firstDay
});
fs.writeFileSync(path.join(localePath, `currencies.${ extension }`), contentTemplate(currencies));
fs.writeFileSync(path.join(localePath, `numbers.${ extension }`), contentTemplate(numbers));
fs.writeFileSync(path.join(localePath, `calendar.${ extension }`), contentTemplate(calendar));
}
}
};