{"id":13779660,"url":"https://github.com/koajs/locales","last_synced_at":"2025-05-16T07:07:51.158Z","repository":{"id":32152891,"uuid":"35725924","full_name":"koajs/locales","owner":"koajs","description":"koa locales, i18n solution for koa","archived":false,"fork":false,"pushed_at":"2025-01-11T12:34:58.000Z","size":85,"stargazers_count":69,"open_issues_count":11,"forks_count":30,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-29T05:28:25.418Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"History.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":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-05-16T14:04:08.000Z","updated_at":"2025-01-11T00:22:56.000Z","dependencies_parsed_at":"2023-01-14T20:38:09.294Z","dependency_job_id":"8d53c1d9-78f3-4807-bfc7-1e4144cc31b5","html_url":"https://github.com/koajs/locales","commit_stats":{"total_commits":55,"total_committers":15,"mean_commits":"3.6666666666666665","dds":0.7272727272727273,"last_synced_commit":"b22bfc52a2dbe0ca22270f1b49f63aaa1dfb7520"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Flocales","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Flocales/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Flocales/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Flocales/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/locales/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252782587,"owners_count":21803404,"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":"2024-08-03T18:01:07.715Z","updated_at":"2025-05-16T07:07:46.149Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"koa-locales\n=======\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][cov-image]][cov-url]\n[![David deps][david-image]][david-url]\n[![npm download][download-image]][download-url]\n\nkoa locales, i18n solution for koa:\n\n1. All locales resources location on `options.dirs`.\n2. resources file supports: `*.js`, `*.json`, `*.yml`, `*.yaml` and `*.properties`, see [examples](test/locales/).\n3. One api: `__(key[, value, ...])`.\n4. Auto detect request locale from `query`, `cookie` and `header: Accept-Language`.\n\n## Installation\n\n```bash\n$ npm install koa-locales --save\n```\n\n## Quick start\n\n```js\nconst koa = require('koa');\nconst locales = require('koa-locales');\n\nconst app = koa();\nconst options = {\n  dirs: [__dirname + '/locales', __dirname + '/foo/locales'],\n};\nlocales(app, options);\n```\n\n## API Reference\n\n### `locales(app, options)`\n\nPatch locales functions to koa app.\n\n- {Application} app: koa app instance.\n- {Object} options: optional params.\n  - {String} functionName: locale function name patch on koa context. Optional, default is `__`.\n  - {String} dirs: locales resources store directories. Optional, default is `['$PWD/locales']`.\n  - {String} defaultLocale: default locale. Optional, default is `en-US`.\n  - {String} queryField: locale field name on query. Optional, default is `locale`.\n  - {String} cookieField: locale field name on cookie. Optional, default is `locale`.\n  - {String} cookieDomain: domain on cookie. Optional, default is `''`.\n  - {Object} localeAlias: locale value map. Optional, default is `{}`.\n  - {Boolean} writeCookie: set cookie if header not sent. Optional, default is `true`.\n  - {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is `1y`, expired after one year.\n\n```js\nlocales({\n  app: app,\n  dirs: [__dirname + '/app/locales'],\n  defaultLocale: 'zh-CN',\n});\n```\n\n#### Aliases\n\nThe key `options.localeAlias` allows to not repeat dictionary files, as you can configure to use the same file for *es_ES* for *es*, or *en_UK* for *en*.\n\n```js\nlocales({\n  localeAlias: {\n    es: es_ES,\n    en: en_UK,\n  },\n});\n```\n\n### `context.__(key[, value1[, value2, ...]])`\n\nGet current request locale text.\n\n```js\nasync function home(ctx) {\n  ctx.body = {\n    message: ctx.__('Hello, %s', 'fengmk2'),\n  };\n}\n```\n\nExamples:\n\n```js\n__('Hello, %s. %s', 'fengmk2', 'koa rock!')\n=\u003e\n'Hello fengmk2. koa rock!'\n\n__('{0} {0} {1} {1} {1}', ['foo', 'bar'])\n=\u003e\n'foo foo bar bar bar'\n\n__('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})\n=\u003e\n'foo foo bar bar bar'\n```\n\n### `context.__getLocale()`\n\nGet locale from query / cookie and header.\n\n### `context.__setLocale()`\n\nSet locale and cookie.\n\n### `context.__getLocaleOrigin()`\n\nWhere does locale come from, could be `query`, `cookie`, `header` and `default`.\n\n### `app.__(locale, key[, value1[, value2, ...]])`\n\nGet the given locale text on application level.\n\n```js\nconsole.log(app.__('zh', 'Hello'));\n// stdout '你好' for Chinese\n```\n\n## Usage on template\n\n```js\nthis.state.__ = this.__.bind(this);\n```\n\n[Nunjucks] example:\n\n```html\n{{ __('Hello, %s', user.name) }}\n```\n\n[Pug] example:\n\n```pug\np= __('Hello, %s', user.name)\n```\n\n[Koa-pug] integration:\n\nYou can set the property *locals* on the KoaPug instance, where the default locals are stored.\n\n```js\napp.use(async (ctx, next) =\u003e {\n  koaPug.locals.__ = ctx.__.bind(ctx);\n  await next();\n});\n```\n\n## Debugging\n\nIf you are interested on knowing what locale was chosen and why you can enable the debug messages from [debug].\n\nThere is two level of verbosity:\n\n```sh\n$ DEBUG=koa-locales node .\n```\nWith this line it only will show one line per request, with the chosen language and the origin where the locale come from (queryString, header or cookie).\n\n```sh\n$ DEBUG=koa-locales:silly node .\n```\nUse this level if something doesn't work as you expect. This is going to debug everything, including each translated line of text.\n\n## License\n\n[MIT](LICENSE)\n\n\n[nunjucks]: https://www.npmjs.com/package/nunjucks\n[debug]: https://www.npmjs.com/package/debug\n[pug]: https://www.npmjs.com/package/pug\n[koa-pug]: https://www.npmjs.com/package/koa-pug\n\n[npm-image]: https://img.shields.io/npm/v/koa-locales.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-locales\n[travis-image]: https://img.shields.io/travis/koajs/locales.svg?style=flat-square\n[travis-url]: https://travis-ci.org/koajs/locales\n[cov-image]: https://codecov.io/github/koajs/locales/coverage.svg?branch=master\n[cov-url]: https://codecov.io/github/koajs/locales?branch=master\n[david-image]: https://img.shields.io/david/koajs/locales.svg?style=flat-square\n[david-url]: https://david-dm.org/koajs/locales\n[download-image]: https://img.shields.io/npm/dm/koa-locales.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa-locales\n","funding_links":[],"categories":["Middleware","仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Flocales","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Flocales","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Flocales/lists"}