-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlocalescript.mjs
187 lines (163 loc) · 5.29 KB
/
localescript.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fs from 'fs';
import path from 'path';
import * as url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const chromeSourceDir = path.resolve(__dirname, 'chrome');
const localesPath = path.join(chromeSourceDir, '_locales/');
const combinedLocalesFile = path.join(__dirname, 'combined-locales.json');
const defaultLanguage = 'en';
function sortLocales(locales) {
const sortedLocales = new Map();
// Set english as the first locale
if (locales.has(defaultLanguage)) {
sortedLocales.set(defaultLanguage, locales.get(defaultLanguage));
}
Array.from(locales.keys()).sort().forEach((key) => {
if (key === defaultLanguage) {
return;
}
sortedLocales.set(key, locales.get(key));
});
return sortedLocales;
}
function getLocalesFromMultiPath() {
const locales = new Map();
const localesFolders = fs.readdirSync(localesPath);
for (const localeFolder of localesFolders) {
const messagesPath = path.join(localesPath, localeFolder, 'messages.json');
// check if file exists
if (!fs.existsSync(messagesPath)) {
continue;
}
const messages = JSON.parse(fs.readFileSync(messagesPath, 'utf8'));
const translationMap = new Map();
Object.keys(messages).forEach((key) => {
translationMap.set(key, messages[key]);
});
locales.set(localeFolder, translationMap);
}
return sortLocales(locales);
}
function getLocalesFromCombinedFile() {
if (!fs.existsSync(combinedLocalesFile)) {
return;
}
const locales = new Map();
const messages = JSON.parse(fs.readFileSync(combinedLocalesFile, 'utf8'));
Object.entries(messages).forEach(([translationKey, values]) => {
Object.entries(values).forEach(([locale, translation]) => {
if (locale.endsWith('_description')) {
return;
}
if (!locales.has(locale)) {
locales.set(locale, new Map());
}
const obj = {
message: translation,
};
if (Object.hasOwn(values, `${locale}_description`)) {
obj.description = values[`${locale}_description`];
} else if (locale === defaultLanguage && Object.hasOwn(values, 'description')) {
obj.description = values.description;
}
locales.get(locale).set(translationKey, obj);
});
});
return sortLocales(locales);
}
function saveLocalesToMultiPath(locales, whiteList) {
for (const [locale, translations] of locales.entries()) {
if (whiteList && !whiteList.includes(locale)) {
continue;
}
const messagesPath = path.join(localesPath, locale, 'messages.json');
const messages = {};
for (const [key, value] of translations.entries()) {
messages[key] = value;
}
fs.writeFileSync(messagesPath, JSON.stringify(messages, null, 4));
}
}
function saveLocalesToCombinedFile(locales, whiteList) {
const messages = {};
for (const [locale, translations] of locales.entries()) {
if (whiteList && !whiteList.includes(locale)) {
continue;
}
for (const [key, value] of translations.entries()) {
if (!Object.hasOwn(messages, key)) {
messages[key] = {};
}
messages[key][locale] = value.message;
if (Object.hasOwn(value, 'description')) {
if (locale === defaultLanguage) {
messages[key].description = value.description;
} else {
messages[key][`${locale}_description`] = value.description;
}
}
}
}
fs.writeFileSync(combinedLocalesFile, JSON.stringify(messages, null, 4));
}
function checkLocaleKeys(locales) {
const defaultLocale = locales.get(defaultLanguage);
const defaultLocaleKeys = new Set(defaultLocale.keys());
for (const [locale, translations] of locales.entries()) {
if (locale === defaultLanguage) {
continue;
}
const localeKeys = new Set(translations.keys());
const missingKeys = defaultLocaleKeys.difference(localeKeys);
if (missingKeys.size > 0) {
console.log(`Missing keys in ${locale}:`, Array.from(missingKeys));
}
const extraKeys = localeKeys.difference(defaultLocaleKeys);
if (extraKeys.size > 0) {
console.log(`Extra keys in ${locale}:`, Array.from(extraKeys));
}
}
}
// Get arguments
const args = process.argv.slice(2);
let combine = false;
let split = false;
let whiteList = null;
if (args.length > 0) {
for (let i = 0; i < args.length; i++) {
if (args[i] === '--whitelist') {
whiteList = args[i + 1].split(',');
i++;
} else if (args[i] === '--split') {
split = true;
} else if (args[i] === '--combine') {
combine = true;
} else {
console.log('Invalid argument:', args[i]);
process.exit(1);
}
}
}
if (combine && split) {
console.log('Cannot combine and split at the same time');
process.exit(1);
}
const localesCombined = getLocalesFromCombinedFile();
const localesMulti = getLocalesFromMultiPath();
console.log('Checking locale keys');
checkLocaleKeys(localesMulti);
checkLocaleKeys(localesCombined);
if (combine) {
console.log('Combining locales');
localesMulti.forEach((translations, locale) => {
localesCombined.set(locale, translations);
});
saveLocalesToCombinedFile(localesCombined, whiteList);
}
if (split) {
console.log('Splitting locales');
localesCombined.forEach((translations, locale) => {
localesMulti.set(locale, translations);
});
saveLocalesToMultiPath(localesMulti, whiteList);
}