I18n fro koa, based on i18n-2. NOTE: If want to use koa-i18n, koa-locale must be requred!
$ npm install koa-i18n
var app = require('koa')();
var locale = require('koa-locale'); // detect the locale
var render = require('koa-swig'); // swig render
var i18n = require('koa-i18n');
// Required!
locale(app);
render(app, {
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`
]
}));
app.use(function *(next) {
this.body = this.i18n.__('any key');
});
app.use(function *(next) {
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.
- i18n-2
- koa-locale - Get locale variable from query, subdomain, accept-languages or cookie
MIT