i18n-json-compiler 一个为TypeScript编写的国际化方案

i18n-json-compiler是一款用于将国际化json文件编译为TypeScript或JavaScript函数的工具。该工具支持从json文件中读取多语言字符串,并能够处理带有变量的字符串,最终生成易于使用的函数,简化了应用国际化的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写一个APP的过程中, 难免会遇到要做国际化的时候. 也就是需要根据不同的地区, 展示不同的文案. 对于简单的文本, 直接用一个xml或者json或者一个变量就能搞定, 但是有时候需要在一句话中加入变量, 就比较麻烦或者说比较恶心了. 比如这样的情况:

cn: 有xx个人喜欢了你.
en: xx People liked you.

i18n-json-compiler应运而生, 其作用是将json模板编译成TypeScript(或者JavaScript)函数或者字符串.

比如对以下json:

[
  { "cn": "你好", "en": "Hello" },
  { "cn": "{n}个好友", "en": "{n} friends" },
]

可以直接编译出ts文件, 内容大致如下:

export const cn = {
  Hello: "你好",
  n_friends: (n: any) => n + "个好友",
}

export const en = {
  Hello: "Hello",
  n_friends: (n: any) => n + " friends",
}

在代码中直接调用相应的属性或函数即可.

安装

yarn add i18n-json-compiler
# 或者使用npm/cnpm
npm i -S i18n-json-compiler

使用

在命令行中, 执行./node_modules/.bin/i18n, 参数如下:

i18n [options] - Parse i18n json files to typescript files.

Options:
  -i, --input-files  The json files to parse                 [string] [required]
  -o, --output-dir   The directory to emit output            [string] [required]
  -h, --help         Show help                                         [boolean]
  -v, --version      Show version number                               [boolean]
  --default-lang                                        [string] [default: "cn"]

JSON文档的格式

