{"id":13487465,"url":"https://github.com/eggjs/i18n","last_synced_at":"2025-10-14T14:17:31.229Z","repository":{"id":57169359,"uuid":"64043872","full_name":"eggjs/i18n","owner":"eggjs","description":"i18n plugin for egg","archived":false,"fork":false,"pushed_at":"2025-01-12T02:04:19.000Z","size":52,"stargazers_count":41,"open_issues_count":0,"forks_count":9,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-10-05T14:02:01.616Z","etag":null,"topics":["egg-plugin","i18n"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/eggjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2016-07-24T01:51:29.000Z","updated_at":"2025-05-15T15:23:17.000Z","dependencies_parsed_at":"2025-01-11T16:26:55.709Z","dependency_job_id":"d6c07e69-850c-4a7f-be7a-d9c8ef9f813f","html_url":"https://github.com/eggjs/i18n","commit_stats":null,"previous_names":["eggjs/i18n"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/eggjs/i18n","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fi18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fi18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fi18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fi18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eggjs","download_url":"https://codeload.github.com/eggjs/i18n/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fi18n/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279019101,"owners_count":26086576,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["egg-plugin","i18n"],"created_at":"2024-07-31T18:00:59.716Z","updated_at":"2025-10-14T14:17:31.213Z","avatar_url":"https://github.com/eggjs.png","language":"TypeScript","readme":"# @eggjs/i18n\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/eggjs/i18n/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/i18n/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/@eggjs/i18n.svg?style=flat)](https://nodejs.org/en/download/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)\n\n[npm-image]: https://img.shields.io/npm/v/@eggjs/i18n.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@eggjs/i18n\n[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/i18n.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/eggjs/i18n?branch=master\n[snyk-image]: https://snyk.io/test/npm/@eggjs/i18n/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/@eggjs/i18n\n[download-image]: https://img.shields.io/npm/dm/@eggjs/i18n.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@eggjs/i18n\n\n可以为你的应用提供多语言的特性\n\n## 功能\n\n- 支持多种语言独立配置，统一存放在 config/locale/\\*.js 下（ 兼容 `config/locales/*.js` ）；\n- 提供 Middleware 为 View 提供 `\\_\\_`, `gettext` 函数获取多语言文案；\n- 基于 URL 参数 `locale` 修改语言显示，同时会记录到 Cookie，下次请求会用 Cookie 里面的语言方案。\n\n## 配置\n\n默认处于关闭状态，你需要在 `config/plugin.ts` 开启它：\n\n```ts\n// config/plugin.ts\nexport default {\n  i18n: {\n    enable: true,\n    package: '@eggjs/i18n',\n  },\n};\n```\n\n你可以修改 `config/config.default.ts` 来设定 i18n 的配置项：\n\n```ts\n// config/config.default.ts\nexport default {\n  i18n: {\n    // 默认语言，默认 \"en_US\"\n    defaultLocale: 'zh-CN',\n    // URL 参数，默认 \"locale\"\n    queryField: 'locale',\n    // Cookie 记录的 key, 默认：\"locale\"\n    cookieField: 'locale',\n    // Cookie 的 domain 配置，默认为空，代表当前域名有效\n    cookieDomain: '',\n    // Cookie 默认 `1y` 一年后过期， 如果设置为 Number，则单位为 ms\n    cookieMaxAge: '1y',\n  },\n};\n```\n\n其实大部分时候，你只需要修改一下 `defaultLocale` 设定默认的语言。\n\n## 编写你的 I18n 多语言文件\n\n```ts\n// config/locale/zh-CN.ts\nexport default {\n  \"Email\": \"邮箱\",\n  \"Welcome back, %s!\": \"欢迎回来，%s!\",\n  \"Hello %s, how are you today?\": \"你好 %s, 今天过得咋样？\",\n};\n```\n\n```ts\n// config/locale/en-US.ts\nexport default {\n  \"Email\": \"Email\",\n};\n```\n\n或者也可以用 JSON 格式的文件：\n\n```json\n// config/locale/zh-CN.json\n{\n  \"email\": \"邮箱\",\n  \"login\": \"帐号\",\n  \"createdAt\": \"注册时间\"\n}\n```\n\n## 使用 I18n 函数获取语言文本\n\nI18n 为你提供 `__` (Alias: `gettext`) 函数，让你可以轻松获得 locale 文件夹下面的多语言文本。\n\n\u003e NOTE: __ 是两个下划线哦！\n\n- `ctx.__ = function (key, value[, value2, ...])`: 类似 util.format 接口\n- `ctx.__ = function (key, values)`: 支持数组下标占位符方式，如\n\n```ts\nctx.__('{0} {0} {1} {1}'), ['foo', 'bar']);\nctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar']);\n\n=\u003e\nfoo foo bar bar\n```\n\n### Controllers 下的使用示例\n\n```ts\nexport default ctx =\u003e {\n  ctx.body = {\n    message: ctx.__('Welcome back, %s!', ctx.user.name)\n    // 或者使用 gettext，如果觉得 __ 不好看的话\n    // message: this.gettext('Welcome back, %s!', ctx.user.name)\n    user: ctx.user,\n  };\n};\n```\n\n### View 文件下的使用示例\n\n```html\n\u003cli\u003e{{ __('Email') }}: {{ user.email }}\u003c/li\u003e\n\u003cli\u003e\n  {{ __('Hello %s, how are you today?', user.name) }}\n\u003c/li\u003e\n\u003cli\u003e\n  {{ __('{0} {0} {1} {1}'), ['foo', 'bar']) }}\n\u003c/li\u003e\n```\n\n### 修改应用的默认语言\n\n你可以用下面几种方式修改应用的当前语言（修改或会记录到 Cookie），下次请求直接用设定好的语言。\n\n优先级从上到下：\n\n- query: `/?locale=en-US`\n- cookie: `locale=zh-TW`\n- header: `Accept-Language: zh-CN,zh;q=0.5`\n\n## Questions \u0026 Suggestions\n\nPlease open an issue [here](https://github.com/eggjs/egg/issues).\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=eggjs/i18n)](https://github.com/eggjs/i18n/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","funding_links":[],"categories":["仓库"],"sub_categories":["[内置插件](https://eggjs.org/zh-cn/basics/plugin.html#%E6%8F%92%E4%BB%B6%E5%88%97%E8%A1%A8)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fi18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feggjs%2Fi18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fi18n/lists"}