Git Product home page Git Product logo

locales's Introduction

koa-locales

NPM version build status Test coverage David deps npm download

koa locales, i18n solution for koa:

  1. All locales resources location on options.dirs.
  2. resources file supports: *.js, *.json and *.properties, see examples.
  3. One api: __(key[, value, ...]).
  4. Auto detect request locale from query, cookie and header: Accept-Language.

Installation

$ npm install koa-locales --save

Quick start

var koa = require('koa');
var locales = require('koa-locales');

var app = koa();
var options = {
  dirs: [__dirname + '/locales', __dirname + '/foo/locales'],
};
locales(app, options);

API Reference

locales(app, options)

Patch locales functions to koa app.

  • {Application} app: koa app instance.
  • {Object} options: optional params.
    • {String} functionName: locale function name patch on koa context. Optional, default is __.
    • {String} dirs: locales resources store directories. Optional, default is ['$PWD/locales'].
    • {String} defaultLocale: default locale. Optional, default is en-US.
    • {String} queryField: locale field name on query. Optional, default is locale.
    • {String} cookieField: locale field name on cookie. Optional, default is locale.
    • {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is 1y, expired after one year.
locales({
  app: app,
  dirs: [__dirname + '/app/locales'],
  defaultLocale: 'zh-CN',
}));

context.__(key[, value1[, value2, ...]])

Get current request locale text.

function* home() {
  this.body = {
    message: this.__('Hello, %s', 'fengmk2'),
  };
}

Examples:

__('Hello, %s. %s', 'fengmk2', 'koa rock!')
=>
'Hello fengmk2. koa rock!'

__('{0} {0} {1} {1} {1}', ['foo', 'bar'])
=>
'foo foo bar bar bar'

__('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
=>
'foo foo bar bar bar'

Usage on template

this.state.__ = this.__.bind(this);

nunjucks example:

{{ __('Hello, %s', user.name) }}

License

MIT

locales's People

Contributors

fengmk2 avatar greenkeeperio-bot avatar harbin1053020115 avatar hotoo avatar huacnlee avatar popomore avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.