# koa-locales

> koa locales, i18n solution for koa

Latest version **1.12.0** (published 2019-06-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-locales
pnpm add koa-locales
yarn add koa-locales
bun add koa-locales
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.12.0 |
| Published | 2019-06-16 |
| First published | 2015-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 4 |
| Unpacked size | 17.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 69 |
| Author | fengmk2 |
| Maintainers | dead_horse, fengmk2, popomore |
| Keywords | koa-locales, i18n, locales, koa-i18n, koa |

## Links

- npm: https://www.npmjs.com/package/koa-locales
- Repository: https://github.com/koajs/locales
- Issues: https://github.com/koajs/locales/issues
- npm.io page: https://npm.io/package/koa-locales

## Dependencies (4)

- [ini](https://npm.io/package/ini.md) ^1.3.4
- [debug](https://npm.io/package/debug.md) ^2.6.0
- [humanize-ms](https://npm.io/package/humanize-ms.md) ^1.2.0
- [object-assign](https://npm.io/package/object-assign.md) ^4.1.0

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 1.12.0 (latest) — 2019-06-16
- 1.11.0 — 2019-04-30
- 1.10.0 — 2019-04-29
- 1.9.0 — 2019-04-17
- 1.8.0 — 2018-01-12
- 1.7.0 — 2017-04-27
- 1.6.0 — 2017-04-27
- 1.5.2 — 2017-01-13
- 1.5.1 — 2016-05-20
- 1.5.0 — 2016-03-16
- 1.4.4 — 2015-12-23
- 1.4.3 — 2015-12-09
- 1.4.2 — 2015-09-20
- 1.4.0 — 2015-09-17
- 1.3.1 — 2015-09-14
- … 6 more at https://npm.io/package/koa-locales/versions

## README

koa-locales
=======

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][cov-image]][cov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]

koa locales, i18n solution for koa:

1. All locales resources location on `options.dirs`.
2. resources file supports: `*.js`, `*.json` and `*.properties`, see [examples](test/locales/).
3. One api: `__(key[, value, ...])`.
4. Auto detect request locale from `query`, `cookie` and `header: Accept-Language`.

## Installation

```bash
$ npm install koa-locales --save
```

## Quick start

```js
const koa = require('koa');
const locales = require('koa-locales');

const app = koa();
const options = {
  dirs: [__dirname + '/locales', __dirname + '/foo/locales'],
};
locales(app, options);
```

## API Reference

### `locales(app, options)`

Patch locales functions to koa app.

- {Application} app: koa app instance.
- {Object} options: optional params.
  - {String} functionName: locale function name patch on koa context. Optional, default is `__`.
  - {String} dirs: locales resources store directories. Optional, default is `['$PWD/locales']`.
  - {String} defaultLocale: default locale. Optional, default is `en-US`.
  - {String} queryField: locale field name on query. Optional, default is `locale`.
  - {String} cookieField: locale field name on cookie. Optional, default is `locale`.
  - {String} cookieDomain: domain on cookie. Optional, default is `''`.
  - {Object} localeAlias: locale value map. Optional, default is `{}`.
  - {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is `1y`, expired after one year.

```js
locales({
  app: app,
  dirs: [__dirname + '/app/locales'],
  defaultLocale: 'zh-CN',
});
```

#### Aliases

The 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*.

```js
locales({
  localeAlias: {
    es: es_ES,
    en: en_UK,
  },
});
```

### `context.__(key[, value1[, value2, ...]])`

Get current request locale text.

```js
async function home(ctx) {
  ctx.body = {
    message: ctx.__('Hello, %s', 'fengmk2'),
  };
}
```

Examples:

```js
__('Hello, %s. %s', 'fengmk2', 'koa rock!')
=>
'Hello fengmk2. koa rock!'

__('{0} {0} {1} {1} {1}', ['foo', 'bar'])
=>
'foo foo bar bar bar'

__('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
=>
'foo foo bar bar bar'
```

### `context.__getLocale()`

Get locale from query / cookie and header.

### `context.setLocale()`

Set locale and cookie.

### `context.__getLocaleOrigin()`

Where does locale come from, could be `query`, `cookie`, `header` and `default`.

### `app.__(locale, key[, value1[, value2, ...]])`

Get the given locale text on application level.

```js
console.log(app.__('zh', 'Hello'));
// stdout '你好' for Chinese
```

## Usage on template

```js
this.state.__ = this.__.bind(this);
```

[Nunjucks] example:

```html
{{ __('Hello, %s', user.name) }}
```

[Pug] example:

```pug
p= __('Hello, %s', user.name)
```

[Koa-pug] integration:

You can set the property *locals* on the KoaPug instance, where the default locals are stored.

```js
app.use(async (ctx, next) => {
  koaPug.locals.__ = ctx.__.bind(ctx);
  await next();
});
```

## Debugging

If you are interested on knowing what locale was chosen and why you can enable the debug messages from [debug].

There is two level of verbosity:

```sh
$ DEBUG=koa-locales node .
```
With 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).

```sh
$ DEBUG=koa-locales:silly node .
```
Use this level if something doesn't work as you expect. This is going to debug everything, including each translated line of text.

## License

[MIT](LICENSE)


[nunjucks]: https://www.npmjs.com/package/nunjucks
[debug]: https://www.npmjs.com/package/debug
[pug]: https://www.npmjs.com/package/pug
[koa-pug]: https://www.npmjs.com/package/koa-pug

[npm-image]: https://img.shields.io/npm/v/koa-locales.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-locales
[travis-image]: https://img.shields.io/travis/koajs/locales.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/locales
[cov-image]: https://codecov.io/github/koajs/locales/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/koajs/locales?branch=master
[david-image]: https://img.shields.io/david/koajs/locales.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/locales
[download-image]: https://img.shields.io/npm/dm/koa-locales.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-locales

---
_Source: https://npm.io/package/koa-locales · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
