支持 JSON 文件格式的语言包内容翻译成指定语种并写入其他语言 JSON 文件
目前支持 有道云翻译 、阿里翻译、谷歌翻译
ps:翻译接口有请求频率限制,若一次翻译不成功,建议多试几次呦,或者更换 ip 绕过限制。
阿里翻译说明:
除繁体中文、蒙语、粤语外,其他212种语言,可支持任意两种语言之间互译。
繁体中文、蒙语、粤语仅支持与中文之间的互译。
npm install --save-dev i18n-alltranslate
// or
yarn add i18n-alltranslate
建立一个 js 文件,写入以下内容
const { i18nI18nLang, i18nI18nTranslate } = require('i18n-alltranslate')
const { YDConfigSet, ALIConfigSet, GConfigSet, setI18nLang, setI18nTranslate } = require('i18n-alltranslate')
/**
* @description: 将中文 JSON 文件翻译写入其他语言 JSON 文件
* @param {String} dir/语言文件目录
* @param {String} zh/中文语言文件路径
* @param {String} other/其他语言文件路径
* @param {String} lang/语言种类,默认英文
* @param {String} type/翻译类型,默认全部翻译 all/翻译全部内容 new/仅翻译新增内容
* @return {type}
*/
const translate = async (dir, zh, en, fromLang, toLang, type) => {
// 使用 YDConfigSet 方法传入有道云翻译key,则使用有道云翻译
// YDConfigSet({ YD_APPKEY: '', YD_KEY: '' })
// 使用 ALIConfigSet 方法传入阿里翻译key,则使用阿里翻译
// ALIConfigSet({ accessKeyId: '', accessKeySecret: '' })
// 使用 GConfigSet 方法传入谷歌翻译key和代理信息,则使用谷歌翻译
GConfigSet(G_KEY, { proxy: { host: '127.0.0.1', port: '7890' } })
await setI18nLang(dir, zh)
await setI18nTranslate(zh, en, fromLang, toLang, type)
}
translate('demo', 'demo/zh.json', 'demo/en.json', 'zh', 'en', 'all')