Skip to content

Share i18n #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
},
"scripts": {
"lint": "prettier --write \"src/**/*.js\"",
"make:combined-translation-keys": "babel-node scripts/findTranslationKeys.js && babel-node scripts/combineTranslationKeys.js",
"make:translation-keys": "babel-node scripts/findTranslationKeys.js",
"make:arrows": "babel-node scripts/makeArrows.js",
"make:combined-translation-keys": "npm run make:translation-keys && node scripts/combineTranslationKeys.js",
"make:translation-keys": "node scripts/findTranslationKeys.js",
"make:arrows": "node scripts/makeArrows.js",
"make:lib": "mkdirp lib && npm run make:lib:js && npm run make:lib:css && npm run make:combined-translation-keys",
"make:lib:js": "mkdirp lib && babel src --out-dir lib --ignore=__tests__/* --source-maps",
"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",
Expand Down
61 changes: 40 additions & 21 deletions scripts/combineTranslationKeys.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import path from 'path';
import fs from 'fs';
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 = {
repository: 'plotly.js',
path: path.join(
__dirname,
'../node_modules/plotly.js/dist/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 editor = {
repository: 'react-plotly.js-editor',
path: path.join(__dirname, './translationKeys/translation-keys.txt'),
};
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;

[plotlyJS, editor].forEach(file => {
const lines = fs.readFileSync(file.path, 'utf-8').split(/\r?\n/);
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(/\/\//);
Expand All @@ -35,9 +48,9 @@ function combineTranslationKeys() {
maxLen = Math.max(maxLen, stringToTranslate.length);

if (!dict[stringToTranslate]) {
dict[stringToTranslate] = ' // ' + file.repository + ': ' + source;
dict[stringToTranslate] = ' // ' + repository + ': ' + source;
} else {
dict[stringToTranslate] += ` && ${file.repository}: ${source}`;
dict[stringToTranslate] += ` && ${repository}: ${source}`;
}
});
});
Expand All @@ -47,10 +60,16 @@ function combineTranslationKeys() {
.map(k => k + spaces(maxLen - k.length) + dict[k])
.join('\n');

fs.writeFile(pathToCombinedTranslationKeys, strings);
console.log(
`combined translation keys were written to: ${pathToCombinedTranslationKeys}`
);
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) {
Expand Down
28 changes: 15 additions & 13 deletions scripts/findTranslationKeys.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {transform} from 'babel-core';
import traverse from 'babel-traverse';
import fs from 'fs';
import glob from 'glob';
import path from 'path';

const pathToSrc = path.join(__dirname, '../src');
const transform = require('babel-core').transform;
const traverse = require('babel-traverse').default;
const fs = require('fs');
const glob = require('glob');
const path = require('path');

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

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

findLocaleStrings();

Expand Down Expand Up @@ -84,11 +90,7 @@ function findLocaleStrings() {
.map(k => k + spaces(maxLen - k.length) + ' // ' + dict[k])
.join('\n');

const pathToTranslationKeys = path.join(
__dirname,
'./translationKeys/translation-keys.txt'
);
fs.writeFile(pathToTranslationKeys, strings);
fs.writeFileSync(pathToTranslationKeys, strings);
console.log(`translation keys were written to: ${pathToTranslationKeys}`);
});
}
Expand Down
25 changes: 18 additions & 7 deletions scripts/makeArrows.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import path from 'path';
import fs from 'fs';
const path = require('path');
const fs = require('fs');

const pathToCombinedTranslationKeys = path.join(
// generalize so we can use this script in other repos
// so you can call:
// makeArrows <srcPath> <outputPath>

const pathToCombinedTranslationKeys = process.argv[2] || path.join(
__dirname,
'./translationKeys/combined-translation-keys.txt'
);

const pathToArrowsOut = path.join(__dirname, '../src/locales/xx.js');
const pathToArrowsOut = process.argv[3] || path.join(
__dirname,
'../src/locales/xx.js'
);

const wordRE = /^[A-Za-z]+$/;
const maxLineLen = 80;

function makeArrows() {
const lines = fs
Expand All @@ -31,7 +39,9 @@ function makeArrows() {
const quotedVal = quoteChar + arrowStr + key + arrowStr + quoteChar + ',';
const singleLine = ' ' + maybeQuoteKey + ': ' + quotedVal;

if (singleLine.length <= 80) return singleLine;
if (singleLine.length <= maxLineLen) {
return singleLine;
}

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

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

// inferred from the arrow file Greg provided
const arrowFactor = 5.7;
function getArrowLen(key) {
return Math.max(1, Math.round(key.length / 5.7));
return Math.max(1, Math.round(key.length / arrowFactor));
}

function arrowPad(n) {
Expand Down
Loading