Skip to content

Commit 6473b95

Browse files
committed
feat: add auto-i18n
1 parent 0cfd883 commit 6473b95

File tree

57 files changed

+2701
-641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2701
-641
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ docs/.vuepress/dist
1616
docs/.vuepress/public/tools/realcugan-ncnn-webassembly*
1717
docs/.vuepress/public/tools/up2x*
1818
docs/.vuepress/public/tools/up3x*
19+
processed_list.txt

.markdownlint.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"MD024": {
1111
"allow_different_nesting": true
1212
},
13+
"MD028": false,
1314
"MD033": {
1415
"allowed_elements": [
1516
"br",
@@ -26,5 +27,6 @@
2627
"style": "---"
2728
},
2829
"MD040": false,
30+
"MD045": false,
2931
"MD046": false
3032
}

Auto-i18n/.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto eol=lf

Auto-i18n/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
env.py
2+
__pycache__
3+
processed_list.txt
4+
testdir/docs/
5+
.vscode/settings.json

Auto-i18n/CONTRIBUTING.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 贡献指南
2+
3+
感谢你对本项目的兴趣和参与!我们欢迎所有形式的贡献。请阅读以下指南,以了解如何为项目做出贡献。
4+
5+
## 提交问题
6+
7+
如果你发现了问题,或者有建议和改进的想法,请在 [Issues](https://github.com/linyuxuanlin/Auto-i18n/issues) 中提出。在提交问题时,请提供尽可能多的上下文信息,以便我们能更好地理解问题。
8+
9+
## 提出建议
10+
11+
如果你有新功能的想法或改进建议,我们欢迎你的建议。请在 [Issues](https://github.com/linyuxuanlin/Auto-i18n/issues) 中提出你的建议,并确保描述得尽可能清晰详细。
12+
13+
## 参与代码贡献
14+
15+
如果你希望参与代码贡献,请按照以下步骤进行:
16+
17+
1. 克隆项目仓库。
18+
2. 创建你的分支,并确保你的更改不会破坏现有代码。
19+
3. 提交代码更改,并确保提交信息清晰和有意义。
20+
4. 通过代码审查,确保你的更改符合项目标准。
21+
5. 在代码被合并之前,请确保通过项目的测试。
22+
23+
## 许可证
24+
25+
通过参与项目,你同意你的贡献将受到项目的许可证约束。详情请查看 [许可证文件](LICENSE)
26+
27+
## 行为准则
28+
29+
我们鼓励友好、尊重和开放的沟通和协作。
30+
31+
## 联系方式
32+
33+
如果你需要与项目维护者联系或获取更多信息,请发送电子邮件至 [[email protected]]
34+
35+
## 更新历史
36+
37+
- 2023-10-15: 初始化贡献指南

Auto-i18n/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 linyuxuanlin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Auto-i18n/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Auto-i18n:使用 ChatGPT 的自动多语言翻译工具
2+
3+
Auto-i18n 是一个使用 ChatGPT 自动将 Markdown 文件批量翻译为多语言的工具。它实现了博客文章 i18n(Internationalization) 的全自动化。你仅需将博文推送至 GitHub 仓库,即可借助 GitHub Actions 实现自动转译为多种语言。(目前支持英语、西班牙语和阿拉伯语,后续将提供更多语言支持)
4+
5+
Auto-i18n 的主要特性:
6+
7+
- **批量多语言翻译**:Auto-i18n 提供了批量翻译的功能,使你能够将一整个路径下的所有 Markdown 文档一次性翻译多语言,极大地提高了多语言化项目的效率。
8+
- **兼容 Front Matter**:Auto-i18n 兼容 Markdown Front Matter 语法,你可以自定义不同字段的翻译或替换规则。
9+
- **固定内容替换**:Auto-i18n 还支持固定内容替换。如果你希望文档中一些重复字段的译文保持不变,这个功能可以帮助你实现文档的一致性。
10+
- **自动化工作流**:你可以使用 GitHub Actions 实现自动化的翻译流程"。。无需手动干预,翻译工作会自动进行并更新文档,使你能够更专注于内容。
11+
12+
## 快速上手
13+
14+
1. 将仓库克隆到本地,把 `env_template.py` 重命名为 `env.py`,并提供你的 ChatGPT API。如果你没有自己的 API,可以到 [GPT_API_free](https://github.com/chatanywhere/GPT_API_free) 申请到一个免费的;也可以借助 [go-chatgpt-api](https://github.com/linweiyuan/go-chatgpt-api) 把网页版 ChatGPT 转 API 使用。
15+
2. 安装必需的模块:`pip install -r requirements.txt`
16+
3. 执行命令 `python auto-translater.py` 运行程序,它将会自动处理测试目录 `testdir/to-translate` 下的所有 Markdown 文件,批量翻译为英语、西班牙语、阿拉伯语。(后续将提供更多语言支持)
17+
18+
## 详细描述
19+
20+
程序 `auto-translater.py` 的运行逻辑如下:
21+
22+
1. 程序将自动处理测试目录 `testdir/to-translate` 下的所有 Markdown 文件,你可以在 `exclude_list` 变量中排除不需要翻译的文件。
23+
2. 处理后的文件名会被记录在自动生成的 `processed_list.txt` 中。下次运行程序时,已处理的文件将不会再次翻译。
24+
3. 对于原本使用英文撰写的文章,程序不会重新翻译成英文,也不会翻译回中文,而会翻译为其他语言。你需要在文章中添加字段 `> This post was originally written in English.`(注意在上下各留一个空行),以便程序识别。请参考 [测试文章\_en.md](https://github.com/linyuxuanlin/Auto-i18n/blob/main/testdir/to-translate/测试文章_en.md)
25+
4. 如果需要重新翻译指定文章(例如,翻译结果不准确,或文章内容发生更改等),你可以在文章中加入字段 `[translate]`(同样需要在上下各留一个空行)。这将会忽略 `exclude_list``processed_list` 的规则,强制进行翻译处理。请参考 [测试文章\_force-mark.md](https://github.com/linyuxuanlin/Auto-i18n/blob/main/testdir/to-translate/测试文章_force-mark.md)
26+
5. 如果 Markdown 文件中包含 Front Matter,将按照程序内的规则 `front_matter_translation_rules` 选择以下处理方式:
27+
1. 自动翻译:由 ChatGPT 翻译。适用于文章标题或文章描述字段。
28+
2. 固定字段替换:适用于分类或标签字段。例如同一个中文标签名,不希望被翻译成不同的英文标签造成索引错误。
29+
3. 不做任何处理:如果字段未出现在以上两种规则中,将保留原文,不做任何处理。适用于日期、url 等。
30+
31+
## GitHub Actions 自动化指南
32+
33+
你可以在自己项目仓库下创建 `.github/workflows/ci.yml`,当检测到 GitHub 仓库更新后,可以使用 GitHub Actions 自动进行翻译处理,并自动 commit 回原仓库。
34+
35+
`ci.yml` 的内容可参考模板:[ci_template.yml](https://github.com/linyuxuanlin/Auto-i18n/blob/main/ci_template.yml)
36+
37+
你需要在仓库的 `Settings` - `Secrets and variables` - `Repository secrets` 中添加两个 secrets:`CHATGPT_API_BASE``CHATGPT_API_KEY`,并在程序 `auto-translater.py` 中将 `import env` 语句注释掉。
38+
39+
## 错误排除
40+
41+
1. 如果需要验证 ChatGPT API key 的可用性,可以使用程序 [verify-api-key.py](https://github.com/linyuxuanlin/Auto-i18n/blob/main/Archive/verify-api-key.py) 进行测试。如果在国内使用官方 API,需要有本地代理。
42+
2. 如果 Markdown 中的 Front Matter 无法被正常识别,可以使用程序 [detect_front_matter.py](https://github.com/linyuxuanlin/Auto-i18n/blob/main/Archive/detect_front_matter.py) 测试。
43+
3. 在使用 GitHub Actions 遇到问题时,请优先检查路径引用是否正确(例如 `dir_to_translate` `dir_translated_en` `dir_translated_es` `dir_translated_ar` `processed_list`)。
44+
45+
## 待解决的问题
46+
47+
1. 在某些特殊的情况下,可能会出现翻译不准确、或某些字段没有翻译的情况,建议翻译后手动校验再发布文章。
48+
2. (已解决)~~如果 Markdown 中包含 Front Matter,将保留 Front Matter 的原始内容。Front Matter 部分参数翻译的功能正在开发中。~~
49+
50+
## 贡献
51+
52+
欢迎你参与本项目的改进!如果您想要贡献代码、报告问题或提出建议,请查看 [贡献指南](https://github.com/linyuxuanlin/Auto-i18n/blob/main/CONTRIBUTING.md)
53+
54+
## 版权和许可
55+
56+
本项目采用 [MIT 许可证](https://github.com/linyuxuanlin/Auto-i18n/blob/main/LICENSE)
57+
58+
## 问题和支持
59+
60+
如果你在使用 Auto-i18n 时遇到任何问题,或者需要技术支持,请随时 [提交问题](https://github.com/linyuxuanlin/Auto-i18n/issues)
61+
62+
我的博客使用 Auto-i18n 实现了多语言支持,你可以到 [Power's Wiki](https://wiki-power.com) 查看 Demo 效果。
63+
64+
[![](https://wiki-media-1253965369.cos.ap-guangzhou.myqcloud.com/img/202310222223670.png)](https://wiki-power.com)
65+
66+
## 致谢
67+
68+
- 感谢 [chatanywhere/GPT_API_free](https://github.com/chatanywhere/GPT_API_free) 提供的免费 ChatGPT API key。
69+
- 感谢 [linweiyuan/go-chatgpt-api](https://github.com/linweiyuan/go-chatgpt-api) 提供的把网页版 ChatGPT 转 API 的方法。
70+
71+
[![Star History Chart](https://api.star-history.com/svg?repos=linyuxuanlin/Auto-i18n&type=Date)](https://star-history.com/#linyuxuanlin/Auto-i18n&Date)

0 commit comments

Comments
 (0)