Skip to content

bpcloud/middleware-koa-i18n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa i18n middleware in bpframework.

Middleware specification

https://github.com/bpcloud/middleware

usage

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'});

  ...
}

About

koa i18n middleware in bpframework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published