Unity3D特效百例 | 案例项目实战源码 | Android-Unity实战问题汇总 |
---|---|---|
游戏脚本-辅助自动化 | Android控件全解手册 | 再战Android系列 |
Scratch编程案例 | 软考全系列 | Unity3D学习专栏 |
蓝桥系列 | ChatGPT和AIGC |
👉关于作者
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎底部卡片私我,获取更多支持,交流让学习不再孤单。
👉实践过程
const fs = require('fs')
const path = require('path')
const fanyi = require('./utils/fanyi')
const cache = require('./utils/cache')
const render = require('./render');
const tools = require('./tools')const translate = (list, listIndex = 0) => {const listLen = list.lengthconsole.log(listIndex, '/', listLen);if (listIndex >= listLen) {console.log('完毕');return;}const suffix = tools.getFileSuffix(list[listIndex]);const jsonFile = list[listIndex].replace('.' + suffix, '.json')console.log('========正在翻译:', listIndex, '/', listLen, jsonFile);const texts = require(jsonFile)const total = texts.lengthconsole.log("总条数:", total);const writeFile = (data) => {fs.writeFileSync(jsonFile, JSON.stringify(data, null, 2));}const translation = async (index) => {if (index <= texts.length) {const current = texts[index - 1]const {origin, local} = currentif (local) {console.log(`${index}/${total} ${origin} 已翻译了-> ${local}`)translation(index + 1)return}const hasLocal = cache.get(origin)if (hasLocal) {current.local = hasLocalconsole.log(`${index}/${total} ${origin} 已缓存了-> ${hasLocal}`)writeFile(texts);translation(index + 1)} else {current.local = await fanyi(origin)cache.set(origin, current.local)writeFile(texts);setTimeout(() => {translation(index + 1)}, 1000)console.log(`${index}/${total} ${origin} API翻译-> ${current.local}`)}} else {console.log(`${index}/${total} 完成`)writeFile(texts);render(list[listIndex], jsonFile);translate(list, listIndex + 1);}}translation(1);
}const configTranslate = (filename)=>{try {const config = require(filename);for (val of config.translation){console.log("自定义译文",val.name,val.value);cache.set(val.name,val.value);}}catch (e) {console.log("自定义译文",e);}
}module.exports = {translate: translate,configTranslate,configTranslate
}
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const getFileSuffix = (filename)=>{const spl = filename.split(".");return spl[spl.length-1];
}//同步拷贝目录
const copyFolder = (copiedPath, resultPath) => {if (fs.existsSync(resultPath)) {fse.removeSync(resultPath)}fs.mkdirSync(resultPath);if (fs.existsSync(copiedPath)) {fse.copySync(copiedPath, resultPath)} else {console.log('这个目录不存在:', copiedPath);}
}//获取所有html或htm
const getList = (dir = '') =>{if (dir == "") {console.log('请输入目录');return;}let files = fs.readdirSync(dir);let list = [];files.forEach(async (item, index) => {let fPath = path.join(dir, item);let stat = fs.statSync(fPath);if (stat.isDirectory() === true) {let sublist = getList(fPath);sublist.forEach(file => {list.push(file);})}if (stat.isFile() === true) {if (fPath.slice(-4) == 'html' || fPath.slice(-3) == 'htm') {list.push(fPath);}}});return list;
}
module.exports = {getFileSuffix:getFileSuffix,getList:getList,copyFolder:copyFolder
}
const fs = require('fs')
const path = require('path')
const lodash = require('lodash')
const render = require('posthtml-render')
const html2texts = require('./utils/html2texts')module.exports = (htmlfile, jsonfile) => {const html = fs.readFileSync(htmlfile).toString()const texts = require(jsonfile)const { texts: originTexts, tree } = html2texts(html)originTexts.forEach((item, index) => {lodash.set(tree, item.paths, texts[index].local)})const newHtml = render(tree)try {fs.writeFileSync(htmlfile, newHtml)console.log('还原文件成功', htmlfile)}catch (e){console.log('还原文件失败', htmlfile)}
}
const fs = require('fs')
const path = require('path')
const html2texts = require('./utils/html2texts')
const tools = require('./tools')module.exports = (file) => {const filepath = file;const suffix = tools.getFileSuffix(filepath);const outpath = filepath.replace('.'+suffix, '.json')if(fs.existsSync(outpath)){return ;}const html = fs.readFileSync(filepath).toString()const { texts } = html2texts(html)const localTexts = texts.map(({ text }) => {return { origin: text, local: '' }})fs.writeFileSync(outpath, JSON.stringify(localTexts, null, 2));console.log("生成待翻译",outpath)return outpath;
}
#!/usr/bin/env node
const fs = require('fs')
const path = require('path');
const parse = require('./lib/parse');
const translate = require('./lib/translate');
const tools = require('./lib/tools');let dirname = process.argv[2]; //项目名
let fileIndex = process.argv[3] || 0; //从第几个开始
const config = require( path.join(process.cwd(), 'config.json'));if(!config.appid || !config.secret){console.error("请配置appid and secret")return;
}
if(!dirname){console.error("请输入翻译文件夹 ");console.log('npm run [dirname]');console.log("-----------------------------------");console.log("请将项目文件放在本目录的html中");console.log("示例:");console.log("html/nodejs");console.log("html/php8");return;
}let dir = path.join(process.cwd(), 'html', dirname);//目标目录
let outDir = dir + '_zh_cn';if(!fs.existsSync(outDir)){tools.copyFolder(dir, outDir);
}
let list = tools.getList(outDir);
translate.configTranslate(dir+'/config.json')//生成json
list.forEach(file => {console.log('html',file)parse(file);
});
translate.translate(list,fileIndex);
const dayjs = require('dayjs')
const parser = require('posthtml-parser')module.exports = html => {const texts = []const tree = parser(html)const transform = (node, paths = '') => {const isStyle = node.tag === 'style'const isScript = node.tag === 'script'const isPre = node.tag === 'pre'const isCode = node.tag === 'code'if (isStyle || isScript || isCode || isPre) returnconst { placeholder } = node.attrs || {}const hasPlaceholder = !!placeholderif (hasPlaceholder) {transform(placeholder, `${paths}.attrs.placeholder`)return}const hasContent = !!node.contentif (hasContent) {node.content.forEach((item, index) => {transform(item, `${paths}.content[${index}]`)})return}const isString = typeof node === 'string'if (!isString) returnconst text = node.replace(/\s+/g, ' ').trim()const isEmpty = !textif (isEmpty) returnconst isDOCTYPE = !!text.match(/<!DOCTYPE/)const isDOCTYPE2 = !!text.match(/<!doctype/)const isComment = !!text.match(/<!--/)if (isDOCTYPE || isComment || isDOCTYPE2) returnconst isDate = dayjs(text).isValid()const isDivider = text === '|'if (isDate || isDivider) returntexts.push({ paths, text })}tree.forEach((item, index) => transform(item, `[${index}]`))return { tree, texts }
}
👉其他
📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。