https://github.com/bpcloud/middleware
Setup.
import { Application } from 'bpframework';
import * as middleware_i18n from '@bpframework/middleware-koa-i18n';
let cfg = {
dirs: ['resource/locales'],
defaultLocale: 'zh-CN',
queryField: 'locale', // querystring - `/?locale=en-US`
cookieField: 'locale',
localeAlias: {
zh: 'zh-CN',
},
};
Application.use(middleware_i18n.middleware(cfg))
Application.runKoa(...);
Use in anywhere.
__i18n('hello {name}', {name:'world'}) // return 'hello world' by default locale
__i18n('hello %s %s', 'name', 'world') // return 'hello name world' by default locale
__i18nLang('en', 'hello {name}', {name:'world'}) // return 'hello world' by 'en' locale
Use in controller.
@RequestMapping({
path: '/test',
method: RequestMethod.GET,
})
async test(
@RestObject restObj: RestObjectTypeRest<koa.Context> // or RestObjectType
): Promise<any> {
// return 'hello world' by locale
restObj.ctx.__i18n('hello {name}', {name:'world'});
...
}