npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@crasff/intl-generator

v0.0.7

Published

国际化语言包生成工具

Downloads

16

Readme

intl-generator

NPM version NPM downloads

国际化语言包生成工具

介绍

intl-generator工具是一个用于解决开发时,国际化语言包难于维护,减少因切换目录亦影响开发进度

目前默认自动识别一下模式intl.formatMessage({id:xxxx})

默认使用id后的语言做语言包的key值

默认的识别的范围时项目下 src, 生成语言en-US.tszh-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`,
  },
};