koa-i18n-2

1.3.0 • Public • Published

koa-i18n-2

I18n for koa, based on [i18n-2]. NOTE: If want to use koa-i18n-2, [koa-locale] must be required!

NPM version Build status License Dependency status

Differences with koa-i18n

koa-i18n-2 will attempt to find the best match from the supported locales, whereas koa-i18n will only find exact matches. For example, if a user requests es-MX and you support ['en-US', 'es-ES'], koa-i18n will use en-US, while koa-i18n-2 will use es-ES.

Installation

$ npm install koa-i18n-2

Usage

var app = require('koa')();
var locale = require('koa-locale'); //  detect the locale
var render = require('koa-swig');   //  swig render
var i18n = require('koa-i18n-2');
 
// Required!
locale(app);
 
app.context.render = render({
  root: __dirname + '/views/',
  ext: 'html'
});
 
app.use(i18n(app, {
  directory: './config/locales',
  locales: ['zh-CN', 'en'], //  `zh-CN` defaultLocale, 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`
    function() {}           //  optional custom function (will be bound to the koa context)
  ]
}));
 
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 finds a match, the modes after it will be ignored.

License

MIT

Package Sidebar

Install

npm i koa-i18n-2

Weekly Downloads

8

Version

1.3.0

License

MIT

Last publish

Collaborators

  • strawbrary