forked from saleor/storefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateLocales.mjs
56 lines (45 loc) · 1.78 KB
/
updateLocales.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
// @ts-check
import asserts from "node:assert";
import Path from "node:path";
import Fs from "node:fs/promises";
/**
*
* @param {string} path
* @returns {Promise<any>}
*/
const readJson = async (path) => JSON.parse(await Fs.readFile(path, "utf8"));
const defaultLocalePath = (() => {
const [_node, _path, defaultLocalePathTmp] = process.argv;
asserts(defaultLocalePathTmp, "Please provide a path to the default language");
return Path.normalize(defaultLocalePathTmp);
})();
const localesDirPath = Path.dirname(defaultLocalePath);
const localesDir = await Fs.readdir(localesDirPath);
const otherLocalesPaths = localesDir
.map((locale) => Path.join(localesDirPath, locale))
.filter((path) => path !== defaultLocalePath);
const defaultLocale = await readJson(defaultLocalePath);
const defaultLocaleKeys = Object.keys(defaultLocale);
for (const otherLocalePath of otherLocalesPaths) {
console.log(`Processing ${otherLocalePath}…`);
const otherLocale = await readJson(otherLocalePath);
const missingKeys = defaultLocaleKeys.filter((key) => !otherLocale[key]);
const extraKeys = Object.keys(otherLocale).filter((key) => !defaultLocale[key]);
console.log(`Extra ${extraKeys.length} keys. Removing…`);
console.log(`Missing ${missingKeys.length} keys. Adding…`);
const newOtherLocale = Object.fromEntries(
[
...Object.entries(otherLocale)
// remove unused keys
.filter(([key]) => defaultLocaleKeys.includes(key)),
...missingKeys.map((key) => [
key,
{
...defaultLocale[key],
"@TODO": "Please translate this",
},
]),
].sort(([a], [b]) => defaultLocaleKeys.indexOf(a) - defaultLocaleKeys.indexOf(b))
);
await Fs.writeFile(Path.resolve(otherLocalePath), JSON.stringify(newOtherLocale, null, 2));
}