Repository navigation
Expand file tree
/
Copy pathcombineTranslationKeys.js
More file actions
87 lines (68 loc) · 2.16 KB
/
Copy pathcombineTranslationKeys.js
File metadata and controls
87 lines (68 loc) · 2.16 KB
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
const path = require('path');
const fs = require('fs');
// generalize so we can use this script in other es6 repos
// so you can call:
// combineTranslationKeys <inputPath> <inputPath> <inputPath> ... <outputPath>
const pathToCombinedTranslationKeys = path.join(
__dirname,
'./translationKeys/combined-translation-keys.txt'
);
const plotlyJS = path.join(
__dirname,
'../node_modules/plotly.js/dist/translation-keys.txt'
);
const editor = path.join(__dirname, './translationKeys/translation-keys.txt');
const argvLen = process.argv.length;
const minHasPaths = 4;
const hasPaths = argvLen >= minHasPaths;
const inputPaths = hasPaths
? process.argv.slice(2, argvLen - 1)
: [plotlyJS, editor];
const outputPath = hasPaths
? process.argv[argvLen - 1]
: pathToCombinedTranslationKeys;
combineTranslationKeys();
function combineTranslationKeys() {
const dict = {};
let maxLen = 0;
inputPaths.map(relPath => path.resolve(relPath)).forEach(inputPath => {
const lines = fs.readFileSync(inputPath, 'utf-8').split(/\r?\n/);
const repository = getRepository(inputPath);
lines.forEach(line => {
const splitString = line.split(/\/\//);
const stringToTranslate = splitString[0].trim();
const source = splitString[1].trim();
maxLen = Math.max(maxLen, stringToTranslate.length);
if (!dict[stringToTranslate]) {
dict[stringToTranslate] = ' // ' + repository + ': ' + source;
} else {
dict[stringToTranslate] += ` && ${repository}: ${source}`;
}
});
});
const strings = Object.keys(dict)
.sort()
.map(k => k + spaces(maxLen - k.length) + dict[k])
.join('\n');
fs.writeFileSync(outputPath, strings);
console.log(`combined translation keys were written to: ${outputPath}`);
}
function getRepository(inputPath) {
const dir = path.dirname(inputPath);
if (fs.existsSync(path.join(dir, 'package.json'))) {
return path.basename(dir);
}
return getRepository(dir);
}
function spaces(len) {
let out = '';
for (let i = 0; i < len; i++) {
out += ' ';
}
return out;
}
process.on('exit', function(code) {
if (code === 1) {
throw new Error('combineTranslationKeys failed.');
}
});