# i18n-ast-auto

> auto i18n

Latest version **0.0.1** (published 2023-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install i18n-ast-auto
pnpm add i18n-ast-auto
yarn add i18n-ast-auto
bun add i18n-ast-auto
```

Provides the command `i18n`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; large bundle; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-11-20 |
| First published | 2023-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 14 |
| Unpacked size | 26.3 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | kinfuy |
| Maintainers | alqmc |

## Links

- npm: https://www.npmjs.com/package/i18n-ast-auto
- npm.io page: https://npm.io/package/i18n-ast-auto

## Dependencies (14)

- [cac](https://npm.io/package/cac.md) 6.7.14
- [vue](https://npm.io/package/vue.md) ^3.3.4
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [kolorist](https://npm.io/package/kolorist.md) ^1.5.1
- [fast-glob](https://npm.io/package/fast-glob.md) ^3.3.1
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.22.20
- [blueimp-md5](https://npm.io/package/blueimp-md5.md) ^2.19.0
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.12.12
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.22.16
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.22.15
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.22.20
- [@babel/generator](https://npm.io/package/@babel/generator.md) ^7.22.15
- [@vue/compiler-sfc](https://npm.io/package/@vue/compiler-sfc.md) ^3.3.4
- [@vue/compiler-core](https://npm.io/package/@vue/compiler-core.md) ^3.3.4

## Recent versions

- 0.0.1 (latest) — 2023-11-20

## README

# i18n-ast-auto

<p align="center">
  <a href="https://www.npmjs.org/package/i18n-ast-auto">
  <img src="https://img.shields.io/npm/v/i18n-ast-auto.svg">
  </a>
  <a href="https://npmcharts.com/compare/i18n-ast-auto?minimal=true">
  <img src="https://img.shields.io/npm/dm/i18n-ast-auto.svg?color=357C3C">
  </a>
  <a href="https://npmcharts.com/compare/i18n-ast-auto?minimal=true">
  <img src="https://img.shields.io/npm/l/i18n-ast-auto.svg?color=blue">
  </a>
  <a href="https://github.com/kinfuy/i18n-ast" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/alqmc/i18n-ast-auto?style=social">
  
  </a>
  <br>
</p>

基于 AST 的国际化解决方案、自动提取和替换国际化词典工具

## 功能

- 支持 include exclude 配置按需替换
- 支持@i18n-ignore 忽略文案
- vue 自动引入 vue-i18n
- vue 自动声明 t 函数 `const { t } = useI18n()`
- vue 自动转化 vue 语法
- vue 支持 setup 语法糖
- js 自动引入国际化相关函数
- js 自定义调用方式
- js 自动转化 js 语法

## 效果

#### i18n scan

![scan](https://user-images.githubusercontent.com/37766068/284317518-04df92c9-263d-43f9-9c73-a359dafdb564.png)

#### i18n replace

![replace](https://user-images.githubusercontent.com/37766068/284317603-7195a4a3-427a-4a45-83bb-25ab423eca6a.png)

## 使用

### 1、在根目录放置配置文件

// i18n.config.js

```js
module.exports = {
  output: {
    flat: true,
    localeOutput: 'playground/locales/lang',
    rootPath: 'playground',
    overlap: 'playground',
    writeMode: 'overwrite',
  },
  include: ['playground/test.vue'],
  logLevel: 5,
  // include: ['playground/**/*.{js,vue}'],
  // include: ['playground/**/*.{js,vue}'],
  exclude: ['playground/output'],
  transform: {
    keyMode: 'FILEPATH',
    locales: [],
    displayName: 'TEXT',
    import: [
      {
        importVar: 'useI18n ',
        importPath: 'vue-i18n',
        defaultExport: false,
        userVar: {
          script: 't',
          template: '$t',
        },
        useing: 'const { t } = useI18n()',
        effect: '.vue',
      },
      {
        importVar: 'i18n',
        importPath: '@/i18n',
        defaultExport: true,
        userVar: 'i18n.global.t',
        useing: 'const { t } = useI18n()',
        effect: '.js',
      },
    ],
  },
};
```

### 2、全局安装

```sh
pnpm add i18n-ast-auto -g
```

### 3、运行 i18n scan 扫描

```sh
i18n scan
```

### 4、去 .i18n/keyMap.json 修改 locale key

i18n scan 命令运行后会自动生成.i18n/keyMap.json 文件记录中文映射关系，你可以通过修改 displayName 改变替换后的 key

### 5、开始替换

```sh
i18n replace
```

运行 i18n replace 开始替换，该命令会基于 keyMap 替换相关文件，并转化语法，已经转换后的文件会记录在.i18n/skipFile.json 不会二次替换

### 支持

- 目前仅支持 vue3 与 js

## 常见问题

### 需要手动修改的场景

1、vue 语法现在不支持中文出现在 props 与 defineComponent 外中

### 需要注意的情况，可能不符合预期

1、嵌套复杂的三元表达式可能不符合预期

2、在 js 文件中拼接字符串中出现 html 标记，当前会自动转移 html 字符

---
_Source: https://npm.io/package/i18n-ast-auto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
