实现理想: 给一个文件名,让文件下面所有文件里面的汉字按照语言包的格式,自动完成替换! ٩(๑`灬´๑)۶ ...
大概思路:
-
1.它要能自己找到文件夹下面所有的文件,并且读取到文件;
解决办法: node文件系统自带的方法 (stats.isDirectory 是不是文件夹 、 stats.isFile 是不是文件 、 fs.readFile 读取文件
-
2.找到文件中的汉字,并且汉字要转换成拼音作为键动;
解决办法:找到汉字的正则:/([\u4e00-\u9fa5]+)/g 替换拼音:npm库里面的插件 npm i node-pinyin 汉字转拼音
-
3.主要替换的格式;
<!-- template 里面的汉字 比如“二级” 转换成 {{$t('index.erji')}} 的格式 erji是键名哈~ -->
<div class="xiangmu font_size_16">二级</div>
<!-- template 里面 input标签里面的placeholder 比如“请输入您的名字” 转换成 {{$t('index.qingshurunindemingzi')}} 的格式 qingshurunindemingzi依旧是键名哈~ -->
<el-input v-model="username" placeholder="请输入您的名字" style="width:256px;"></el-input>
<!-- script 里面的汉字 比如“唯一” 转换成 {{$t('index.weiyi')}} 的格式 weiyi是键名哈~ -->
member_id: "唯一ID"
-
4.还要生成键值对文件;
解决办法: 没找到一串汉字转成拼音的时候就往index对象里面添加 index['zhongwen'] = '中文'; 然后把index对象打印到控制台手动复制一下就行
完整代码 (.js文件):
要认真看注释哦!代码有不能做到的地方,要手动实现!
const fs = require("fs");
const pinyin = require("node-pinyin");
// 使用说明
// 1. 执行 npm i node-pinyin 安装汉字转换中文包
// 2.然后把这个文件拖到和要被转换的模块同等级(在一个文件夹下)
// 3.把下面name变量的名字替换成你要翻译的模块的名字
let name = 'index.vue'
// 4.执行 node find-chinese.js
// 5.替换完成 文件会自动替换 每一页语言包的键值 会打印在控制台 需要手动赋值然后合成一个文件
// 6.中文语言的键值对如何转换成 其他语言 可以见本人另一个转换语言工具 QAQ
// 谨慎起见 最好一个小模块一个小模块替换 替换完就运行一下看看是否正确
// 缺点!!(技术所限)
// 1.只能识别template单行纯中文注释 scrript里面的注释在hbuilder里面跑自动保留了 所以方法里面就没加处理
// 2.template里面如果有正则表达式需要自行处理 方法无法处理
// 存放生成的键值对
var index = {};
fun(name)
function fun(name) {
// 判断是目录还是文件
fs.stat(name, function(err, stats) {
if (err) {
return console.error(err);
}
// 是目录 去查他下面的所有文件
if (stats.isDirectory()) {
// 读取目录
fs.readdir(`${name}/`, function(err, files) {
if (err) {
return console.error(err);
}
// 遍历文件夹下面所有的文件名
files.forEach(function(file) {
// mac系统会有这个文件 这个文件就跳过
if (file == '.DS_Store') {
return
}
console.log(file);
// 为下面的每一个文件 再判断是否为文件
fun(`${name}/${file}`)
});
});
}
// 是文件
if (stats.isFile()) {
// 读文件
fs.readFile(name, (err, data) => {
if (err) {
console.log(err);
} else {
// 获取成功 转为字符串
let transferStr = data.toString();
// 拆分成 template 和 script 两部分 因为上下两部分处理方式不同
let spl = transferStr.split('</template>')
let temp = spl[0],
scri = spl[1];
// 替换template里面 input标签 palceholder的文字
temp = temp.replace(/(placeholder=")([\u4e00-\u9fa5]+)"/g, (str) => {
// 匹配到的字符串里面的中文 换成 语言包格式
str = str.replace(/([\u4e00-\u9fa5]+)/g, (chinese) => {
// 转成拼音
let pin = pinyin(chinese, {
style: "normal"
}).join("")
// 并添加到index对象里面
index[pin] = chinese;
// 返回语言包格式
return `$t('index.${pin}')`
})
// 加冒号
return `:${str}`
})
// 替换template里面的文字 `{{$t('index.$1')}}` 排除单行纯中文无端点注释
temp = temp.replace(/(<!-- )([\u4e00-\u9fa5]+)( -->)|([\u4e00-\u9fa5]+)/g, (chinese) => {
console.log(chinese);
if (chinese.startsWith('<!--') || chinese.endsWith('-->')) {
return chinese
}
// 转成拼音
let pin = pinyin(chinese, {
style: "normal"
}).join("")
// 并添加到index对象里面
index[pin] = chinese;
// 返回语言包格式
return `{{$t('index.${pin}')}}`
})
// 替换script里面的文字 `$t('index.$1')` hbuilder自动忽略了注释和console.log里面的文字
scri = scri.replace(/(['"])([\u4e00-\u9fa5]+)\1/g, (chinese) => {
// 去掉头尾引号
chinese = chinese.slice(1, -1)
// 转成拼音
let pin = pinyin(chinese, {
style: "normal"
}).join("")
// 并添加到index对象里面 `${pin}`
index[pin] = chinese;
// 返回语言包格式
return `this.$t('index.${pin}')`
})
// 上下部分拼到一起 写回文件里
fs.writeFile(name, temp + '</template>' + scri, function(err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
});
// 打印当前页面所有键值结果
console.log(index);
}
});
}
});
}
键值对文件生成完了,怎么翻译成其他国家的语言呢.. 还是无法摆脱cv的命运嘛~~~
我的另一篇文章,一件转换成其他国家的语言:juejin.cn/post/684490…
耶!