Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-i18n

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-i18n

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

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
decreased by-5.77%
Maintainers
1
Weekly downloads
 
Created
Source

koa-i18n

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

NPM version Build status Test coverage License Dependency status

koa-i18n versionbranchkoa version
1.x latestv1.x1.x latest
2.x nextmaster2.x next

Installation

$ npm install koa-i18n

Usage

const Koa = require('koa')
const convert = require('koa-convert')
const locale = require('koa-locale') //  detect the locale
const render = require('koa-swig')   //  swig render
const i18n = require('koa-i18n')

const app = new Koa()

// 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` defualtLocale, 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 (ctx) {
  ctx.body = ctx.i18n.__('any key');
})

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

Tip: We can change position of the elements in the modes array. If one mode is detected, no continue to detect.

Dependencies

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

License

MIT

Keywords

FAQs

Package last updated on 12 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc