Thanks to visit codestin.com
Credit goes to github.com

Skip to content

soggie/koa-i18n

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-i18n Build Status

I18n fro koa.
Based on i18n-2.
NOTE: If want to use koa-i18n, koa-locale muste be requred!
NOTE: If want to work together with templates render, koa-locals must be required!

Usage

Install

npm install koa-i18n

Example

var app = require('koa')();
var locale = require('koa-locale'); //  detect the locale
var locals = require('koa-locals'); //  local variables for templates render
var render = require('koa-swig');   //  swig render
var i18n = require('koa-i18n');

// Required! 
locale(app);

// Working together with template render must require!
locals(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
  query: true,                    //  optional detect querystring - `/?lang=en-US`
  subdomain: true,                //  optional detect subdomain   - `zh-CN.koajs.com`
  cookie: true,                   //  optional detect cookie      - `Accept-Language: zh-CN,zh;q=0.5`
  header: true                    //  optional detect header      - `Cookie: lang=zh-TW`
}));

app.use(function *(next) {
  this.body = this.i18n.__('any key');
});

app.use(function *(next) {
  yield this.render('index')
});

Dependencies

  • i18n-2
  • koa-locale - Get locale variable from query, subdomain, accept-languages or cookie

License

MIT

About

Lightweight simple translation middleware for koa, based on i18n-2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%