npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-adam-locale

v2.4.0

Published

Simple Multi Language Middleware for Koa.

Downloads

20

Readme

koa-adam-locale

Simple Multi Language Middleware for Koa.

Installation

$ npm install koa-adam-locale

Example

app.js:

var Path = require('path');
var View = require('koa-views')
var KAL = require('koa-adam-locale');
var koa = require('koa');
var app = koa();

/* i18n */
app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  // reference: require('./i18n_ref'),
  cookieKey: 'current_lang',
  cookies: {
    maxAge: 365*24*60*60*1000,
    domain: 'hello.com',
    signed: false
  },
  default: 'en_US',
  supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]
}, app));

/* templating */
app.use(View(__dirname + '/views', {
  default: 'ejs'
}));

app.use(function * () {
  if(!this.path == '/') return;
  yield this.render('locale')
})

app.listen(process.argv[2])

i18n/en_US.json

{
  "hello": "hello",
  "world": "world"
}

views/locale.ejs

<p><%= _i18n_.hello %>, <%= _i18n_.world %>.</p>
<p>current:<%= _i18n_current_ %>.</p>
<% for(var i in _i18n_supported_) { %>
<a href='/set_locale?lang=<%= _i18n_supported_[i].code %>'>
	switch to <%= _i18n_supported_[i].lang %>
</a>
<% } %>

Options

path {String}

The folder path of i18n files

reference {Object}

The reference of i18n Object, key of this reference should match the code, like 'zh_CN', 'en_US' (Case sensitive), and this parameter has a higher priority than path.

supported {Array}

The supported languages. default value: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]

Example:

app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  // reference: require('./i18n_ref'),
  supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]
}, app));

namespace {String}

Custom template data params namespace. Default value is 'locals'

  • koa-views 2.x - ctx.locals
  • koa-views 3.x - ctx.state

Example:

// for koa-views 3.x
app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  // reference: require('./i18n_ref'),
  supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}],
  namespace: 'state'
}, app));

cookieKey {String}

cookies {Object}

Store the selected language in the cookie. see koa cookie for more information.

Example:

app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  cookies: {
    domain: 'hello.com',
    maxAge: 365*24*60*60*1000
  }
}, app));

default {String}

Default language

Example:

app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  // reference: require('./i18n_ref'),
  supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}],
  default: 'en_US'
}, app));

set_url {String}

The url to switch current language. default value: /set_locale

Example:

app.use(KAL({
  path: Path.resolve(__dirname, 'i18n'),
  // reference: require('./i18n_ref'),
  set_url: '/set_locale'
}, app));

Then you can set current language by request url like this '/set_locale?lang=en_US'

Local Params

The following three params are set to ctx.namespace, default ctx.locals. For koa-views 3.x version, you can set namespace to ctx.state

  • this.locals._i18n_ {Object}
  • this.locals._i18n_current_ {String}
  • this.locals._i18n_supported_ {Array}

static addLanguageHelper

添加dust模板的helper,用于展示语言名称

var dust = require('dustjs-helpers');
KAL.addLanguageHelper(dust)

在模板里使用@language

{@language code="ru_RU" current="zh_CN" /} // 输出"俄语"

{@language code="ru_RU" current="en_US" /} // 输出"Russian"

{@language code="ru_RU" /} // 根据系统设置的自动判断语言

Debug

Set the DEBUG environment variable to koa-adam-locale when starting your server.

$ DEBUG=koa-adam-locale

License

MIT