{"_id":"@c-sheep/i18n-extract-cli","name":"@c-sheep/i18n-extract-cli","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@c-sheep/i18n-extract-cli","version":"1.0.0","description":"这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然，你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react","publishConfig":{"access":"public"},"bin":{"cc":"bin/index.js"},"types":"types/index.d.ts","scripts":{"dev":"tsc --watch","build":"rimraf dist && tsc --build","check":"tsc --noEmit"},"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/export.js"}},"keywords":["i18n","intl","extract","intl-cli","i18n-cli","vue-i18n","react-i18n","i18n-translate","auto-translate","translate"],"engines":{"node":">=14.21.2"},"author":"","license":"MIT","dependencies":{"@babel/core":"^7.20.2","@babel/generator":"^7.20.0","@babel/plugin-proposal-optional-chaining":"^7.18.9","@babel/plugin-syntax-async-generators":"^7.8.4","@babel/plugin-syntax-class-properties":"^7.12.13","@babel/plugin-syntax-decorators":"^7.19.0","@babel/plugin-syntax-do-expressions":"^7.18.6","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-export-extensions":"7.0.0-beta.32","@babel/plugin-syntax-function-bind":"^7.18.6","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-syntax-object-rest-spread":"^7.8.3","@babel/preset-typescript":"^7.18.6","@babel/template":"^7.18.10","@babel/traverse":"^7.20.0","@babel/types":"^7.20.0","@ifreeovo/translate-utils":"^1.2.0","@vue/compiler-sfc":"^3.2.39","chalk":"4.1.2","cli-progress":"^3.11.2","commander":"^9.4.1","ejs":"^3.1.8","fs-extra":"^10.1.0","glob":"^8.0.3","htmlparser2":"^8.0.1","inquirer":"8.2.5","leven":"3.1.0","lodash":"^4.17.21","md5":"^2.3.0","minimatch":"^10.0.1","minimist":"^1.2.8","mustache":"^4.2.0","node-xlsx":"^0.21.0","prettier":"^2.7.1","proxy-agent":"^5.0.0","qs":"^6.11.0","semver":"^7.3.8","serialize-javascript":"^6.0.0","slash":"3.0.0","tencentcloud-sdk-nodejs":"^4.0.839"},"devDependencies":{"@types/babel__core":"^7.1.20","@types/babel__generator":"^7.6.4","@types/babel__template":"^7.4.1","@types/babel__traverse":"^7.18.2","@types/cli-progress":"^3.11.0","@types/ejs":"^3.1.1","@types/fs-extra":"^9.0.13","@types/glob":"^8.0.0","@types/inquirer":"^9.0.3","@types/lodash":"^4.14.188","@types/md5":"^2.3.2","@types/minimist":"^1.2.2","@types/mustache":"^4.2.5","@types/node":"^18.11.6","@types/node-fetch":"^2.6.2","@types/prettier":"^2.7.1","@types/qs":"^6.9.7","@types/semver":"^7.3.13","@types/serialize-javascript":"^5.0.2","@typescript-eslint/eslint-plugin":"^5.40.1","@typescript-eslint/parser":"^5.40.1","rimraf":"^3.0.2","typescript":"^4.8.4"},"_id":"@c-sheep/i18n-extract-cli@1.0.0","gitHead":"78f1c75ddff1ddabbd255ba4b3cb20078fdbe0c6","_nodeVersion":"20.19.2","_npmVersion":"10.8.2","dist":{"integrity":"sha512-GGnOWRoH29NCY/qzkYjaEW7x1gDVQoQHQp+jx8QGy3leOqABAnEMooHfxUWVR8brkMPwwT2cbC76ZxDCn2IfWQ==","shasum":"3476e2576818435f6ddaf8ec35c2031a47b634a3","tarball":"https://registry.npmjs.org/@c-sheep/i18n-extract-cli/-/i18n-extract-cli-1.0.0.tgz","fileCount":86,"unpackedSize":162025,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIGH8r1mo6AdRuK1tbnoF81mP6orO8zfbIfKUzkHBQpY/AiEA1hfswlTvgYymdKXgcJ+4/cPSIUq1KMtEONNuur2lq1M="}]},"_npmUser":{"name":"happysheep","email":"1759633997@qq.com"},"directories":{},"maintainers":[{"name":"happysheep","email":"1759633997@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/i18n-extract-cli_1.0.0_1754873763773_0.9200179560342885"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-11T00:56:03.629Z","1.0.0":"2025-08-11T00:56:04.008Z","modified":"2025-08-11T00:56:04.305Z"},"maintainers":[{"name":"happysheep","email":"1759633997@qq.com"}],"description":"这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然，你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react","keywords":["i18n","intl","extract","intl-cli","i18n-cli","vue-i18n","react-i18n","i18n-translate","auto-translate","translate"],"license":"MIT","readme":"## 使用\n\n在项目根目录执行下面命令\n\n```\ncc\n```\n\n## 指令参数\n\n| 参数              | 类型   | 默认值 | 描述                                               |\n| ----------------- | ------ | ------ | -------------------------------------------------- |\n| -c, --config-file | String | ''     | 指定命令行配置文件的所在路径（可以自定义更多功能） |\n\n## 子命令\n\n| 子命令    | 描述                             |\n| --------- | -------------------------------- |\n| init      | 在项目里初始化一个命令行配置     |\n| translate | 把中文翻译成 en tw               |\n| export    | 将 module 里面的语言导出 excel   |\n| merge     | 把 excel 导入到项目里面生成 json |\n\n## 命令行配置\n\n如果有更多的定制需求，可以在项目根目录执行`cc init`，创建`i18n.config.js`文件，按自身需求修改完配置后，再执行`cc -c i18n.config.js`。（注意：配置文件里参数的优先级比指令参数高）\n\n```json\n{\n   \"i18n:init\": \"npx cc init\",\n    \"i18n:genre\": \"npx cc -c ./i18n.config.js\",\n    \"i18n:translate\": \"npx cc translate -c ./i18n.config.js\",\n    \"i18n:export\": \"npx cc exportExcel -c ./i18n.config.js\",\n    \"i18n:merge\": \"npx cc merge -c ./i18n.config.js\",\n}\n```\n```js\n// 以下为i18n.config.js默认的完整配置，所有属性均为可选，可以根据自身需要修改\nmodule.exports = {\n  input: 'src',\n  output: '', // 没有值时表示完成提取后自动覆盖原始文件\n  exclude: ['**/node_modules/**/*'], // 排除不需要提取的文件\n  localePath: './locales/module', // 中文语言包的存放位置\n  localeFileType: 'json', // 设置语言包的文件类型，支持js、json。默认为json\n  // rules每个属性对应的是不同后缀文件的处理方式\n  rules: {\n    js: {\n      caller: '', // 自定义this.$t('xxx')中的this。不填则默认没有调用对象\n      functionName: 't', // 自定义this.$t('xxx')中的$t\n      customizeKey: function (key, currentFilePath) {\n        return key\n      }, // 自定义this.$t('xxx')中的'xxx'部分的生成规则\n      importDeclaration: 'import { t } from \"i18n\"', // 默认在文件里导入i18n包。不填则默认不导入i18n的包。由于i18n的npm包有很多，用户可根据项目自行修改导入语法\n      forceImport: false, // 即使文件没出现中文，也强行插入importDeclaration定义的语句\n    },\n    // ts,cjs,mjs,jsx,tsx配置方式同上\n    ts: {\n      caller: '',\n      functionName: 't',\n      customizeKey: function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: 'import { t } from \"i18n\"',\n      forceImport: false,\n    },\n    cjs: {\n      caller: '',\n      functionName: 't',\n      customizeKey: function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: 'import { t } from \"i18n\"',\n      forceImport: false,\n    },\n    mjs: {\n      caller: '',\n      functionName: 't',\n      customizeKey: function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: 'import { t } from \"i18n\"',\n      forceImport: false,\n    },\n    jsx: {\n      caller: '',\n      functionName: 't',\n      customizeKey: function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: 'import { t } from \"i18n\"',\n      functionSnippets: '', // react函数组件里，全局加代码片段\n      forceImport: false,\n    },\n    tsx: {\n      caller: '',\n      functionName: 't',\n      customizeKey: function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: 'import { t } from \"i18n\"',\n      functionSnippets: '',\n      forceImport: false,\n    },\n    vue: {\n      caller: 'this',\n      functionNameInTemplate: '$t',// vue这里的配置，仅针对vue的template标签里面的内容生效\n      functionNameInScript: '$t', // vue这里的配置，仅针对vue的script部分export default里面的内容生效\n      customizeKey: : function (key, currentFilePath) {\n        return key\n      },\n      importDeclaration: '',\n      forceImport: false,\n      tagOrder: ['template', 'script', 'style'], // 支持自定义vue文件的标签顺序\n    },\n  },\n  globalRule: {\n    ignoreMethods: [] // 忽略指定函数调用的中文提取。例如想忽略sensor.track('中文')的提取。这里就写['sensor.track']\n  },\n  // prettier配置，参考https://prettier.io/docs/en/options.html\n  prettier: {\n    semi: false,\n    singleQuote: true,\n  },\n  skipExtract: false, // 跳过提取中文阶段\n  // 以下是和翻译相关的配置，注意搭配使用\n  skipTranslate: true, // 跳过翻译语言包阶段。默认不翻译\n  locales: [], // 需要翻译的语言包。例如['en', 'zh-CHT']，会自动翻译英文和繁体\n  excelPath: './locales.xlsx', // excel存放路径\n  exportExcel: false, // 是否导出excel\n  // 参数：\n  // allKeyValue：已遍历的所有文件的key-value\n  // currentFileKeyMap: 当前文件提取到的key-value\n  // currentFilePath: 当前遍历的文件路径\n  adjustKeyMap(allKeyValue, currentFileKeyMap, currentFilePath) {return allKeyValue}, // 对提取结构进行二次处理\n}\n```\n\n具体用法可以点击下方链接参考\n\n- [react 项目实战例子](https://github.com/IFreeOvO/i18n-cli/tree/master/examples/react-demo)\n\n- [vue 项目实战例子](https://github.com/IFreeOvO/i18n-cli/tree/master/examples/vue-demo)\n\n## 转换效果示例\n\n#### react 转换示例\n\n转换前\n\n```jsx\nimport { useState } from \"react\";\n\n/*i18n-ignore*/\nconst b = \"被忽略提取的文案\";\n\nfunction Example() {\n  const [msg, setMsg] = useState(\"你好\");\n\n  return (\n    <div>\n      <p title=\"标题\">{msg + \"呵呵\"}</p>\n      <button onClick={() => setMsg(msg + \"啊\")}>点击</button>\n    </div>\n  );\n}\n\nexport default Example;\n```\n\n转换后\n\n```jsx\nimport { t } from \"i18n\";\nimport { useState } from \"react\";\n\n/*i18n-ignore*/\nconst b = \"被忽略提取的文案\";\n\nfunction Example() {\n  const [msg, setMsg] = useState(t(\"你好\"));\n  return (\n    <div>\n      <p title={t(\"标题\")}>{msg + t(\"呵呵\")}</p>\n      <button onClick={() => setMsg(msg + t(\"啊\"))}>{t(\"点击\")}</button>\n    </div>\n  );\n}\nexport default Example;\n```\n\n#### vue 转换示例\n\n转换前\n\n```vue\n<template>\n  <div :label=\"'标签'\" :title=\"1 + '标题'\">\n    <p title=\"测试注释\">内容</p>\n    <button @click=\"handleClick('信息')\">点击</button>\n  </div>\n</template>\n\n<script>\nexport default {\n  methods: {\n    handleClick() {\n      console.log(\"点了\");\n    }\n  }\n};\n</script>\n```\n\n转换后\n\n```vue\n<template>\n  <div :label=\"$t('标签')\" :title=\"1 + $t('标题')\">\n    <p :title=\"$t('测试注释')\">{{ $t(\"内容\") }}</p>\n    <button @click=\"handleClick($t('信息'))\">{{ $t(\"点击\") }}</button>\n  </div>\n</template>\n<script>\nexport default {\n  methods: {\n    handleClick() {\n      console.log(this.$t(\"点了\"));\n    }\n  }\n};\n</script>\n```\n\n## 注意事项\n\n- 自定义配置里的 js 规则，除了用于处理 js 文件，也会应用到 vue 的模版和 vue`script`标签的非`export default`部分。例如\n\n```js\n<script>import a from 'a.js' function b() {a(\"哈哈哈\")}</script>\n```\n\n- 自定义配置里的 vue 的`functionNameInScript`规则，仅针对`script`标签的`export default`部分生效。例如\n\n```vue\n<script>\nexport default {\n  data: {\n    return {\n      a: '测试'\n    }\n  }\n}\n</script>\n```\n\n- 代码转换后，新插入的导入语句中`import { t } from \"i18n\"`的`i18n`是通过打包工具(如`webpack`)的别名`alias`功能实现的。开发者可以结合自身需求自己定义，通过别名把`i18n`文件指向一个绝对路径\n\n- 导入语句中`import { t } from \"i18n\"`，其中的`i18n`文件内容要自己去封装实现\n\n- 翻译后，命令行工具自动去掉提取汉字里的回车，这是因为回车会影响翻译准确度。所有原文里如果有回车，请自行校对，在语言包里手动补上回车\n\n<!-- npm publish --registry=http://nexus.bwcjxt.com/repository/npm-snapshots/ --verbose -->\n\n\n<!-- 语言 -->\n<!-- \"ja\",\"ko\",\"fr\",\"de\",\"es\",\"ru\",\"pt\",\"it\",\"ar\",\"nl\",\"th\",\"vi\",\"hi\",\"tr\",\"pl\",\"el\",\"cs\",\"sv\",\"da\",\"fi\",\"hu\",\"ro\",\"uk\",\"he\",\"bn\",\"ms\",\"id\",\"sr\", -->","readmeFilename":"README.md","_rev":"1-86cac080e4341236785090bae7dff9ab"}