Created
February 21, 2023 00:13
-
-
Save haozes/f8cf9be9e3095c738f52fe0ce8080277 to your computer and use it in GitHub Desktop.
翻译一条多语言文本,并写入 XCode strings 多语言文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 翻译一条多语言文本,并写入 XCode strings 多语言文件 | |
* (如果没有路由器翻墙,可能需要使用proxychains 执行) | |
* //https://github.com/iamtraction/google-translate | |
* 1. npm install --save @iamtraction/google-translate | |
* 2. pc node ./ios_proj_trans.js | |
*/ | |
const fs = require('fs'); | |
const translate = require('@iamtraction/google-translate'); | |
// 项目文件,xcodeproj 所在目录 | |
var PROJECT_DIR = "/Users/haozes/Documents/GitHub/yaoyao/YaoYao/YaoYao" | |
// 需要翻译的内容 | |
var TRANS_TEXT = "Background is on"; | |
// 有时翻译文本太长或有引号,缩写作为KEY | |
var TRANS_ABBR = "BACKGROUND_TIP_DESC" | |
// 文本原语言 | |
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(".lproj") > 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 = `\n"${txtKey}" = "${trans}";`; | |
if (abbr) { | |
line = `\n// ${txtKey}`; // 加一行注释 | |
line += `\n"${abbr}" = "${trans}";`; | |
} | |
const buffer = fs.readFileSync(strFile); | |
const fileContent = buffer.toString(); | |
if (fileContent.indexOf(line) >= 0) { | |
console.warn(`${line} exists in ${strFile}`); | |
} else { | |
fs.appendFileSync(strFile, line) | |
} | |
} | |
// xcode多语言文件夹名,映射成google translate 目标语言 | |
function getLans(projDirs) { | |
return projDirs.map((p) => { | |
var dName = p.split(".")[0]; | |
if (dName == "Base") { | |
dName = "en" | |
} | |
else if (dName == "es-419") { | |
dName = "es" | |
} | |
else if (dName == "pt-BR") { | |
dName = "pt" | |
} | |
else if (dName == "pt-PT") { | |
dName = "pt" | |
} | |
else if (dName == "ro-RO") { | |
dName = "ro" | |
} | |
else if (dName == "zh-Hans") { | |
dName = "zh-CN" | |
} | |
else if (dName == "zh-Hant") { | |
dName = "zh-TW" | |
} | |
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 + "/Localizable.strings"); | |
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