sg-koa-i18n
i18n middleware for koa@2.x, use with sg-i18n, you need to use together with koa-locale.
Installation
npm i sg-koa-i18n -S
Usage
var app = require('koa')();
var locale = require('koa-locale');
var i18n = require('sg-koa-i18n');
locale(app, 'language');
app.use(i18n(app, {
directory: __dirname + '/fixtures/locales',
locales: ['zh-CN', 'en', 'zh-TW'],
defaultLocale: 'zh-CN',
mappings: {
'zh-HK': 'zh-TW'
},
modes: [
'header',
'url',
(ctx) => {
return ctx.headers['locale'];
}
]
}));
app.use(async (ctx, next) => {
let i18n = ctx.i18n;
let msg = i18n.__('msg');
let msg2 = ctx.__('msg2');
console.log(ctx.state.locale)
ctx.body = ctx.i18n.__('locales.en')
});
ctx.state.locale
ctx.state.locale = {
locale: locale,
detected: localeDetected,
map: whiteMap,
use: use,
}
options
{
locales: ['zh-CN'],
defaultLocale: 'zh-CN',
directory: '',
extension: '.js',
modes: ['url'],
mappings: {},
rewrite: true,
}
Locale lookup priority
- modes
- locales
- mappings
- defaultLocale
translate