GithubHelp home page GithubHelp logo

ianfeather / i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from koa-modules/i18n

0.0 1.0 0.0 119 KB

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

License: MIT License

Makefile 4.39% HTML 0.45% JavaScript 95.15%

i18n's Introduction

koa-i18n

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

NPM version Build status Test coverage License Dependency status

Installation

$ npm install koa-i18n

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');

// Required!
locale(app);

render(app, {
  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`
  ]
}));

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 is detected, no continue to detect.

Dependencies

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

License

MIT

i18n's People

Contributors

fundon avatar ianfeather avatar robinqu 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.