-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmigrateIntl.mjs
147 lines (136 loc) · 4.03 KB
/
migrateIntl.mjs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import fs from 'fs-extra';
import glob from 'fast-glob';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
let mapToNewKeys = {
'inlinealert.info': 'inlinealert.informative',
};
let stringsToAllow = new Set([
'actionbar.actions',
'actionbar.actionsAvailable',
'actionbar.clearSelection',
'actionbar.selected',
'actionbar.selectedAll',
'breadcrumbs.more',
'button.pending',
'menu.moreActions',
'dialog.alert',
'contextualhelp.info',
'contextualhelp.help',
'dialog.dismiss',
'dropzone.replaceMessage',
'label.(required)',
'label.(optional)',
'inlinealert.informative',
'inlinealert.negative',
'inlinealert.notice',
'inlinealert.positive',
'picker.placeholder',
'slider.minimum',
'slider.maximum',
'table.loading',
'table.loadingMore',
'table.sortAscending',
'table.sortDescending',
'table.resizeColumn',
'tag.actions',
'tag.showAllButtonLabel',
'tag.hideButtonLabel',
'tag.noTags'
]);
function prefixKeys(obj, prefix) {
let newObj = {};
for (let [key, value] of Object.entries(obj)) {
let newKey = `${prefix}.${key}`;
if (mapToNewKeys[newKey]) {
newKey = mapToNewKeys[newKey];
}
newObj[newKey] = value;
}
return newObj;
}
// remove properties from an object if they are not in the set of strings to allow
function filterKeys(obj) {
let newObj = {};
for (let [key, value] of Object.entries(obj)) {
if (stringsToAllow.has(key)) {
newObj[key] = value;
}
}
return newObj;
}
let rspIntlPackages = glob.sync('../packages/**/intl/en-US.json', {cwd: __dirname, absolute: true});
let dest = path.join(__dirname, '../packages/@react-spectrum/s2/intl')
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, {recursive: true});
}
let packs = new Map();
for (let intlPkg of rspIntlPackages) {
if (intlPkg.includes('react-aria-components') || intlPkg.includes('react-stately') || intlPkg.includes('react-aria')) {
continue;
}
let matches = intlPkg.match(/packages\/(.*)\/intl\/en-US.json/);
// use the match as a package name and copy
// the entire intl directory to the @react-spectrum/s2/messages directory
let pkgName = matches[1].split('/')[1];
let locales = glob.sync(path.join(path.dirname(intlPkg), '*.json'));
for (let locale of locales) {
let localeName = path.basename(locale).split('.')[0];
let messages = fs.readJsonSync(locale);
if (!packs.has(localeName)) {
packs.set(localeName, filterKeys(prefixKeys(messages, pkgName)));
} else {
let existing = packs.get(localeName);
let duplicates = new Map();
for (let [key, value] of Object.entries(messages)) {
duplicates.set(key, {locale, value});
}
for (let [key, value] of Object.entries(existing)) {
if (duplicates.has(key)) {
if (value !== duplicates.get(key).value) {
console.log(`Key collision: ${key} - ${duplicates.get(key).value} - ${value}\nfrom ${duplicates.get(key).locale} - ${locale}`);
}
}
}
let concat = {...existing, ...filterKeys(prefixKeys(messages, pkgName))};
packs.set(localeName, concat);
}
}
// let dest = path.join(__dirname, '../packages/@react-spectrum/s2/messages', pkgName);
// // make sure the destination directory exists
// fs.copySync(path.dirname(intlPkg), dest);
}
console.log(packs)
for (let [key, value] of packs) {
let dest = path.join(__dirname, '../packages/@react-spectrum/s2/intl', `${key}.json`);
if (fs.existsSync(dest)) {
let translations = fs.readJsonSync(dest);
for (let [name, message] of Object.entries(translations)) {
if (!value[name]) {
value[name] = message;
}
}
}
fs.writeFile(dest, JSON.stringify(value, null, 2));
}
/**
* Removed strings
* actionbar
* autocomplete
* button
* calendar
* card
* color
* combobox
* datepicker
* list
* listbox
* pagination
* searchwithin
* steplist
* table - coming soon
* toast
*
*/