@crasff/intl-generator
v0.0.7
Published
国际化语言包生成工具
Downloads
16
Readme
intl-generator
国际化语言包生成工具
介绍
intl-generator工具是一个用于解决开发时,国际化语言包难于维护,减少因切换目录亦影响开发进度
目前默认自动识别一下模式intl.formatMessage({id:xxxx})
默认使用id后的语言做语言包的key值
默认的识别的范围时项目下 src, 生成语言en-US.ts、zh-CN.ts,当然这些都是可配的,包括上方识别取值规则
暂时没有内置翻译,只是自动生成文件,翻译对接接口可后续根据业务进行配置
初始化
npm install @crasff/intl-generator
yarn add @crasff/intl-generator
pnpm i @crasff/intl-generator
常用命令
在package.json 中的 scripts字段配置以下命令
{
//...
scripts: {
// 扫描项目全局,识别国际化标识,生成语言包
"intl:scan": "intl-generator",
// 监听项目文件变化自动生成,时机为保存时
"intl:watch": "intl-generator --watch",
// 清除未被项目引用的语言包中的标识
"intl:clean": "intl-generator --clean",
}
}
配置文件
可通过在项目根目录中新增intlGen 配置文件,对脚本进行配置,支持一下文件类型
- intlGen.ts
- intlGen.js
- intlGen.yaml
- intlGen.json
配置项
| 属性名 | 描述 | 类型 | 默认值 |
| ---- | ---- | ---- | ---- |
| document | 项目文件识别正则 | String
| ./src/**/*.ts?(x)
|
| matchPattern | 国际化识别并提取key正则,需包含子表达式,用于提取key | String
| intl.formatMessage\\({[^{]*id:\\s?['"]([\\s\\S]*?)['"]
|
| locales | 多语言类型数组 | String[]
| ['zh-CN', 'en-US']
|
| translate | 翻译扩展接口,locale为配置的多语言类型,texts为在项目中识别出来的中文key值,最终在等量promise返回翻译。默认不做翻译 | (locale:String, texts:String[]) => Promise<String[]>
| -- |
| output | 输出配置。 mode
为输出路径是绝对路径或是相对路径; path
为输出路径; filename
根据语言类型生成文件名 | {mode:String,path:String,filename:(locale:String)=>String}
| {mode: 'relative',path: './src/locales',filename: (locale) => locale+'.ts'}
|
配置项参考DEMO
以下亦是默认配置
// intlGen.ts
export default {
document: './src/**/*.ts?(x)',
matchPattern: `intl.formatMessage\\({[^{]*id:\\s?['"]([\\s\\S]*?)['"]`,
locales: ['zh-CN', 'en-US'],
translate: (locale, texts) => Promise.resolve(texts),
output: {
mode: 'relative',
path: './src/locales',
filename: (locale) => `${locale}.ts`,
},
};