Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including koa-locale with all npm packages installed. Try it out:

var koaLocale = require("koa-locale")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

koa-locale v1.3.0

Get locale variable from query, subdomain, the last domain, accept-languages or cookie for koa.

koa-locale

Get locale variable from query, subdomain, the last domain, accept-languages or cookie for koa.

NPM version Build status Test coverage License Dependency status

Installation

$ npm install koa-locale

Usage

var app = require('koa')();
var locale = require('koa-locale');

// the locale key name defaults to `locale`
locale(app, 'language');

app.use(function *(next) {
  // query: '?language=en'
  this.body = this.getLocaleFromQuery();
});

API

ctx.getLocaleFromQuery(), ctx.request.getLocaleFromQuery()

/?locale=en-US

ctx.getLocaleFromSubdomain(), ctx.request.getLocaleFromSubdomain()

zh-CN.koajs.com

ctx.getLocaleFromHeader(multi = false), ctx.request.getLocaleFromHeader(multi = false)

Accept-Language: zh-CN,zh;q=0.5

ctx.getLocaleFromCookie(), ctx.request.getLocaleFromCookie()

Cookie: locale=zh-TW

ctx.getLocaleFromUrl(options), ctx.request.getLocaleFromUrl(options)

http://koajs.com/en
options = { offset: 2 }

http://koajs.com/foo/bar/en

ctx.getLocaleFromTLD(), ctx.request.getLocaleFromTLD()

http://koajs.com/
http://koajs.cn/
http://koajs.it/

License

MIT

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free