Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18njs

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18njs

A simple i18n for Javascript with a templating feature.

  • 2.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
119
increased by4.39%
Maintainers
1
Weekly downloads
 
Created
Source

i18njs

A simple i18n for Javascript with a templating feature.

npm version bower version travis

Installation

Either


npm install --save i18njs

or


bower install --save i18njs

Test


npm test

Usage

After importing it var i18n = require('i18njs');

Add locales


var en_locales = {
	'hello_world': {
		'hello': 'Hello',
		'world': 'World'
	}
};

var fr_locales = {
	'hello_world': {
		'hello': 'Bonjour',
		'world': 'Monde'
	}
};

// i18n.add(language, [namespace,] locales);
i18n.add('en', 'first_test', en_locales);
i18n.add('fr', 'first_test', fr_locales);

Change language

By default, language is set to en.


i18n.setLang('fr');

Get current language


i18n.getCurrentLang();

Get dictionary


i18n.getDico();

Check for availability

If needed, you can also check for the presence of a specific localized string in a particular language.

You can check only the language too.

// i18n.has([key,] lang)
i18n.has('first_test.hello_world.hello', 'en');
// true

i18n.has('first_test.hello_world.hello');
// true

i18n.has('en');
// true

List available languages


i18n.listLangs();
// ['en', 'fr']

Get basic localized string


// i18n.get(key[, data, options][, lang]);
i18n.get('first_test.hello_world.hello');
// Hello

i18n.get('first_test.hello_world.hello', 'fr');
// Bonjour

Get templated string

It uses a basic templating engine, the same as underscore.

It works in the form of {{=interpolate}}, {{evaluate}} or {{-escape}} :


var en_locales = {
	'st': '{{=interpolate}} {{for(var i = 0, max = 1; i < max; i += 1) {}}to{{}}} {{-escape}}'
};

var data = {
	'interpolate': 'Hello',
	'escape': '\'<the>\' `&` "World"'
};

i18n.add('en', en_locales);

var st = i18n.get('st', data);
// "Hello  to  &#x27;&lt;the&gt;&#x27; &#x60;&amp;&#x60; &quot;World&quot;"

Change delimiters

You can also change delimiters by passing the third options arguments


var st = i18n.get('st', data, {
	evaluate: /<%([\s\S]+?)%>/g;
    interpolate: /<%=([\s\S]+?)%>/g;
    escape: /<%-([\s\S]+?)%>/g;
});

Will result in <%=interpolate%>, <%evaluate%> or <%-escape%>

Add default values for templates

If you need to have a special key always replaced by the same value (brand for example), you can set it as default.

var defaults = {
	fr: {
		key: 'default fr'
	},
	en: {
		key: 'default en'
	}
};

i18n.setDefaults(defaults);
i18n.get('ns.inter')
//default en

If not needed, you don't have to use localized defaults :

var defaults = {
	key: 'My Brand'
};

i18n.setDefaults(defaults);
i18n.get('ns.inter')
//My Brand

Keywords

FAQs

Package last updated on 03 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc