Skip to content

Commit 82d5c92

Browse files
authored
Merge pull request #246 from plotly/share-i18n
Share i18n
2 parents bc7dad6 + 66fec73 commit 82d5c92

File tree

12 files changed

+388
-243
lines changed

12 files changed

+388
-243
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
},
1515
"scripts": {
1616
"lint": "prettier --write \"src/**/*.js\"",
17-
"make:combined-translation-keys": "babel-node scripts/findTranslationKeys.js && babel-node scripts/combineTranslationKeys.js",
18-
"make:translation-keys": "babel-node scripts/findTranslationKeys.js",
19-
"make:arrows": "babel-node scripts/makeArrows.js",
17+
"make:combined-translation-keys": "npm run make:translation-keys && node scripts/combineTranslationKeys.js",
18+
"make:translation-keys": "node scripts/findTranslationKeys.js",
19+
"make:arrows": "node scripts/makeArrows.js",
2020
"make:lib": "mkdirp lib && npm run make:lib:js && npm run make:lib:css && npm run make:combined-translation-keys",
2121
"make:lib:js": "mkdirp lib && babel src --out-dir lib --ignore=__tests__/* --source-maps",
2222
"make:lib:css": "mkdirp lib && babel-node scripts/styles.js && SASS_ENV=ie babel-node scripts/styles.js && babel-node scripts/postcss.js && SASS_ENV=ie babel-node scripts/postcss.js",

scripts/combineTranslationKeys.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1-
import path from 'path';
2-
import fs from 'fs';
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
// generalize so we can use this script in other es6 repos
5+
// so you can call:
6+
// combineTranslationKeys <inputPath> <inputPath> <inputPath> ... <outputPath>
37

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.map(relPath => path.resolve(relPath)).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.writeFileSync(outputPath, strings);
64+
console.log(`combined translation keys were written to: ${outputPath}`);
65+
}
66+
67+
function getRepository(inputPath) {
68+
const 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: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import {transform} from 'babel-core';
2-
import traverse from 'babel-traverse';
3-
import fs from 'fs';
4-
import glob from 'glob';
5-
import path from 'path';
6-
7-
const pathToSrc = path.join(__dirname, '../src');
1+
const transform = require('babel-core').transform;
2+
const traverse = require('babel-traverse').default;
3+
const fs = require('fs');
4+
const glob = require('glob');
5+
const path = require('path');
6+
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,11 +90,7 @@ 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-
);
91-
fs.writeFile(pathToTranslationKeys, strings);
93+
fs.writeFileSync(pathToTranslationKeys, strings);
9294
console.log(`translation keys were written to: ${pathToTranslationKeys}`);
9395
});
9496
}

scripts/makeArrows.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import path from 'path';
2-
import fs from 'fs';
1+
const path = require('path');
2+
const fs = require('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]+$/;
19+
const maxLineLen = 80;
1220

1321
function makeArrows() {
1422
const lines = fs
@@ -31,7 +39,9 @@ function makeArrows() {
3139
const quotedVal = quoteChar + arrowStr + key + arrowStr + quoteChar + ',';
3240
const singleLine = ' ' + maybeQuoteKey + ': ' + quotedVal;
3341

34-
if (singleLine.length <= 80) return singleLine;
42+
if (singleLine.length <= maxLineLen) {
43+
return singleLine;
44+
}
3545

3646
return ' ' + maybeQuoteKey + ':\n ' + quotedVal;
3747
})
@@ -40,13 +50,14 @@ function makeArrows() {
4050
const head = 'export default {';
4151
const tail = '};\n';
4252

43-
fs.writeFile(pathToArrowsOut, [head, entries, tail].join('\n'));
53+
fs.writeFileSync(pathToArrowsOut, [head, entries, tail].join('\n'));
4454
console.log('arrows mock translation written to: ' + pathToArrowsOut);
4555
}
4656

4757
// inferred from the arrow file Greg provided
58+
const arrowFactor = 5.7;
4859
function getArrowLen(key) {
49-
return Math.max(1, Math.round(key.length / 5.7));
60+
return Math.max(1, Math.round(key.length / arrowFactor));
5061
}
5162

5263
function arrowPad(n) {

0 commit comments

Comments
 (0)