{"id":28729781,"url":"https://github.com/didi/di18n","last_synced_at":"2025-06-15T17:11:02.407Z","repository":{"id":37474277,"uuid":"254264808","full_name":"didi/di18n","owner":"didi","description":"一种自动转换、基于配置的前端国际化方案","archived":false,"fork":false,"pushed_at":"2023-03-10T02:56:39.000Z","size":2975,"stargazers_count":289,"open_issues_count":18,"forks_count":45,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-24T05:06:10.042Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/didi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-04-09T03:46:33.000Z","updated_at":"2024-04-23T05:00:49.000Z","dependencies_parsed_at":"2024-01-29T10:10:06.240Z","dependency_job_id":"8c6c8622-8459-4e91-827e-6300dba5cf63","html_url":"https://github.com/didi/di18n","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/didi/di18n","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fdi18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fdi18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fdi18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fdi18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/didi","download_url":"https://codeload.github.com/didi/di18n/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fdi18n/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260016055,"owners_count":22946321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-06-15T17:11:01.460Z","updated_at":"2025-06-15T17:11:02.395Z","avatar_url":"https://github.com/didi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# di18n\n\n自动转换、基于配置的前端国际化方案。\n\n## 为什么存在\n\n在前端国际化开发中，很多社区方案给我们带来了极大便利，但是仍有2个问题没有解决：\n\n- 需要手工将代码中的主语言（比如中文）替换成国际化标记；\n- 语言包应该是一种配置，不必强耦合到项目代码里。\n\n为此，我们开发了 **di18n**，它能自动扫描代码中的主语言，将其替换成国际化标记；同时将语言抽取成配置，可以放到服务端保存及更新。\n\n## 工作原理\n\ndi18n 会先按如下步骤扫描源码：\n\n- 使用 Babel 解析得到 AST，遍历 AST，并对特殊的节点进行检查，抽取出需要翻译的字符串；\n- ~~自动为每一个字符串分配一个 key~~ 因随机 key 可读性差，已改成使用主语言（如中文）文案为 key；\n- 自动调用 Google 翻译服务（可选），得到一个英文的字符串。\n\n\u003e 注：对于 React，上面提到的特殊节点包括： `JSXText` `StringLiteral` `TemplateLiteral` 等。\n\n扫描之后，对于源代码：\n\n- 构造 `CallExpression` 表达式 `intl.t('key1')`；\n- 替换原有节点 `path.replaceWith(newNode)`；\n- 将新的 AST 通过 Babel 转换为代码；\n- 使用 Prettier 格式化代码；\n- 将新的代码落盘。\n\n对于国际化资源：\n\n- 将 key-value 转换为 i18n 配置文件格式。\n\n## 流程图\n\n![](di18n_work_flow.jpg)\n\n## 安装\n\n```\n$ npm i -D di18n-cli\n\n# or\n\n$ yarn add  -D di18n-cli\n```\n\n## 初始化\n\n**React**\n\n```\n$ npx di18n init\n```\n\n注意：`TypeScript` 需要将配置文件 di18n.config.js 的 `prettier.parser` 改为 `\"typescript\"`。\n\n**Vue**\n\n```\n$ npx di18n init --vue\n```\n\n\n## 转换\n\n```\n$ npx di18n sync\n```\n\n## License\n\n[MIT](https://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Fdi18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdidi%2Fdi18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Fdi18n/lists"}