Skip to content

Commit

Permalink
feat: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
甄帅 committed May 10, 2021
1 parent 4b7353c commit 1970ec4
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 4 deletions.
Binary file modified .DS_Store
Binary file not shown.
22 changes: 19 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@

帮助你处理 i18n 的工具,可以提取代码的翻译项,可以导入导出到 excel 文件,可以生成 json 翻译资源。

全局参数:

- `--dry-run` 演习运行,用来效验和发现错误,如下。
- 翻译同 key 不同翻译
- 翻译 key 父子冲突
- 工具会匹配代码格式为`i18nTranslate(langKey, langText, langTextOptions)`

```javascript
import React from 'react';
import i18n from 'i18next';

function i18nTranslate(key: string, text: string, option: {}) {
// your code
// ...
return i18n.t(key);
}

export default function BatchSendPage() {
return <div>{i18nTranslate('common.name', '我的名字{name}', { name: 'xiaohong' })}</div>;
}
```

# 支持的命令

## 命令`code2excel`

从代码中筛选出翻译项,然后输入到 excel 文件中。
从代码中筛选出翻译项,然后输入到 excel 文件中(多次导出到 excel,新增项会追加到最后)

### 参数

Expand Down Expand Up @@ -51,3 +65,5 @@
-el, --excel-lang-column-index <number> 翻译内容对应列序号(从 1 开始)
-jp, --json-path <path> json 文件路径
```

# 常见问题
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@
Help you deal with I18N internationalization, extraction from code based on Babel, import and export excel, generate translation JSON file.

- dry-run option, run command without saving the changes to the file system.
- Extract from code, matching paradigm `i18nTranslate(langKey, langText, langOptions)`.
- Error message
- Different text from key.
- Father son conflict.
- Extract from code, matching paradigm `i18nTranslate(langKey, langText, langTextOptions)`

```javascript
import React from 'react';
import i18n from 'i18next';

function i18nTranslate(key: string, text: string, option: {}) {
// your code
// ...
return i18n.t(key);
}

export default function BatchSendPage() {
return <div>{i18nTranslate('common.name', '我的名字{name}', { name: 'xiaohong' })}</div>;
}
```

# All commands

Expand Down
Empty file added scripts/test.js
Empty file.
12 changes: 12 additions & 0 deletions src/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class CodeServiceFactory {

const shortFile = file.replace(this.config.codeFolderPath, '');

term(`=> Code: file ${shortFile}\n`);

const ast = babelParse(fileContent, {
sourceType: 'module',
plugins: ['jsx', 'typescript', 'classProperties', 'decorators-legacy'],
Expand All @@ -76,6 +78,16 @@ class CodeServiceFactory {
let rawKey = args[0].value;
let text;

if (!args[1]) {
this.errorTranslations.push({
rawKey,
text,
file,
shortFile,
error: '没有找到翻译默认值'
});
return;
}
if (args[1].type !== 'StringLiteral') {
text = babelGenerator(args[1]).code;
} else {
Expand Down
Binary file added test/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions test/demo/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a1 = i18nTranslate('common.name1');
const a2 = i18nTranslate('common.name2', '我的名字{name}');
const a3 = i18nTranslate('common.name3', '我的名字{name}', { name: 'xiaohong' });
7 changes: 7 additions & 0 deletions test/demo/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

function b() {
return <div>{i18nTranslate('common.name4', '我的名字{name}', { name: 'xiaohong' })}</div>;
}

export default b;
Binary file added test/excel/t.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions test/lang/zh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"common": {
"name2": "我的名字{name}",
"name3": "我的名字{name}",
"name4": "我的名字{name}"
}
}

0 comments on commit 1970ec4

Please sign in to comment.