{"id":22225184,"url":"https://github.com/frostime/py-auto-i18n","last_synced_at":"2026-03-04T11:01:59.088Z","repository":{"id":265183354,"uuid":"867419930","full_name":"frostime/py-auto-i18n","owner":"frostime","description":"A CLI program based on python, and using GPT to help you deal with i18n","archived":false,"fork":false,"pushed_at":"2025-03-16T09:09:06.000Z","size":153,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-27T17:54:49.440Z","etag":null,"topics":["cli","gpt","i18n","internationalization","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/auto-i18n","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frostime.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-04T03:13:47.000Z","updated_at":"2025-03-16T09:09:02.000Z","dependencies_parsed_at":"2025-03-16T10:20:16.647Z","dependency_job_id":"3409d40b-fea1-4e82-adb4-bdba15285f3b","html_url":"https://github.com/frostime/py-auto-i18n","commit_stats":null,"previous_names":["frostime/py-auto-i18n"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/frostime/py-auto-i18n","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fpy-auto-i18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fpy-auto-i18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fpy-auto-i18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fpy-auto-i18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frostime","download_url":"https://codeload.github.com/frostime/py-auto-i18n/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fpy-auto-i18n/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30078408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T08:01:56.766Z","status":"ssl_error","status_checked_at":"2026-03-04T08:00:42.919Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","gpt","i18n","internationalization","python"],"created_at":"2024-12-03T00:15:56.577Z","updated_at":"2026-03-04T11:01:59.056Z","avatar_url":"https://github.com/frostime.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: py-auto-i18n\ndate: 2024-10-04T21:41:08.000Z\nlastmod: 2024-10-04T22:20:08.000Z\ndocument: 20241004214108-5aymt55\nnotebook: Life\nhpath: /我的项目/py-auto-i18n\nexport: '2024-10-04 22:20:16'\n---\n\n# auto-i18n\n\n[English README](README_en_US.md)\n\nauto-i18n 是一个基于 python 开发的命令行工具，旨在简化项目中的国际化（i18n）过程。它利用 GPT 自动化翻译并从代码中提取可翻译内容。\n\n## 特性\n\n* 自动从代码中提取可翻译字符串\n* 自动生成 I18n 变量替换原始的文本\n* 使用 GPT 翻译 i18n 文件\n* 灵活的配置选项，适用于全局和项目特定设置\n\n## 快速开始\n\n### 1. 初始化配置\n\n首先安装 auto-i18n：\n\n```bash\npip install auto-i18n\n```\n\n安装后，你可以使用 `i18n`​ 或者 `auto-i18n`​ 来运行命令\n\n### 2. 配置 GPT 参数\n\nauto-i18n 使用 GPT 来翻译，需要设置一下 GPT 的参数。运行以下命令：\n\n```bash\ni18n config set --global GPT.endpoint \"https://api.openai.com/v1/chat/completions\"\ni18n config set --global GPT.key \"你的_API_密钥\"\ni18n config set --global GPT.model \"模型名称\"\n```\n\n\u003e [!NOTE]\n\u003e v1.6.0 版本之前，全局配置信息保存在 `~/.auto-i18n.yaml`​ 文件中。\n\u003e\n\u003e v1.6.0 版本开始，配置文件会自动迁移到 `~/.config/auto-i18n/global-config.yaml`​ 中。\n\n运行 `testgpt`​ 命令测试一下 GPT 是否配置正确：\n\n```bash\n\u003e i18n testgpt\n\nTesting GPT, send: Hello, how are you?\nGPT response: Hello! I'm here and ready to help. How can I assist you today?\n```\n\n---\n\n​`auto-i18n`​ 默认使用英文，如果你想要使用中文，可以运行\n\n```bash\ni18n config set --global lang \"zh_CN\"\n```\n\n### 3. 在你的项目中初始化\n\n在你需要配置 i18n 的项目的根目录下，运行以下命令：\n\n```bash\ni18n init\n```\n\n这个命令会创建一个 `auto-i18n.project.yaml`​ 文件。\n\n创建完成后，你首先应该根据自己项目的情况修改这几个字段:\n\n```yaml\ncode_files:  # 请在此处配置你的代码文件 glob 表达式\n- 'src/**/*.ts'\n- 'src/**/*.svelte'\ni18n_dir: src/i18n  # 指定 i18n 文件的存放目录\nmain_file: zh_CN.yaml  # 指定主语言文件名\ni18n_pattern: \\(\\(`(.+?)`\\)\\)  # 指定需要翻译的文本的匹配模式，见下一节\n```\n\n### 4. 在项目代码中直接书写文本\n\n​`auto-i18n`​ 基于模板匹配 + 字符串替换的方式来自动提取和翻译文本。在开发的时候，你需要直接在你的项目中使用符合 `i18n_pattern`​ 语法的字面量。\n\n```\n((`文本内容`))\n```\n\n例如这样（以下以一个前端项目为例）\n\n```ts\n// src/test.ts\nimport { i18n } from 'somewhere-in-your-project';\n\nconst main = () =\u003e {\n    console.log( ((`初始化成功，请继续`)) );\n}\n```\n\n```svelte\n\u003c!-- src/sample.svelte --\u003e\n\u003cscript\u003e\n    import { i18n } from 'somewhere-in-your-project';\n\u003c/script\u003e\n\u003cdiv\u003e\n    { ((`欢迎来到 Auto-i18n`)) }\n\u003c/div\u003e\n```\n\n\u003e [!NOTE]  \n\u003e 这里使用 ((\\`xxx\\`)) 作为范例是因为示例为 javascript 语言。  \n\u003e 你可以根据自己的项目语言来配置对应的模式，比如在 python 里面可以配置为:\n\u003e\n\u003e ```yaml\n\u003e i18n_pattern: \\(\\(r\"(.+?)\"\\)\\)\n\u003e ```\n\u003e\n\u003e 然后在代码中写:\n\u003e\n\u003e ```py\n\u003e print( ((r\"简单测试一下\")) )\n\u003e ```\n\u003e\n\u003e 外层使用了两个括号是因为几乎在所有语言里，`()`​ 都是合法的表达式语法；这么写即便后面不使用 i18n 命令进行替换也能正常运行。考虑到几乎很少有实际的项目会连续使用两个 `()`​，这样就可以避免在对源代码进行不当侵入的情况下对 (需要自动翻译的) 特殊部分进行标记。\n\n### 5. 自动提取 i18n 文本\n\n在项目目录下运行:\n\n```bash\ni18n extract\n```\n\n程序会自动扫描所有匹配到的文本，并使用 GPT 来生成合适的 i18n 变量名称，写入的 `main_file`​ （如：zh_CN.yaml 文件）中。\n\n```yaml\nhello: 你好\nsamplesvelte:\n  welcometoautoi18n: 欢迎来到 Auto-i18n\ntestts:\n  initsuccesspleasecontinue: 初始化成功，请继续\n```\n\n同时，原本的 i18n 文本会自动被替换为对应的变量:\n\n```ts\n// src/test.ts\nimport { i18n } from 'somewhere-in-your-project';\n\nconst main = () =\u003e {\n    console.log( i18n.testts.initsuccesspleasecontinue );\n}\n```\n\n```svelte\n\u003c!-- src/sample.svelte --\u003e\n\u003cscript\u003e\n    import { i18n } from 'somewhere-in-your-project';\n\u003c/script\u003e\n\u003cdiv\u003e\n    { i18n.samplesvelte.welcometoautoi18n }\n\u003c/div\u003e\n```\n\n被替换的变量有三个部分组成:\n\n1. ​`i18n_var_prefix`​: 可以在项目配置文件中设置\n2. ​`filename`​: 一个基于当前文件名，只包含字母、数字的字面量\n3. ​`i18n_var_name`​: 由 GPT 生成的变量名称\n\n    \u003e 如果变量名称出现了冲突，程序会自动的变量后面加上数字以避免命名冲突\n    \u003e\n\n### 6. 翻译 i18n 文本\n\n运行以下命令翻译 i18n 文本：\n\n```bash\ni18n translate\n```\n\n程序会：\n\n1. 读取 `main_file`​ 中的文本，如 zh_CN.json 文件\n2. 扫描 `i18n_dir`​ 下所有的其他语言的文件，如同目录的 en_US.json, ja_JP.json 文件\n3. 使用 GPT 翻译内容，并合并到其他语言的文件中\n\n```yaml\n# en_US.yaml\nhello: Hello\nsamplesvelte:\n  welcometoautoi18n: Welcome to Auto-i18n\ntestts:\n  initsuccesspleasecontinue: Initialization succeeded, please continue\n```\n\n\u003e [!NOTE]\n\u003e\n\u003e 默认会使用 `--diff`​ 模式进行翻译，在该模式下程序只翻译增量部分，而不会全部翻译（以节省 token 和时间消耗）。\n\u003e\n\u003e 你可以通过指定 `--full`​ 参数要求程序完整翻译整个 i18n 文件。\n\n### 7. 导出\n\n使用 `export`​ 命令，可以将主 i18n 文件导出为其他格式，目前支持 TypeScript 接口 (.d.ts)。\n\n```bash\ni18n export\n```\n\n该命令会默认将导出的文件写入到项目目录下，你可以在 `auto-i18n.project.yaml`​中配置 `export_dir`​来指定其他目录。\n\n## 配置选项\n\n### 全局配置\n\n存放在 `~/.config/auto-i18n/global-config.yaml`​ 文件中。\n\n```yaml\nGPT:\n  endpoint: \n  key: \n  model: \nprompt:\n  autokey: \n  translate: \nlang: \n\n```\n\n* ​`GPT.endpoint`​: GPT API 的地址\n* ​`GPT.key`​: GPT API 的密钥\n* ​`GPT.model`​: GPT 模型名称\n* ​`prompt.autokey`​: 用于自动生成的 i18n 变量前缀名称的 prompt\n* ​`prompt.translate`​: 用于翻译文本的 prompt\n* ​`lang`​: 使用的语言，可选为 `en_US`​ 和 `zh_CN`​\n\n### 项目级配置\n\n通过 `init`​ 命令创建的 `auto-i18n.project.yaml`​ 文件内容如下：\n\n```yaml\ncode_files:\n- '**/*.ts'\n- '**/*.svelte'\ni18n_dir: temp/i18n\nmain_file: zh_CN.yaml\ndict: {}\ni18n_pattern: \\(\\(`(.+?)`\\)\\)\ni18n_var_prefix: i18n\nstrategy: diff\nexport_dir:\n```\n\n* ​`i18n_dir`​: 存放翻译文件的目录\n* ​`main_file`​: 主要语言的翻译文件\n* ​`code_files`​: 需要扫描的代码文件类型\n* ​`i18n_pattern`​: 在代码中标记需要翻译的文本的模式\n* ​`dict`​: 特殊词汇的翻译对照表；你可以把项目中涉及到的一些属于翻译写在这个地方\n* ​`strategy`​: 翻译策略\n  * `\"diff\"`​ 表示只翻译新增的内容\n  * `\"full\"`​ 表示翻译所有内容\n* ​`i18n_var_prefix`​: 在代码中使用的替换变量的前缀\n* ​`export_dir`​: 导出目录，如果设置，将用作 export 命令的输出目录\n* `i18n_var_mid`: i18n 键的中间部分生成策略。选项包括：\n  * `\"filename\"`: 使用完整文件名，包含扩展名，如 `utilsts`\n  * `\"filename_noext\"`: 使用不带扩展名的文件名\n  * `\"pathname\"`: 使用文件的相对路径\n\n\n### 覆盖全局配置\n\n在项目配置中，可以在 `global_config`​ 字段中覆盖全局配置，例如这样:\n\n```yaml\ncode_files:\n- '**/*.ts'\n- '**/*.svelte'\ni18n_dir: temp/i18n\nmain_file: zh_CN.yaml\ndict: {}\ni18n_pattern: \\(\\(`(.+?)`\\)\\)\ni18n_var_prefix: i18n\nstrategy: diff\nglobal_config:\n  GPT:\n    endpoint: \"https://api.openai.com/v1/chat/completions\"\n    key: \"你的_API_密钥\"\n    model: \"模型名称\"\n```\n\n‍\n\n## 其他说明\n\n### 默认的 prompt.autokey\n\n\u003e ```md\n\u003e ## Task Description\n\u003e\n\u003e - Background: You are developing a project which need to using i18n variables for internationalization.\n\u003e - Task:\n\u003e   1. Read all the [## i18n text]\n\u003e   2. Generate appropriate key names based on the content of each text\n\u003e  3. Summarize the results into a JSON\n\u003e - **Output Format Requirements**:\n\u003e   - Retaining JSON format\n\u003e   - Output the JSON code directly, without attaching the ‍‍‍```json‍‍‍``` code block identifier\n\u003e - **Key Name Requirements**:\n\u003e   - **Only lowercase English letters and numbers are allowed**, no other special symbols (such as spaces, -, underscores, etc.)\n\u003e     - E.g. \"greeting\" and \"invalidinputnumber\" are valid, while \"welcome_here\", \"invalid-input-number\", and \"非英文字符\" are not valid\n\u003e     - **Keep short and concise**, each key name within 15 characters, upmost to 25 characters, it is ok to scacrifice readability for brevity.\n\u003e\n\u003e ## i18n Text\n\u003e\n\u003e ‍‍‍```txt\n\u003e {lines}\n\u003e ‍‍‍```\n\u003e\n\u003e ## An example, for reference only!\n\u003e\n\u003e Input:\n\u003e\n\u003e ‍‍‍```txt\n\u003e Hello {0}\n\u003e Warning! Please do not enter numbers outside 0-10!\n\u003e ‍‍‍```\n\u003e\n\u003e Output\n\u003e\n\u003e {\n\u003e \"greeting\": \"Hello {0}\",\n\u003e \"invalidinputnumber\": \"Warning! Please do not enter numbers outside 0-10!\"\n\u003e }\n\u003e ```\n\n本 prompt 在运行时将会替换如下变量：\n\n* ​`{lines}`​：替换为在源代码文件中找到的所有 i18n 文本\n\n  * 例如：如果源代码为:\n\n    ```ts\n    console.log(((`你好啊`)))\n    ele.innerText = ((`警告!`))\n    ```\n  * 则 `{lines}`​ 会被替换为\n\n    ```ts\n    你好啊\n    警告!\n    ```\n\n### 默认的 prompt.translate\n\n\u003e ```md\n\u003e ## Task Description\n\u003e\n\u003e - Task: Translate the content of the i18n file {InFile} (see [## i18n Content]) to another language (file {OutFile}).\n\u003e - Requirements:\n\u003e   - Target language file: {OutFile}\n\u003e   - Output format: JSON code, please retaining JSON format\n\u003e   - Output the translated JSON code directly, without attaching the ‍‍‍```json‍‍‍``` code block identifier\n\u003e\n\u003e ## Vocabulary\n\u003e\n\u003e {Dict}\n\u003e\n\u003e ## i18n Content\n\u003e\n\u003e ‍‍‍```json\n\u003e {I18n}\n\u003e ‍‍‍```\n\u003e ```\n\n本 prompt 在运行时将会替换如下变量：\n\n* ​`{InFile}`​：你的主 i18n 文件的文件名，例如 `zh_CN.json`​\n* ​`{OutFile}`​：将要翻译的目标 i18n 文件，例如 `ja_JP.json`​\n* ​`{Dict}`​: Project 配置中的 `dict`​ 字段\n* ​`{I18n}`​: 需要被翻译的 i18n 对应的 json 字符串\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostime%2Fpy-auto-i18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrostime%2Fpy-auto-i18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostime%2Fpy-auto-i18n/lists"}