koa-i18n
I18n fro koa, based on i18n-2.
NOTE: If want to use koa-i18n, koa-locale must be required!
koa-i18n version | branch | koa version |
---|
1.x latest | v1.x | 1.x latest |
2.x next | master | 2.x next |
Installation
$ npm install koa-i18n
Usage
const Koa = require('koa')
const convert = require('koa-convert')
const locale = require('koa-locale')
const render = require('koa-swig')
const i18n = require('koa-i18n')
const app = new Koa()
locale(app)
app.context.render = render({
root: __dirname + '/views/',
ext: 'html'
})
app.use(i18n(app, {
directory: './config/locales',
locales: ['zh-CN', 'en'],
modes: [
'query',
'subdomain',
'cookie',
'header',
'url',
'tld',
function() {}
]
}))
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