Use babylon and babel-traverse directly to find strings to translate

This commit is contained in:
Frank Li 2017-07-07 18:15:06 -07:00
parent e3139eea2f
commit d620f2e6dd
5 changed files with 145 additions and 34 deletions

View file

@ -79,7 +79,6 @@ const siteConfig = {
let languages;
if (fs.existsSync("./languages.js")) {
languages = require("./languages.js");
siteConfig["en"] = require("./i18n/en.json");
} else {
languages = [
{

View file

@ -0,0 +1,49 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* replaces translate tags with calls to translate function */
module.exports = function translatePlugin(babel) {
const { types: t } = babel;
return {
visitor: {
JSXElement(path) {
if (path.node.openingElement.name.name !== "translate") {
return;
}
/* assume translate element only has one child which is the text */
const text = path.node.children[0].value.trim();
let description;
const attributes = path.node.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name.name === "desc") {
description = attributes[i].value.value;
}
}
/* use an expression container if inside a jsxelement */
if (path.findParent(path => true).node.type === "JSXElement") {
path.replaceWith(
t.jSXExpressionContainer(
t.callExpression(t.identifier("translate"), [
t.stringLiteral(text + "|" + description)
])
)
);
} else {
path.replaceWith(
t.callExpression(t.identifier("translate"), [
t.stringLiteral(text + "|" + description)
])
);
}
}
}
};
};

View file

@ -12,7 +12,6 @@ const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const mkdirp = require('mkdirp');
let siteConfig;
console.log('translation.js triggered...');
@ -24,38 +23,11 @@ function writeFileAndCreateFolder(file, content) {
function execute() {
if (fs.existsSync(CWD + '/languages.js')) {
injectContent();
translatePages();
} else {
console.log('No languages besides English enabled');
}
}
function translatePages() {
const siteConfig = require(CWD + '/siteConfig.js');
const files = glob.sync(CWD + '/pages/en/**/*.js');
files.forEach(file => {
let fileContent = fs.readFileSync(file, 'utf8');
let baseName = path.basename(file, '.js');
for (let i = 0; i < siteConfig['languages'].length; i++) {
let language = siteConfig['languages'][i];
if (language.tag === 'en') continue;
if (siteConfig[language.tag]) {
let translatedContent = fileContent.slice(0);
Object.keys(siteConfig[language.tag]["pages-strings"][baseName])
.forEach(function(key) {
translatedContent = translatedContent.replace(key, siteConfig[language.tag]["pages-strings"][baseName][key]);
});
let translatedFile = file.replace('/en/', '/' + language.tag + '/');
writeFileAndCreateFolder(translatedFile, translatedContent);
}
}
});
}
function injectContent() {
const I18N_JSON_DIR = CWD + '/i18n/';
@ -73,7 +45,7 @@ function injectContent() {
});
let injectedContent = '';
languages.filter(language => language != 'en').forEach(language => {
languages.forEach(language => {
injectedContent +=
"\nsiteConfig['" +
language +

View file

@ -0,0 +1,93 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* generate the i18n/en.json file */
const CWD = process.cwd();
const fs = require("fs-extra");
const mkdirp = require("mkdirp");
const glob = require("glob");
const readMetadata = require("./readMetadata.js");
const path = require("path");
const siteConfig = require(CWD + "/siteConfig.js");
const babylon = require("babylon");
const traverse = require("babel-traverse").default;
function writeFileAndCreateFolder(file, content) {
mkdirp.sync(file.replace(new RegExp("/[^/]*$"), ""));
fs.writeFileSync(file, content);
}
function execute() {
let translations = {
"localized-strings": {
next: "Next",
previous: "Previous"
},
tagline: siteConfig.tagline,
"pages-strings": {}
};
/* look through front matter of docs for titles and categories to translate */
let files = glob.sync(CWD + "/../docs/en/**");
files.forEach(file => {
const extension = path.extname(file);
if (extension === ".md" || extension === ".markdown") {
const metadata = readMetadata.extractMetadata(
fs.readFileSync(file, "utf8")
).metadata;
translations["localized-strings"][metadata.id] = metadata.title;
translations["localized-strings"][metadata.category] = metadata.category;
}
});
/* look through header links for text to translate */
for (let i = 0; i < siteConfig.headerLinksInternal.length; i++) {
translations["localized-strings"][siteConfig.headerLinksInternal[i].text] =
siteConfig.headerLinksInternal[i].text;
}
for (let i = 0; i < siteConfig.headerLinksExternal.length; i++) {
translations["localized-strings"][siteConfig.headerLinksExternal[i].text] =
siteConfig.headerLinksExternal[i].text;
}
/* go through pages to look for text inside translate tags */
const plugin = __dirname + "/find-strings-plugin.js";
files = glob.sync(CWD + "/pages/en/**");
files.forEach(file => {
const extension = path.extname(file);
if (extension === ".js") {
const ast = babylon.parse(fs.readFileSync(file, "utf8"), {
plugins: ["jsx"]
});
traverse(ast, {
enter(path) {
if (
path.node.type === "JSXElement" &&
path.node.openingElement.name.name === "translate"
) {
const text = path.node.children[0].value.trim();
let description;
const attributes = path.node.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name.name === "desc") {
description = attributes[i].value.value;
}
}
translations["pages-strings"][text + "|" + description] = text;
}
}
});
}
});
writeFileAndCreateFolder(CWD + "/i18n/en.json", JSON.stringify(translations));
}
module.exports = execute;

View file

@ -8,6 +8,8 @@
"dependencies": {
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"babel-traverse": "^6.25.0",
"babylon": "^6.17.4",
"classnames": "^2.2.5",
"express": "^4.15.3",
"fs-extra": "^3.0.1",
@ -17,10 +19,6 @@
"request": "^2.81.0",
"shelljs": "^0.7.8"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-react": "^6.24.1"
},
"name": "docusaurus",
"version": "1.0.0-alpha.12",
"bin": {