@tenado/i18n-cli
Version:
i18n-cli是一个自动国际化脚本,通过执行命令,自动提取代码里面的中文,自动调用百度或谷歌翻译接口,自动将翻译结果以 key-value 形式存入*.json 语言包里
29 lines (27 loc) • 700 B
JavaScript
/**
* 根据中文生成key
* @param { String } text - 需要翻译的文本
* @param { Object } options - 配置
*/
const { nanoid } = require("nanoid");
const limax = require("limax");
module.exports = function generateKey(text, options) {
let result = "";
const { keyPrefix, keygenStrategy } = options;
// 生成随机字符串id
if (keygenStrategy === "random") {
result = nanoid();
}
// 转换
// i ♥ latin > i-love-latin
// 我爱官话 > wo-ai-guan-hua
else {
text = text.replace(/\$/g, "");
result = limax(text, { tone: false });
}
// 如果配置有前缀则要添加前缀
if (keyPrefix) {
result = keyPrefix + result;
}
return result;
};