forked from vendure-ecommerce/storefront-qwik-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-i18n.ts
101 lines (79 loc) · 2.86 KB
/
sort-i18n.ts
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
/**
* From https://gist.github.com/DustinJSilk/b928a228b15da5b87df7c259ffdf2b0d
*/
import { readdir, readFile, writeFile } from 'fs/promises';
import { join, resolve } from 'path';
async function getJsonFiles(source: string) {
const files = await readdir(source, { withFileTypes: true });
return files
.filter((file) => !file.isDirectory() && file.name.includes('.json'))
.map((file) => ({ name: file.name, path: join(source, file.name) }));
}
async function getFilesRecursively(dir: string): Promise<string[]> {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(
dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFilesRecursively(res) : res;
})
);
return Array.prototype.concat(...files);
}
/**
* Objects can't be sorted in JS so it must sorted as a string and then repaired
*/
async function sortTranslations(file: string, valueOrder?: string[], keyOrder?: string[]) {
const locale = file.match(/\.(.*)\./)![1];
console.log(`sorting ${locale}`);
const text = await readFile(file, { encoding: 'utf-8' });
const translations = text.match(/translations":\s{\n((.|\n)*)\n\s*}\n}/)![1];
const list = translations.split(',\n');
list.sort((a, b) => {
let aIndex = 0;
let bIndex = 0;
if (valueOrder) {
const aVal = a.match(/:\s"(.*)"/)![1];
const bVal = b.match(/:\s"(.*)"/)![1];
aIndex = valueOrder.findIndex((val) => val === aVal);
bIndex = valueOrder.findIndex((val) => val === bVal);
} else if (keyOrder) {
const aKey = a.match(/"(.*)":/)![1];
const bKey = b.match(/"(.*)":/)![1];
aIndex = keyOrder.findIndex((val) => val === aKey);
bIndex = keyOrder.findIndex((val) => val === bKey);
}
return aIndex - bIndex;
});
const output = text.replace(translations, list.join(',\n'));
await writeFile(file, output, { encoding: 'utf-8' });
return list;
}
/** Returns the order that each localization is found in the src folder */
async function getSortOrder() {
const dir = join(process.cwd(), 'src');
const files = await getFilesRecursively(dir);
const out: string[] = [];
for (const file of files) {
const text = await readFile(file, { encoding: 'utf-8' });
const localized = text.match(/(\$localize\`.*\`)/g);
if (localized) {
out.push(...localized.map((l) => l.replace(/((^\$localize\`)|(\`$))/g, '')));
}
}
return out;
}
async function main() {
const dir = join(process.cwd(), process.argv[2]);
const files = await getJsonFiles(dir);
const order = await getSortOrder();
const defaultLocale = files.find((f) => f.name.endsWith('.en.json'));
const defaultList = await sortTranslations(defaultLocale!.path, order);
const keyOrder = defaultList.map((i) => i.match(/"(.*)":/)![1]);
for (const file of files) {
if (file.name.endsWith('.en.json')) {
continue;
}
await sortTranslations(file.path, undefined, keyOrder);
}
}
main();