美文网首页
babel 处理中文,快速制作国际化

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

作者: 左手与右脑 | 来源:发表于2021-07-17 12:21 被阅读0次

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

相关文章

网友评论

      本文标题:babel 处理中文,快速制作国际化

      本文链接:https://www.haomeiwen.com/subject/txkwpltx.html