Giter Site home page Giter Site logo

i18n-helper's Introduction

i18n-helper

npm version


I18n helper for node, you can use it as a helper in jsx , handlebars , ejs or any other template engines .

Install

npm install i18n-helper --save

Usage

var locale = 'en';
var langs = {
    en: {
        'greetings': 'Hello {1} !'
    },
    zh: {
        'greetings': '你好 {1}!'
    }
};

var i18n = require('i18n-helper')(langs[locale]);

console.log(i18n('greetings', 'Jack')); // => 'Hello Jack !'

Init

i18n-helper exports a generator which returns an i18n helper.

You can pass multiply resources into this generator, the resources will be merged to one from right to left.

// global resource and module resource
var globalLangs = require('../i18n/en');
var moduleLangs = require('./i18n/en');

// the i18n helper generator
var i18nHelperGenerator = require('i18n-helper');

// create an i18n helper with the giving resources
var i18nHelper = i18nHelperGenerator(globalLangs, moduleLangs);

Find Key

The helper will try to find the matched key from the merged resource, if not found, the key itself is returned.

var i18n = i18nHelper({ key1: 'hello', key2: 'world' });

console.log(i18n('key'));
// => 'key'

You can override i18n.keyNotFound to apply your customized key not found handler ( >= 1.1.0 ).

var i18n = i18nHelper({ key1: 'hello', key2: 'world' });

i18n.keyNotFound = function(key) {
    return key + ' is not found';
};

console.log(i18n('key'));
// => 'key is not found'

Template

If the value to the key is a template ( with a string like {1} ), then the arguments will be substituted into the template.

var i18n = i18nHelper({ greetings: 'Hello {1} ! Welcome to {2} .' });

console.log(i18n('greetings', 'Jack', 'China'));
// => 'Hello Jack ! Welcome to China .'

Change Log

1.2.0

  • support templating key #1.

1.1.0

  • add keyNotFound handler.

Links

i18n-helper's People

Contributors

gbk avatar qinshou avatar

Stargazers

 avatar YueMing avatar  avatar Tim Zhang avatar  avatar tj.zhao avatar

Watchers

James Cloos avatar  avatar

i18n-helper's Issues

i18n的ID也进行参数替换

现在传入key,如果有就根据传参替换,返回出value。如果key不存在(意味着该条语言没有定义),则返回传入的key。
此时key会将传入的参数原封不动的返回回来,应该做替换。

现在找不时:

i18n('{1}颗赛艇', '一');

{1}颗赛艇

期望:

i18n('{1}颗赛艇', '一');

一颗赛艇

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.