2.2.5 • Published 5 months ago

language-translate v2.2.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

language-translate

language-translate 是一款基于 Google 翻译在线转换 ts/js/json 多语言文件并批量生成或插入指定文件的插件,支持增量更新,可使用 bash 翻译单个文件,也能集成在项目中持续批量翻译,支持单文件转单文件,单文件转多文件,多文件转多文件,多文件转单文件

中文English

效果演示

正常翻译效果:

demo.gif

压力测试(1分钟内生成100种语言包):

demo.gif

安装

  npm i language-translate

使用

在您的项目根目录下创建 translate.config.(js|ts) 进行配置

js 配置示例:

// translate.config.js
import { Lang } from 'language-translate/types';
import { defineConfig } from 'language-translate/utils';

export default defineConfig({
  proxy: {
    host: '127.0.0.1',
    port: 7890,
  },
  fromLang: Lang.en,
  fromPath: 'translate.entry.json',
  translate: [
    {
      label: '将结果翻译到locales文件夹下',
      targetConfig: [
        {
          targetLang: Lang.de,
          outPath: 'locales/de.json',
        },
        {
          targetLang: Lang['zh-CN'],
          outPath: 'locales/zh.json',
        },
        {
          targetLang: Lang.ko,
          outPath: 'locales/ko.json',
        },
      ]
    }
  ]
})

在您的项目根目录下创建 translate.entry.(js|ts|json) 文件名随意,但需要和配置中 fromPath 对应,里面填写您需要翻译的内容,内容所使用语言与配置中 fromLang 对应

例如在根目录创建 translate.entry.json

{
  "hello": "Hello {{name}}, how are you today?",
  "button": {
    "confirm": "confirm",
    "cancel": "cancel"
  },
  "header": {
    "login": "login"
  }
}

注意: 翻译后会在结果保留 i18n 中的插值表达式,例如上述 {{name}} 但仅支持 {{}} 这一种语法

package.json 中加入 scripts 命令

"scripts": {
  "translate": "translate"
}

在终端执行命令启动翻译

npm run translate

不出意外已经可以在根目录下 locales 内看到 de.jsonko.jsonzh.json

|-- Your Project Name
  |-- locales
    |-- de.json
    |-- ko.json
    |-- zh.json
  |-- package.json
  |-- translate.config.js
  |-- translate.entry.json

此后有新文案需要翻译时,只需要修改 fromPath 文件内容(即示例中translate.entry.json),再执行 npm run translate 即可实现增量更新

高级用法

配置中的 fromPath 是基于 fast-glob 的,所以支持动态解析,例如递归翻译整个文件夹下以 .en.json 结尾的文件

// translate.config.js
import { Lang } from 'language-translate/types';
import { defineConfig } from 'language-translate/utils';

export default defineConfig({
  proxy: {
    host: '127.0.0.1',
    port: 7890,
  },
  fromLang: Lang.en,
  fromPath: 'locales/**/*.en.json',
  translate: [
    {
      label: '递归翻译文件夹内json文件并重写文件名',
      targetConfig: [
        {
          targetLang: Lang.de,
          outPath: 'locales',
          rewrite: fileName => fileName.replace('.en.json', '.de.json'),
        },
        {
          targetLang: Lang['zh-CN'],
          outPath: 'locales',
          rewrite: fileName => fileName.replace('.en.json', '.zh.json'),
        },
        {
          targetLang: Lang.ko,
          outPath: 'locales',
          rewrite: fileName => fileName.replace('.en.json', '.ko.json'),
        },
      ]
    },
  ]
})

翻译前 locales 目录结构

locales
├─com.en.json
├─header.en.json
├─children
|    ├─color.en.json

翻译后 locales 目录结构

locales
├─com.de.json
├─com.en.json
├─com.ko.json
├─com.zh.json
├─header.de.json
├─header.en.json
├─header.ko.json
├─header.zh.json
├─children
|    ├─color.de.json
|    ├─color.en.json
|    ├─color.ko.json
|    └─color.zh.json

也可以将翻译结果输出到另一个文件夹,只需要更改 outPath 即可,更多用法等你解锁哦,也可结合FAQ输出可选做出更多花样,项目 example 目录中也提供了一些简单示例

配置API

translate.config.(js|ts)

