-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtranslations.mjs
60 lines (55 loc) · 1.8 KB
/
translations.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
import { isLaunchFile } from '../scripts/script-utils.js';
import { readdir } from 'fs/promises';
import { join, basename } from 'node:path';
import { readFileSync, writeFileSync } from 'node:fs';
const paths = [join('pages', 'new-tab'), join('pages', 'history')];
const base = {
smartling: {
string_format: 'icu',
translate_paths: [
{
path: '*/title',
key: '{*}/title',
instruction: '*/note',
},
],
},
};
if (isLaunchFile(import.meta.url)) {
for (const path of paths) {
await processPage(path);
}
}
/**
* @param {string} path
*/
async function processPage(path) {
const targetName = basename(path);
const outputFile = join(path, '/public/locales/en', `${targetName}.json`);
const dirents = await readdir(path, { withFileTypes: true, recursive: true });
const rawEntries = dirents
.filter((entry) => entry.isFile() && entry.name === 'strings.json')
.map((entry) => {
const path = join(entry.parentPath, entry.name);
const raw = readFileSync(path, 'utf8');
let json;
try {
json = JSON.parse(raw);
} catch (e) {
throw new Error(`${e.name} in '${path}' ${e.name}\n ${e.message}`);
}
return {
path,
raw,
json,
};
});
for (const rawEntry of rawEntries) {
console.log(`✅ adding ${rawEntry.path} to ${targetName}.json`);
}
const entries = rawEntries.map((entry) => Object.entries(entry.json)).flat();
const object = Object.fromEntries(entries);
const withBase = { ...base, ...object };
const string = JSON.stringify(withBase, null, 2);
writeFileSync(outputFile, string);
}