Skip to content

Commit d38a5e3

Browse files
committed
allow i18n scripts to take paths as parameters
1 parent bc7dad6 commit d38a5e3

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

scripts/combineTranslationKeys.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
import path from 'path';
22
import fs from 'fs';
33

4+
// generalize so we can use this script in other es6 repos
5+
// so you can call:
6+
// combineTranslationKeys <inputPath> <inputPath> <inputPath> ... <outputPath>
7+
48
const pathToCombinedTranslationKeys = path.join(
59
__dirname,
610
'./translationKeys/combined-translation-keys.txt'
711
);
812

9-
const plotlyJS = {
10-
repository: 'plotly.js',
11-
path: path.join(
12-
__dirname,
13-
'../node_modules/plotly.js/dist/translation-keys.txt'
14-
),
15-
};
13+
const plotlyJS = path.join(
14+
__dirname,
15+
'../node_modules/plotly.js/dist/translation-keys.txt'
16+
);
17+
18+
const editor = path.join(__dirname, './translationKeys/translation-keys.txt');
19+
20+
const argvLen = process.argv.length;
21+
const minHasPaths = 4;
22+
23+
const hasPaths = argvLen >= minHasPaths;
1624

17-
const editor = {
18-
repository: 'react-plotly.js-editor',
19-
path: path.join(__dirname, './translationKeys/translation-keys.txt'),
20-
};
25+
const inputPaths = hasPaths
26+
? process.argv.slice(2, argvLen - 1)
27+
: [plotlyJS, editor];
28+
29+
const outputPath = hasPaths
30+
? process.argv[argvLen - 1]
31+
: pathToCombinedTranslationKeys;
2132

2233
combineTranslationKeys();
2334

2435
function combineTranslationKeys() {
2536
const dict = {};
2637
let maxLen = 0;
2738

28-
[plotlyJS, editor].forEach(file => {
29-
const lines = fs.readFileSync(file.path, 'utf-8').split(/\r?\n/);
39+
inputPaths.forEach(inputPath => {
40+
const lines = fs.readFileSync(inputPath, 'utf-8').split(/\r?\n/);
41+
42+
const repository = getRepository(inputPath);
3043

3144
lines.forEach(line => {
3245
const splitString = line.split(/\/\//);
@@ -35,9 +48,9 @@ function combineTranslationKeys() {
3548
maxLen = Math.max(maxLen, stringToTranslate.length);
3649

3750
if (!dict[stringToTranslate]) {
38-
dict[stringToTranslate] = ' // ' + file.repository + ': ' + source;
51+
dict[stringToTranslate] = ' // ' + repository + ': ' + source;
3952
} else {
40-
dict[stringToTranslate] += ` && ${file.repository}: ${source}`;
53+
dict[stringToTranslate] += ` && ${repository}: ${source}`;
4154
}
4255
});
4356
});
@@ -47,10 +60,16 @@ function combineTranslationKeys() {
4760
.map(k => k + spaces(maxLen - k.length) + dict[k])
4861
.join('\n');
4962

50-
fs.writeFile(pathToCombinedTranslationKeys, strings);
51-
console.log(
52-
`combined translation keys were written to: ${pathToCombinedTranslationKeys}`
53-
);
63+
fs.writeFile(outputPath, strings);
64+
console.log(`combined translation keys were written to: ${outputPath}`);
65+
}
66+
67+
function getRepository(inputPath) {
68+
var dir = path.dirname(inputPath);
69+
if (fs.existsSync(path.join(dir, 'package.json'))) {
70+
return path.basename(dir);
71+
}
72+
return getRepository(dir);
5473
}
5574

5675
function spaces(len) {

scripts/findTranslationKeys.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import fs from 'fs';
44
import glob from 'glob';
55
import path from 'path';
66

7-
const pathToSrc = path.join(__dirname, '../src');
7+
// generalize so we can use this script in other es6 repos
8+
// so you can call:
9+
// findTranslationKeys <srcPath> <outputPath>
10+
const pathToSrc = process.argv[2] || path.join(__dirname, '../src');
811
const srcGlob = path.join(pathToSrc, '**/*.js');
912

10-
const localizeRE = /(^|[\.])(_|localize)$/;
13+
const pathToTranslationKeys = process.argv[3] || path.join(
14+
__dirname,
15+
'./translationKeys/translation-keys.txt'
16+
);
1117

1218
findLocaleStrings();
1319

@@ -84,10 +90,6 @@ function findLocaleStrings() {
8490
.map(k => k + spaces(maxLen - k.length) + ' // ' + dict[k])
8591
.join('\n');
8692

87-
const pathToTranslationKeys = path.join(
88-
__dirname,
89-
'./translationKeys/translation-keys.txt'
90-
);
9193
fs.writeFile(pathToTranslationKeys, strings);
9294
console.log(`translation keys were written to: ${pathToTranslationKeys}`);
9395
});

scripts/makeArrows.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import path from 'path';
22
import fs from 'fs';
33

4-
const pathToCombinedTranslationKeys = path.join(
4+
// generalize so we can use this script in other repos
5+
// so you can call:
6+
// makeArrows <srcPath> <outputPath>
7+
8+
const pathToCombinedTranslationKeys = process.argv[2] || path.join(
59
__dirname,
610
'./translationKeys/combined-translation-keys.txt'
711
);
812

9-
const pathToArrowsOut = path.join(__dirname, '../src/locales/xx.js');
13+
const pathToArrowsOut = process.argv[3] || path.join(
14+
__dirname,
15+
'../src/locales/xx.js'
16+
);
1017

1118
const wordRE = /^[A-Za-z]+$/;
1219

0 commit comments

Comments
 (0)