npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

localizablejs

v2.0.1

Published

A small library to handle translation and localization

Downloads

4

Readme

LocalizableJs

About

LocalizableJs is a small library to enable dynamically localized web pages, marked up in the DOM tree. It's fast and leaves a small footprint, and translations are instant. It's also available as a React component, giving the ability to easily have localized single page apps.

Installation

LocalizableJs supports CommonJS, AMD or simple script tag injection. It's also available as a NPM package, install via

npm install --save-dev localizablejs

or:

Download development version (10KB) Download minified production version (2KB)

Usage

LocalizableJs looks for DOM nodes with the default attribute name of data-translate-key. The value of that attribute should match a translation key in your dictionaries. If an attribute by the name data-translate-params is available, the translation can have formatting parameters.

Quick example

The DOM node

<div data-translate-key="WelcomeMessage" data-translate-params="Name"></div>

and JavaScript

var dictionaries = {
	"en-US": {
		"WelcomeMessage": "Hello {0}!"
	},
	"sv-SE": {
		"WelcomeMessage": "Hejsan {0}!"
	}
};
LocalizableJs.init(dictionaries, "en-US");

will result in the following output

Hello Name!

Change the language and translate all nodes

LocalizableJs.language("sv-SE");
LocalizableJs.translateAll();

and the output will instantly become

Hejsan Name!

React

LocalizableJs also ships as a React component, with attached listeners for language or dictionaries change.

var React = require('react'),
	LocalizableJs = require('localizablejs');

var dictionaries = {
	"en-US": {
		"WelcomeMessage": "Hello {0}!"
	},
	"sv-SE": {
		"WelcomeMessage": "Hejsan {0}!"
	}
};

var Header = React.createClass({
	componentDidMount: function () {
		LocalizableJs.init(dictionaries, "en-US");
	},
	doSetLanguage: function (language) {
		LocalizableJs.language(language);
	},
	render: function () {
		
		return (
			<header>
				<button onClick={this.doSetLanguage.bind(this, 'en-US')}>English</button>
				<button onClick={this.doSetLanguage.bind(this, 'sv-SE')}>Svenska</button>
			</header>
		);
		
	}
});

-----

var React = require('react'),
	LocalizableElement = require('localizablejs/react');

var MyComponent = React.createClass({
	render: function () {
	
		var params = ["Name"];
	
		return (
			<div>
				<LocalizableElement translationKey={"WelcomeMessage"} parameters={params} />
			</div>
		);
		
	}
});

External translation dictionaries

More than often are translation sources not available directly in the JavaScript files, but in external translation files. LocalizableJs supports external XML translation files, in the following format:

<?xml version="1.0" encoding="UTF-8"?>
<dictionaries>
	<dictionary language="en-US">
		<translation key="WelcomeMessage">Hello {0}!</translation>
	</dictionary>
	<dictionary language="sv-SE">
		<translation key="WelcomeMessage">Hejsan {0}!</translation>
	</dictionary>
</dictionaries>

To initialize LocalizableJs with an external translation file, simply pass the URI of the file as a parameter to the read method (and set a default language):

LocalizableJs.read("/translations/dictionaries.xml", "en-US");

API methods

LocalizableJs.init(dictionaries, defaultLanguage [, rootNode = document.body])

Initialize LocalizableJs with JavaScript dictionaries

LocalizableJs.read(xmlFileUri, defaultLanguage [, rootNode = document.body])

Initialize LocalizableJs with an external dictionaries source

LocalizableJs.setAttributeSelectors(key [, params])

Set new DOM attributes to listen to, defaults to data-translate-key and data-translate-params

LocalizableJs.language([lang])

Set or get the current language

LocalizableJs.dictionaries([dictionaries])

Set or get dictionaries

LocalizableJs.translate(key)

Translate a single key (this method returns the result, not writing it to the DOM)

LocalizableJs.translateAll([rootNode = document.body])

Translate all DOM nodes under the optionally specified root node

LocalizableJs.addChangeListener(fn)

Add an observer for when current language or dictionaries change

LocalizableJs.removeChangeListener(fn)

Remove an observer

Happy coding!