babel 处理中文,快速制作国际化

code-i18n

What is this?

code-i18n is a script that converts Chinese in the code into executable functions [图片上传失败...(image-87c9ba-1626495615910)]

Type

Languages currently supported for conversion

Type Support
js * [x]
jsx * [x]
ts * [x]
tsx * [x]
vue * [ ]

Usage

code-i18n exports a convenience function, the parameters are source and config, the return value is an object, please see Documentation for details.

import { transformCode } from 'code-i18n'
const source = 'const language = "中文"'

const { code } = transformCode(source, {
  type: 'js',
})
console.log(code) // const language = $t('StringLiteral_17_21');
console.log(stack) // [ { StringLiteral_17_21: "中文" } ]

Installation

yarn add -D code-i18n

Documentation

interface Config {
  ruleKey?: (node: t.Node) => string | number
  readonly identifier?: string
  type: 'js' | 'jsx' | 'ts' | 'tsx' | 'vue'
}
export declare function transformCode(
  code: string,
  config: Config
): {
  code: string
  stack: Record<string, string>[]
}

Features

  • StringLiteral
  • TemplateLiteral
  • JSXText
  • JSXAttribute

Tests

yarn test

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容