# koa-i18n

> Lightweight simple translation middleware for koa, based on i18n-2

Latest version **2.1.0** (published 2017-04-12) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2017-04-12 |
| First published | 2014-04-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 65 |
| Author | Fangdun Cai |
| Maintainers | fundon |
| Keywords | koa, locale, i18n, languages |

## Links

- npm: https://www.npmjs.com/package/koa-i18n
- Repository: https://github.com/koa-modules/koa-i18n
- Homepage: https://github.com/koa-modules/koa-i18n#readme
- Issues: https://github.com/koa-modules/koa-i18n/issues
- npm.io page: https://npm.io/package/koa-i18n

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) *
- [i18n-2](https://npm.io/package/i18n-2.md) *

## 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

- 2.1.0 (latest) — 2017-04-12
- 2.0.1 (next) — 2016-01-07
- 2.0.0 — 2016-01-07
- 1.2.0 — 2015-11-20
- 1.1.2 — 2015-10-20
- 1.1.1 — 2015-10-20
- 1.1.0 — 2015-03-08
- 1.0.0 — 2015-03-08
- 0.4.0 — 2015-01-12
- 0.3.2 — 2014-12-18
- 0.3.1 — 2014-12-13
- 0.3.0 — 2014-12-13
- 0.2.0 — 2014-11-14
- 0.1.4 — 2014-04-23
- 0.1.3 — 2014-04-23
- … 6 more at https://npm.io/package/koa-i18n/versions

## README

# koa-i18n

> I18n fro koa, based on [i18n-2].
> **NOTE**: If want to use koa-i18n, [koa-locale] must be required!

[![NPM version][npm-img]][npm-url]
[![Build status][travis-img]][travis-url]
[![Test coverage][coveralls-img]][coveralls-url]
[![License][license-img]][license-url]
[![Dependency status][david-img]][david-url]

koa-i18n version | branch | koa version
---------------- | ------ | -----------
1.x **latest**   | v1.x   | 1.x **latest**
2.x **next**     | master | 2.x **next**

### Installation

```bash
$ npm install koa-i18n
```

### Usage

```js
const Koa = require('koa')
const convert = require('koa-convert')
const locale = require('koa-locale') //  detect the locale
const render = require('koa-swig')   //  swig render
const i18n = require('koa-i18n')

const app = new Koa()

// Required!
locale(app)

app.context.render = render({
  root: __dirname + '/views/',
  ext: 'html'
})

app.use(i18n(app, {
  directory: './config/locales',
  locales: ['zh-CN', 'en'], //  `zh-CN` defualtLocale, must match the locales to the filenames
  modes: [
    'query',                //  optional detect querystring - `/?locale=en-US`
    'subdomain',            //  optional detect subdomain   - `zh-CN.koajs.com`
    'cookie',               //  optional detect cookie      - `Cookie: locale=zh-TW`
    'header',               //  optional detect header      - `Accept-Language: zh-CN,zh;q=0.5`
    'url',                  //  optional detect url         - `/en`
    'tld',                  //  optional detect tld(the last domain) - `koajs.cn`
    function() {}           //  optional custom function (will be bound to the koa context)
  ]
}))

app.use(function (ctx) {
  ctx.body = ctx.i18n.__('any key');
})

app.use(convert(function *() {
  yield this.render('index')
}))
```

> **Tip**: We can change position of the elements in the `modes` array.
> If one mode is detected, no continue to detect.


### Dependencies

* [i18n-2][]
* [koa-locale][] - Get locale variable from query, subdomain, accept-languages or cookie


### License

  MIT

[i18n-2]: https://github.com/jeresig/i18n-node-2
[koa-locale]: https://github.com/koa-modules/koa-locale

[npm-img]: https://img.shields.io/npm/v/koa-i18n.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-i18n
[travis-img]: https://img.shields.io/travis/koa-modules/i18n.svg?style=flat-square
[travis-url]: https://travis-ci.org/koa-modules/i18n
[coveralls-img]: https://img.shields.io/coveralls/koa-modules/i18n.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koa-modules/i18n?branch=master
[license-img]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
[license-url]: LICENSE
[david-img]: https://img.shields.io/david/koa-modules/i18n.svg?style=flat-square
[david-url]: https://david-dm.org/koa-modules/i18n

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