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.

  • 1.4.0
  • 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

Installation

Either


npm install --save i18njs

or


bower install --save i18njs

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.lng = 'fr';

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('en');
// true

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;"

You can also change delimeters 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%>

Keywords

FAQs

Package last updated on 15 Apr 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