i18n命令接受参数-i指定输入文件列表(glob), 比如./strings/*.json, 文件格式为json, 内容为一个数组. 每个数组元素为一组需要编译的字符串. key为语言, value为值. 值中被{}包起来的地方会被转换为函数参数, 其格式为{name:type:default}, 其中:

  • name是必需的, 可以是字符串或数字
  • type为数据类型, 可以是int, double, string, boolean, any, 默认为any, 即接受任意参数
  • default为参数默认值

比如:

[
  {
    "cn": "{n:int:1}个人喜欢了你",
    "en": "{n}people liked you"
  }
]

得出的结果为:

const n_people_liked_you = (n: int = 1) => n + '个人喜欢了你'

输出格式

i18n接受参数-o指定输出目录, 输入目录中, 包括一个index.ts文件, 以及若干语言文件夹, 其中index.ts为所用到的文件, 其导出一个变量strings, 结构如下:

export interface I18n<T, L> {
  // 设置语言
  set(lang: string, defaultLang?: L): void;
  // 获取语言下的字符串对象
  get(lang: string, defaultLang?: L): T;
  // 当前语言
  lang: L;
  // 当前语言的字符串结构
  value: T;
}

例子

参考https://github.com/acrazing/i..., 其中input为输入目录, output为输出目录. 在代码中, 只需要:

import { strings } from './output/index.ts'

console.log(strings.value.world.$0_people_liked_you(1))

TODO

  • 编译成js
  • 不同语言参数位置不同
  • 指定名称key

更多内容, 请稳步 https://github.com/acrazing/i...

{ "name": "ims-web-view", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "test:unit": "vue-cli-service test:unit", "analyze": "cross-env ANALYZE=true vue-cli-service build", "build:dameng": "vue-cli-service build --mode dameng", "build:mofan": "vue-cli-service build --mode mofan", "build:prod": "vue-cli-service build", "build:stage": "vue-cli-service build --mode staging", "build:sxf": "vue-cli-service build --mode sxf", "build:uat": "vue-cli-service build --mode uat" }, "dependencies": { "@babel/runtime": "^7.25.7", "@fullcalendar/core": "^6.1.15", "@fullcalendar/daygrid": "^6.1.15", "@fullcalendar/interaction": "^6.1.15", "@fullcalendar/resource-timeline": "^6.1.15", "@fullcalendar/timegrid": "^6.1.15", "@fullcalendar/timeline": "^6.1.15", "@fullcalendar/vue": "^6.1.15", "@jiaminghi/data-view": "^2.10.0", "@riophae/vue-treeselect": "^0.4.0", "@sentry/tracing": "^7.118.0", "@sentry/vue": "^7.118.0", "@stomp/stompjs": "^7.0.0", "@tencentcloud/call-uikit-vue": "^4.0.7", "@tencentcloud/call-uikit-vue2": "^4.0.7", "@tencentcloud/chat-uikit-vue": "^2.5.1", "@vant/area-data": "^1.5.1", "alloyfinger": "^0.1.16", "awe-dnd": "^0.3.4", "axios": "^1.7.7", "core-js": "^3.38.1", "dayjs": "^1.11.13", "dhtmlx-gantt": "^8.0.10", "dom-to-image-hm": "^2.6.1", "dompurify": "^3.2.5", "echarts-wordcloud": "^2.1.0", "el-table-infinite-scroll": "^2.0.2", "element-resize-detector": "^1.2.4", "element-ui": "^2.15.14", "file-saver": "^2.0.5", "html2canvas": "^1.4.1", "jquery": "^3.7.1", "js-cookie": "^3.0.5", "js-md5": "^0.8.3", "jsencrypt": "^3.3.2", "jsonwebtoken": "^9.0.2", "jspdf": "^1.5.3", "jszip": "^3.10.1", "lodash": "^4.17.21", "moment": "^2.30.1", "node-forge": "^1.3.1", "normalize.css": "^8.0.1", "nprogress": "^0.2.0", "path-to-regexp": "^6.3.0", "posthog-js": "^1.230.4", "qrcodejs2": "0.0.2", "qs": "^6.13.0", "quill-image-extend-module": "^1.1.2", "recorder-core": "^1.2.23020100", "recordrtc": "^5.6.2", "save": "^2.4.0", "screenfull": "^6.0.2", "sm-crypto": "^0.3.13", "sortablejs": "^1.15.3", "svg-sprite-loader": "^6.0.11", "svgo": "^1.3.2", "swiper": "^5.4.5", "tim-upload-plugin": "^1.4.2", "trtc-sdk-v5": "^5.1.3", "turndown": "^7.2.0", "v-viewer": "^1.6.4", "vant": "^2.12.54", "vue": "^2.7.16", "vue-amap": "^0.5.10", "vue-awesome-swiper": "^4.1.1", "vue-bus": "^1.2.1", "vue-carousel": "^0.18.0", "vue-class-component": "^7.2.3", "vue-clipboard2": "^0.3.3", "vue-clock2": "^1.1.5", "vue-count-to": "^1.0.13", "vue-gallery": "^2.0.1", "vue-i18n": "^8.28.1", "vue-js-toggle-button": "^1.3.3", "vue-markdown": "^2.2.4", "vue-orgchart": "^1.1.7", "vue-property-decorator": "^9.1.2", "vue-qr": "^2.3.0", "vue-quill-editor": "^3.0.6", "vue-router": "^3.6.5", "vue-splitpane": "^1.0.6", "vue-wordcloud": "^1.1.1", "vue2-org-tree": "^1.3.6", "vuex": "^3.6.2", "vxe-table": "^3.11.14", "vxe-table-plugin-export-xlsx": "^2.2.1", "vxe-utils": "^1.9.3", "wangeditor": "^4.7.15", "xe-ajax": "^4.0.3", "xe-utils": "^3.4.0", "xlsx": "^0.18.5" }, "devDependencies": { "@babel/plugin-transform-runtime": "^7.25.7", "@riophae/vue-treeselect": "^0.4.0", "@types/chai": "^4.2.15", "@types/mocha": "^8.2.1", "@types/node": "^16.18.108", "@vue/cli-plugin-babel": "^5.0.8", "@vue/cli-plugin-router": "^5.0.8", "@vue/cli-plugin-typescript": "~5.0.0", "@vue/cli-plugin-unit-mocha": "^5.0.8", "@vue/cli-plugin-vuex": "^5.0.8", "@vue/cli-service": "^5.0.8", "@vue/preload-webpack-plugin": "^2.0.0", "@vue/test-utils": "^1.3.6", "awe-dnd": "^0.3.4", "babel-plugin-component": "^1.1.1", "babel-plugin-import": "^1.13.8", "buffer": "^6.0.3", "chai": "^4.5.0", "compression-webpack-plugin": "^11.1.0", "cross-env": "^7.0.3", "crypto-browserify": "^3.12.1", "css-loader": "^7.1.2", "cssnano": "^7.0.6", "dom-to-image-hm": "^2.6.1", "echarts": "^5.5.1", "file-loader": "^6.2.0", "https-browserify": "^1.0.0", "less": "^4.2.0", "less-loader": "^12.2.0", "lodash": "^4.17.21", "mini-css-extract-plugin": "^2.9.1", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", "process": "^0.11.10", "sass": "^1.79.4", "sass-loader": "^16.0.2", "sockjs-client": "^1.6.1", "stream-browserify": "^3.0.0", "stream-http": "^3.2.0", "string-replace-loader": "^3.2.0", "terser-webpack-plugin": "^5.3.10", "timers-browserify": "^2.0.12", "typescript": "~4.5.5", "url": "^0.11.4", "url-loader": "^4.1.1", "util": "^0.12.5", "vm-browserify": "^1.1.2", "vue-amap": "^0.5.10", "vue-style-loader": "^4.1.3", "vue-template-compiler": "^2.7.16", "webpack-bundle-analyzer": "^4.10.2" }, "browserslist": [ "> 1%", "last 2 versions", "not dead" ] } 那个是我的node版本这是package.json 文件
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值