Skip to content

Instantly share code, notes, and snippets.

@haozes
Created February 21, 2023 00:14
Show Gist options
  • Save haozes/f728a7dd286c86caca47dc2cb5152ad4 to your computer and use it in GitHub Desktop.
Save haozes/f728a7dd286c86caca47dc2cb5152ad4 to your computer and use it in GitHub Desktop.
翻译一条多语言文本,并写入 android strings.xml 多语言文件
/**
* 翻译一条多语言文本,并写入 android strings.xml 多语言文件
* (如果没有路由器翻墙,可能需要使用proxychains 执行)
* //https://github.com/iamtraction/google-translate
* 1. npm install --save @iamtraction/google-translate
* 2. pc node ./android_proj_trans.js
*/
//https://github.com/iamtraction/google-translate
// npm install --save @iamtraction/google-translate
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
// 项目文件
var PROJECT_DIR = "/Users/haozes/Documents/GitHub/yaoyao_mi/share/src/main/res"
// 需要翻译的内容
var TRANS_TEXT = "Workouts"
// 有时翻译文本是个缩写作为KEY
var TRANS_ABBR = ""
// 文本原语言
var FROM_LAN = "en"
async function translateText(text, fromLan, toLan) {
return new Promise((resolve, reject) => {
translate(text, { from: fromLan, to: toLan }).then(res => {
resolve(res.text);
}).catch(err => {
console.error(err);
reject(err);
});
})
}
const directories = source => fs.readdirSync(source, {
withFileTypes: true
}).reduce((a, c) => {
c.isDirectory() && a.push(c.name)
return a
}, [])
// get all .lproj dir
function getLProjDirs(baseDir) {
let dirs = directories(baseDir);
let projDirs = dirs.filter(d => d.indexOf("values") >= 0);
return projDirs
}
function writeTransFile(txtKey, trans, abbr, strFile) {
console.log(">>> write to:", strFile, "trans:", trans);
if (fs.existsSync(strFile) == false) {
console.warn(`${strFile} not exist!!`);
return
}
let line = `<string name="${txtKey}">${trans}</string>`;
if (abbr) {
// line = `\n// ${txtKey}`; // 加一行注释
line = `<string name="${abbr}">${trans}</string>`;
}
const buffer = fs.readFileSync(strFile);
var fileContent = buffer.toString();
if (fileContent.indexOf(line) >= 0) {
console.warn(`${line} exists in ${strFile}`);
} else {
fileContent = fileContent.replace("</resources>", "\t"+line + "\n</resources>");
// fs.appendFileSync(strFile, line)
fs.writeFileSync(strFile, fileContent);
}
}
// 映射成google translate 目标语言
function getLans(projDirs) {
return projDirs.map((p) => {
var arr = p.split("-");
var dName = "en"
if (arr.length > 1) {
dName = arr[1];
}
if (p == "values-zh-rTW") {
dName = "zh-TW"
}
else if (p == "values-zh") {
dName = "zh-CN"
}
return dName
})
}
async function main() {
let projDirs = getLProjDirs(PROJECT_DIR);
console.log(projDirs);
let toLans = getLans(projDirs);
console.log(toLans);
projDirs = projDirs.map(p => PROJECT_DIR + "/" + p);
let stringsFiles = projDirs.map(d => d + "/strings.xml");
for (var i = 0; i < toLans.length; i++) {
let lan = toLans[i];
let strFile = stringsFiles[i];
let transRet = await translateText(TRANS_TEXT, FROM_LAN, lan);
writeTransFile(TRANS_TEXT, transRet, TRANS_ABBR, strFile);
}
console.log(">>> all done");
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment