Giter VIP home page Giter VIP logo

vve-i18n-cli's Introduction

vve-i18n-cli

Build Status

抽取国际化文本,生成国际化资源文件,配合vve-i18n-zh-check-clivve-i18n-zh-wrap-cli可以把中文包裹一层国际化函数,应用无缝变成国际化应用

安装

使用npm安装:

$ npm install vve-i18n-cli

使用

在package.json添加

"scripts": {
  "i18n": "vve-i18n-cli"
}

然后 npm run i18n

参数

命令行指定参数

program
  .version(require('../package.json').version)
  .option("--cwd <path>", "工作目录")
  .option("--root-dir <path>", "国际文本所在的根目录")
  .option(
    "--module-index-rules <items>",
    "模块入口列表",
    commaSeparatedList
  )
  .option(
    "--ignore-module-index-rules <items>",
    "忽略的模块入口列表",
    commaSeparatedList
  )
  .option(
    "--i18n-file-rules <items>",
    "匹配含有国际化文本的文件规则",
    commaSeparatedList
  )
  .option(
    "--ignore-i18n-file-rules <items>",
    "不匹配含有国际化文本的文件规则",
    commaSeparatedList
  )
  .option(
    "--i18n-text-rules <items>",
    "国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本",
    commaSeparatedList
  )
  .option(
    "--keep-key-rules <items>",
    "模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用",
    commaSeparatedList
  )
  .option(
    "--ignore-key-rules <items>",
    "忽略国际化KEY的规则,这些KEY不会生成再国际化文件中",
    commaSeparatedList
  )
  .option("--out-dir <path>", "生成的国际化资源包的输出目录")
  .option(
    "-l, --i18n-languages <items>",
    "需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部",
    commaSeparatedList
  )
  .option(
    "--config <path>",
    "配置文件的路径,没有配置,默认路径是在${cwd}/vve-i18n-cli.config.js"
  )
  .option("--disable-config-file", "是否取配置文件")
  .option("-t, --translate", "是否翻译,只翻译每次执行提取到新的key,且满足translateValueRules规则")
  .option("--translate-from-lang", "翻译的基础语言,默认是用中文翻译")
  .option(
    "--force-translate",
    "是否强制翻译,将遍历所有的key,且需满足translateValueRules规则"
  )
  .option("--translate-language <items>", "翻译的语言", commaSeparatedList)
  .option(
    "--translate-use-pin-yin",
    "非中文使用拼音来来翻译"
  )
  .option("--translate-baidu-appid <item>", "Baidu翻译appId")
  .option("--translate-baidu-key <item>", "Baidu翻译key")
  .option(
    "--translate-value-rules <items>",
    "需要翻译的Value的规则,只有value满足此条件才会翻译,如果配置空数组,则表示全部都需要翻译",
    commaSeparatedList
  )
  .option(
    "--translate-ignore-key-rules <items>",
    "忽略翻译KEY的规则,规则可以是一个字符串,正则,或者是函数",
    commaSeparatedList
  )
  .option("--copy-index", "模块下${outDir}/index.js文件不存在才拷贝index.js")
  .option("--force-copy-index", "是否强制拷贝最新index.js")
  .parse(process.argv);

配置文件指定参数

默认配置文件在${cwd}/vve-i18n-cli.config.js,样例内容如下所示

module.exports = {
  outDir: 'lang'
}

默认值

const config = {
  // 工作目录
  cwd: ".",
  // 根目录,国际文本所在的根目录
  rootDir: "src",
  // 默认所有模块,如果有传module参数,就只处理某个模块
  // '**/module-**/**/index.js'
  moduleIndexRules: ["."],
  // 忽略模块
  ignoreModuleIndexRules: [],
  // 匹配含有国际化文本的文件规则
  i18nFileRules: ["**/*.+(vue|js)"],
  // 不匹配含有国际化文本的文件规则
  ignoreI18nFileRules: [],
  // 国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本
  i18nTextRules: [/(?:[\$.])t\([\s\n]*['"](.+?)['"]/g],
  // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用
  // 规则可以是一个字符串,正则,或者是函数
  keepKeyRules: [
    /^G\/+/ // G/开头的会被保留
  ],
  // 忽略国际化KEY的规则
  // 规则可以是一个字符串,正则,或者是函数
  ignoreKeyRules: [
  ],
  // 生成的国际化资源包的输出目录
  outDir: "lang",
  // 生成的国际化的语言
  i18nLanguages: [
    "zh", // 中文
    "en" // 英文
  ],
  // 配置文件的路径,没有配置,默认路径是在${cwd}/vve-i18n-cli.config.js
  config: undefined,
  // 是否取配置文件
  disableConfigFile: false,
  // 是否翻译,只翻译每次执行提取到新的key,且满足translateValueRules规则
  translate: false,
  // 翻译的基础语言,默认是用中文翻译
  translateFromLang: "zh",
  // 是否强制翻译,将遍历所有的key,且需满足translateValueRules规则
  forceTranslate: false,
  // 翻译的语言
  translateLanguage: ["zh", "en"],
  // 非中文使用拼音来来翻译
  translateUsePinYin: false,
  // Baidu翻译appId
  translateBaiduAppid: '',
  // Baidu翻译key
  translateBaiduKey: '',
  // 需要翻译的Value的规则,只有value满足此条件才会翻译,如果配置空数组,则表示全部都需要翻译
  translateValueRules: [
    /[^\x00-\xff]+/ // 中文
  ],
  // 忽略翻译KEY的规则,规则可以是一个字符串,正则,或者是函数
  translateIgnoreKeyRules: [],
  // 模块下${outDir}/index.js文件不存在才拷贝index.js
  copyIndex: false,
  // 是否强制拷贝最新index.js
  forceCopyIndex: false
};

开发

  • node >= 8
npm i // 安装依赖
npm test // 测试
npm run release // 发布
git push --follow-tags origin master && npm publish // npm 发布

捐赠

如果你觉得它有用,你可以给我买一杯奶茶。

donation

vve-i18n-cli's People

Contributors

nqdy666 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vve-i18n-cli's Issues

建议优化key生成规则以及翻译长度限制机制

不太适应key为中文的做法,特别是界面上容易有歧义,相同中文对应不同位置的英文翻译可能不一样。
希望能做一些针对key生成规则的优化,以vue2+element-ui为例,分析可能需要翻译内容
假设已知模块名(module可在目录中配置,写在index中或者单独文件,或者全局按目录进行配置)
页面:页面标题 [module].[pagefile].title
列表: 列名 [module].[listname].[column] 或 [module].[column] 允许选择通用或者不同列表单独配置
表单: 字段一般需要翻译 label/placeholder/alt/tip等明确属性,通常存在prop类似属性可确定字段名称 field
label/alt: [module].[field], placeholder:[module].[field]Ph, tip: [module].[field]Tip
不一一列举,总体思路就是通过常用需要翻译的文字,去找到对应模型对应的模型名或字段名,生成符合本来业务含义的key。

另外title/label/button 除了翻译外,可能还涉及到长度的限制,不同语言的差别较大,
可考虑方案: 方案1-增加长度限制的参数作为参考,或者 方案2-整理类似normal正常/small小/large大 三种类型分别翻译
前端根据UI样式来选择对应的翻译版本

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.