title: py-auto-i18n date: 2024-10-04T21:41:08.000Z lastmod: 2024-10-04T22:20:08.000Z document: 20241004214108-5aymt55 notebook: Life hpath: /我的项目/py-auto-i18n export: '2024-10-04 22:20:16'
auto-i18n
auto-i18n 是一个基于 python 开发的命令行工具,旨在简化项目中的国际化(i18n)过程。它利用 GPT 自动化翻译并从代码中提取可翻译内容。
特性
- 自动从代码中提取可翻译字符串
- 自动生成 I18n 变量替换原始的文本
- 使用 GPT 翻译 i18n 文件
- 灵活的配置选项,适用于全局和项目特定设置
快速开始
1. 初始化配置
首先安装 auto-i18n:
pip install auto-i18n
安装后,你可以使用 i18n 或者 auto-i18n 来运行命令
2. 配置 GPT 参数
auto-i18n 使用 GPT 来翻译,需要设置一下 GPT 的参数。运行以下命令:
i18n config set --global GPT.endpoint "https://api.openai.com/v1/chat/completions"
i18n config set --global GPT.key "你的_API_密钥"
i18n config set --global GPT.model "模型名称"
运行 testgpt 命令测试一下 GPT 是否配置正确:
> i18n testgpt
Testing GPT, send: Hello, how are you?
GPT response: Hello! I'm here and ready to help. How can I assist you today?
auto-i18n 默认使用英文,如果你想要使用中文,可以运行
i18n config set --global lang "zh_CN"
3. 在你的项目中初始化
在你需要配置 i18n 的项目的根目录下,运行以下命令:
i18n init
这个命令会创建一个 auto-i18n.project.yaml 文件。
创建完成后,你首先应该根据自己项目的情况修改这几个字段:
code_files: # 请在此处配置你的代码文件 glob 表达式
- 'src/**/*.ts'
- 'src/**/*.svelte'
i18n_dir: src/i18n # 指定 i18n 文件的存放目录
main_file: zh_CN.yaml # 指定主语言文件名
i18n_pattern: \(\(`(.+?)`\)\) # 指定需要翻译的文本的匹配模式,见下一节
4. 在项目代码中直接书写文本
auto-i18n 基于模板匹配 + 字符串替换的方式来自动提取和翻译文本。在开发的时候,你需要直接在你的项目中使用符合 i18n_pattern 语法的字面量。
((`文本内容`))
例如这样(以下以一个前端项目为例)
// src/test.ts
import { i18n } from 'somewhere-in-your-project';
const main = () => {
console.log( ((`初始化成功,请继续`)) );
}
<!-- src/sample.svelte -->
<script>
import { i18n } from 'somewhere-in-your-project';
</script>
<div>
{ ((`欢迎来到 Auto-i18n`)) }
</div>
5. 自动提取 i18n 文本
在项目目录下运行:
i18n extract
程序会自动扫描所有匹配到的文本,并使用 GPT 来生成合适的 i18n 变量名称,写入的 main_file (如:zh_CN.yaml 文件)中。
hello: 你好
samplesvelte:
welcometoautoi18n: 欢迎来到 Auto-i18n
testts:
initsuccesspleasecontinue: 初始化成功,请继续
同时,原本的 i18n 文本会自动被替换为对应的变量:
// src/test.ts
import { i18n } from 'somewhere-in-your-project';
const main = () => {
console.log( i18n.testts.initsuccesspleasecontinue );
}
<!-- src/sample.svelte -->
<script>
import { i18n } from 'somewhere-in-your-project';
</script>
<div>
{ i18n.samplesvelte.welcometoautoi18n }
</div>
被替换的变量有三个部分组成:
-
i18n_var_prefix: 可以在项目配置文件中设置 -
filename: 一个基于当前文件名,只包含字母、数字的字面量 -
i18n_var_name: 由 GPT 生成的变量名称如果变量名称出现了冲突,程序会自动的变量后面加上数字以避免命名冲突
6. 翻译 i18n 文本
运行以下命令翻译 i18n 文本:
i18n translate
程序会:
- 读取
main_file 中的文本,如 zh_CN.json 文件 - 扫描
i18n_dir 下所有的其他语言的文件,如同目录的 en_US.json, ja_JP.json 文件 - 使用 GPT 翻译内容,并合并到其他语言的文件中
# en_US.yaml
hello: Hello
samplesvelte:
welcometoautoi18n: Welcome to Auto-i18n
testts:
initsuccesspleasecontinue: Initialization succeeded, please continue
7. 导出
使用 export 命令,可以将主 i18n 文件导出为其他格式,目前支持 TypeScript 接口 (.d.ts)。
i18n export
该命令会默认将导出的文件写入到项目目录下,你可以在 auto-i18n.project.yaml中配置 export_dir来指定其他目录。
配置选项
全局配置
存放在 ~/.config/auto-i18n/global-config.yaml 文件中。
GPT:
endpoint:
key:
model:
prompt:
autokey:
translate:
lang:
-
GPT.endpoint: GPT API 的地址 -
GPT.key: GPT API 的密钥 -
GPT.model: GPT 模型名称 -
prompt.autokey: 用于自动生成的 i18n 变量前缀名称的 prompt -
prompt.translate: 用于翻译文本的 prompt -
lang: 使用的语言,可选为en_US 和zh_CN
项目级配置
通过 init 命令创建的 auto-i18n.project.yaml 文件内容如下:
code_files:
- '**/*.ts'
- '**/*.svelte'
i18n_dir: temp/i18n
main_file: zh_CN.yaml
dict: {}
i18n_pattern: \(\(`(.+?)`\)\)
i18n_var_prefix: i18n
strategy: diff
export_dir:
-
i18n_dir: 存放翻译文件的目录 -
main_file: 主要语言的翻译文件 -
code_files: 需要扫描的代码文件类型 -
i18n_pattern: 在代码中标记需要翻译的文本的模式 -
dict: 特殊词汇的翻译对照表;你可以把项目中涉及到的一些属于翻译写在这个地方 -
strategy: 翻译策略"diff" 表示只翻译新增的内容"full" 表示翻译所有内容
-
i18n_var_prefix: 在代码中使用的替换变量的前缀 -
export_dir: 导出目录,如果设置,将用作 export 命令的输出目录 i18n_var_mid: i18n 键的中间部分生成策略。选项包括:"filename": 使用完整文件名,包含扩展名,如utilsts"filename_noext": 使用不带扩展名的文件名"pathname": 使用文件的相对路径
覆盖全局配置
在项目配置中,可以在 global_config 字段中覆盖全局配置,例如这样:
code_files:
- '**/*.ts'
- '**/*.svelte'
i18n_dir: temp/i18n
main_file: zh_CN.yaml
dict: {}
i18n_pattern: \(\(`(.+?)`\)\)
i18n_var_prefix: i18n
strategy: diff
global_config:
GPT:
endpoint: "https://api.openai.com/v1/chat/completions"
key: "你的_API_密钥"
model: "模型名称"
其他说明
默认的 prompt.autokey
## Task Description - Background: You are developing a project which need to using i18n variables for internationalization. - Task: 1. Read all the [## i18n text] 2. Generate appropriate key names based on the content of each text 3. Summarize the results into a JSON - **Output Format Requirements**: - Retaining JSON format - Output the JSON code directly, without attaching the ```json``` code block identifier - **Key Name Requirements**: - **Only lowercase English letters and numbers are allowed**, no other special symbols (such as spaces, -, underscores, etc.) - E.g. "greeting" and "invalidinputnumber" are valid, while "welcome_here", "invalid-input-number", and "非英文字符" are not valid - **Keep short and concise**, each key name within 15 characters, upmost to 25 characters, it is ok to scacrifice readability for brevity. ## i18n Text ```txt {lines} ``` ## An example, for reference only! Input: ```txt Hello {0} Warning! Please do not enter numbers outside 0-10! ``` Output { "greeting": "Hello {0}", "invalidinputnumber": "Warning! Please do not enter numbers outside 0-10!" }
本 prompt 在运行时将会替换如下变量:
-
{lines}:替换为在源代码文件中找到的所有 i18n 文本-
例如:如果源代码为:
console.log(((`你好啊`))) ele.innerText = ((`警告!`))
-
则
{lines} 会被替换为你好啊 警告!
-
默认的 prompt.translate
## Task Description - Task: Translate the content of the i18n file {InFile} (see [## i18n Content]) to another language (file {OutFile}). - Requirements: - Target language file: {OutFile} - Output format: JSON code, please retaining JSON format - Output the translated JSON code directly, without attaching the ```json``` code block identifier ## Vocabulary {Dict} ## i18n Content ```json {I18n} ```
本 prompt 在运行时将会替换如下变量:
-
{InFile}:你的主 i18n 文件的文件名,例如zh_CN.json -
{OutFile}:将要翻译的目标 i18n 文件,例如ja_JP.json -
{Dict}: Project 配置中的dict 字段 -
{I18n}: 需要被翻译的 i18n 对应的 json 字符串
Metadata
Release files for auto-i18n 1.6.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| auto_i18n-1.6.4.tar.gz | 23.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| auto_i18n-1.6.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 48.8 kB
Release files / auto_i18n-1.6.4.tar.gz
| Download URL | auto_i18n-1.6.4.tar.gz |
|---|---|
| Size | 23.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d4ea3382a34d9e91cb30ddfb9fb0fd44bce2ca10b3baff8bf7c169e6562ff6a1
|
|
BLAKE2b-256 checksum How to use checksums |
2fbcf8a64bd385f481e5f0f90ce7ed8238230f68b65d1989b1314256d94e4c88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.21.0 CPython/3.10.0 Windows/10
|
Release files / auto_i18n-1.6.4-py3-none-any.whl
| Download URL | auto_i18n-1.6.4-py3-none-any.whl |
|---|---|
| Size | 25.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
22fd5fb4594b57aed3c2221d3a85710dae7ead4ab8e1730a5300b32d1e8c867d
|
|
BLAKE2b-256 checksum How to use checksums |
9d195afec011c021648dfa9edcea5b50cb4d2023a92e628502b72ba034172da1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.21.0 CPython/3.10.0 Windows/10
|