属性描述类型默认值必填
toolsLang翻译工具在使用过程中终端输出的提示语言en | zh-CNzh-CN
proxy使用 Google 翻译,需要网络代理,如果您所在的国家能直接使用 Google 就可以不填 proxy 配置项Proxyundefined
fromLang待翻译文件所使用语言Lang-
fromPath待翻译文件路径,基于 fast-glob,支持动态解析,后缀名可以是 js|ts|jsonstringtranslate.entry.json
translate翻译输出配置,可配置多项在开始翻译时进行选择Translate[]-
incrementalMode增量更新模式(翻译完成后修改 fromPath 对应文件内容再次启动翻译的处理策略,详见IncrementalMode)IncrementalModeIncrementalMode.cover
translateRuntimeDelay翻译过程中每个 key 之间的延迟,单位毫秒number0
translateRuntimeChunkSize翻译过程中每隔多少个 key 将翻译结果同步写入一下硬盘,防止意外中断导致浪费已翻译的 keynumber5
translateRuntimeMergeEnabled在翻译运行时启用文案合并功能,减少 API 调用频率,显著提升翻译速度booleantrue
mergeEnabledChunkValuesLength在启用文案合并功能时限制单个合并文案的最长字符数,不要超过5000,因为 Google 翻译单次翻译字符最大限制5000number5000
ignoreValuesAndCopyToTarget排除fromPath文件中指定的文案并直接拷贝到目标文件内string[][]
apiKeyConfig用于配置非 Google 翻译的密钥信息,目前只扩展了百度翻译接口ApiKeyConfigundefined

Proxy

属性描述类型
host代理主机地址string
port代理主机端口number

Translate

属性描述类型
label自定义名称,当 translate 配置项有多项的时候,启动翻译时会在终端提示进行选择让翻译更加工程化,具体演示: FAQstring
targetConfig翻译输出配置TargetConfig

TargetConfig

属性描述类型必填
targetLang翻译输出的目标语言Lang
outPath翻译后文件输出路径,后缀名可以是js|ts|json,也可以是目录,输出时如果没有目标文件则自动生成,如果有则增量更新string
rewrite可选值,传入回调函数可在输出时重写文件名,形参会传入原始文件名,返回值是最终输出文件名Callback(fileName: string) => string;

IncrementalMode

描述
cover覆盖模式: 目标文件中已有的 key 直接覆盖,没有的 key 新增
fast快速模式: 目标文件中已有 key 并且值不为空在翻译时会排除, 否则新增 key, 如果不理解可以去项目 example 目录查看例子

ApiKeyConfig

用于配置非 Google 翻译的密钥信息,目前扩展了 DeepL 和百度翻译,DeepL 区分输入和输出语言代码,若使用 DeepL 请按照 DeepL 官方语言代码配置。百度翻译API免费版限制请求频率,效果很差,非要用百度建议使用付费高级版

export enum TranslateService {
  baidu = 'baidu',
  google = 'google',
  deepl = 'deepl'
}
export interface BaiduApiKeyConfig {
  appId: string
  appKey: string
}
export interface DeepLApiKeyConfig {
  authKey: string
}
export interface ApiKeyConfig {
  type: TranslateService
  [TranslateService.baidu]?: BaiduApiKeyConfig
  [TranslateService.deepl]?: DeepLApiKeyConfig
}

Lang

语言翻译代码
中文(简体)zh-CN
中文(繁體)zh-TW
英语en
南非荷兰语af
阿尔巴尼亚语sq
阿姆哈拉语am
阿拉伯语ar
亚美尼亚语hy
阿塞拜疆语az
巴斯克语eu
白俄罗斯语be
孟加拉语bn
波斯尼亚语bs
保加利亚语bg
加泰罗尼亚语ca
宿雾语ceb
奇切瓦语ny
科西嘉语co
克罗地亚语hr
捷克语cs
丹麦语da
荷兰语nl
世界语eo
爱沙尼亚语et
菲律宾语tl
芬兰语fi
法语fr
弗里斯兰语fy
加利西亚语gl
格鲁吉亚语ka
德语de
希腊语el
古吉拉特语gu
海地克里奥尔语ht
夏威夷语haw
希伯来语iw
印地语hi
苗语hmn
匈牙利语hu
冰岛语is
伊博语ig
印度尼西亚语id
爱尔兰语ga
意大利语it
日语ja
爪哇语jw
卡纳达语kn
哈萨克语kk
高棉语km
韩语ko
库尔德语ku
吉尔吉斯语ky
老挝语lo
拉丁语la
拉脱维亚语lv
立陶宛语lt
卢森堡语lb
马其顿语mk
马达加斯加语mg
马来语ms
马拉雅拉姆语ml
马耳他语mt
毛利语mi
马拉地语mr
蒙古语mn
缅甸语my
尼泊尔语ne
挪威语no
普什图语ps
波斯语fa
波兰语pl
葡萄牙语pt
旁遮普语pa
罗马尼亚语ro
俄语ru
萨摩亚语sm
苏格兰盖尔语gd
塞尔维亚语sr
塞索托语st
绍纳语sn
信德语sd
僧伽罗语si
斯洛伐克语sk
斯洛文尼亚语sl
索马里语so
西班牙语es
巽他语su
斯瓦希里语sw
瑞典语sv
塔吉克语tg
泰米尔语ta
泰卢固语te
泰语th
土耳其语tr
乌克兰语uk
乌尔都语ur
乌兹别克语uz
越南语vi
威尔士语cy
科萨语xh
依地语yi
约鲁巴语yo
祖鲁语zu

如果 Lang 配置中没有您需要的语言,您可以直接传入 Google 翻译支持的语言代码

FAQ

translate 配置项配置多项的适用场景 在开发中,locales 文件夹下可能会有专门存放错误码、业务文案的文件夹,甚至多种模块,例如:

|-- Your Project Name
  |-- locales
    |-- error
      |-- de.json
      |-- ko.json
      |-- zh.json
    |-- label
      |-- de.json
      |-- ko.json
      |-- zh.json

此时在待翻译文件填入错误码文案内容,例如 translate.entry.json

{
  "0": "service error",
  "4": "client error",
}

现在我们只想让翻译后的结果插入 locales/error 目录下,只需在 translate 配置项添加多个选项后再启动翻译,这时终端会提示让您进行选择:

// translate.config.js
import { Lang } from 'language-translate/types';
import { defineConfig } from 'language-translate/utils';

export default defineConfig({
  proxy: {
    host: '127.0.0.1',
    port: 7890,
  },
  fromLang: Lang.en,
  fromPath: 'translate.entry.json',
  translate: [
    {
      label: '将结果翻译到locales/error文件夹下',
      targetConfig: [
        {
          targetLang: Lang.de,
          outPath: 'locales/error/de.json',
        },
        {
          targetLang: Lang['zh-CN'],
          outPath: 'locales/error/zh.json',
        },
        {
          targetLang: Lang.ko,
          outPath: 'locales/error/ko.json',
        },
      ]
    },
    {
      label: '将结果翻译到locales/label文件夹下',
      targetConfig: [
        {
          targetLang: Lang.de,
          outPath: 'locales/label/de.json',
        },
        {
          targetLang: Lang['zh-CN'],
          outPath: 'locales/label/zh.json',
        },
        {
          targetLang: Lang.ko,
          outPath: 'locales/label/ko.json',
        },
      ]
    },
  ]
})

终端执行

npm run translate

此时已实现翻译输出可选功能

在 bash 中使用

language-translate 也支持在 bash 中翻译单个文件

npm i language-translate -g

直接在 bash 执行

translate -i /User/xxx/source.json -o /User/xxx/target.json -f en -t zh-CN -h 127.0.0.1 -p 7890

您也可以输入 translate --help 获取帮助

translate --help
# 终端输出
Usage: translate [options]

Translate a single js/ts/json file

Options:
  -i, --input <string>     source file path
  -o, --output <string>    target file path
  -f, --fromlang <Lang>    source file language
  -t, --targetlang <Lang>  target file language
  -h, --host <string>      proxy host
  -p, --port <string>      proxy port
  --help                   display help for command
2.2.5

5 months ago

2.2.4

6 months ago

2.2.1

12 months ago

2.2.0

12 months ago

2.2.3

12 months ago

2.2.2

12 months ago

2.1.6

12 months ago

2.1.8

12 months ago

2.1.7

12 months ago

2.1.9

12 months ago

2.1.10

12 months ago

2.1.4

12 months ago

2.1.5

12 months ago

2.1.2

1 year ago

2.0.3

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.1.3

1 year ago

2.0.4

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.1.2

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago