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

simple-locale-i18n

v1.8.0

Published

Very simple locale handler

Downloads

3

Readme

Simple Locale Internationalization (I18n)

This is an extremely simple i18n module, only for those too lazy to code their own.

API

Instanciation

var locales = require('simple-locale-i18n')('en', './locales');

To instanciate a locales object, call require and the constructor function by passing 3 parameters to it:

|| default_locale | directory | callback | |:-:|:-:|:-:|:-:| |Required| false | false | false| |Description| The default locale when none specified, example: en| The directory to read locales from. Locales will have the name of the files they are read from | A callback function to call when all locales are read |

Locale Files

The module will read the folder ./locales by default. This folder should be located in the same directory where the locales object is instanciated. You can change the folder name by adding a 2nd parameter to the constructor function.

Format

By default, locale files are in JSON format, thus .json extension (this will be checked against when instanciated). And should be written like:

{
	"some_key": "The translation",
	"some_other_key": "Another translation"
}

Translating

To translate simply call the i18n method from the instanciated object:

var locales = require('simple-locale-i18n')('en', './locales');
locales.i18n('trad.hello'); // 'Hello!'
locales.i18n('trad.hello', 'fr'); // 'Salut!'

To simplify, you can also define a helper variable:

var locales = require('simple-locale-i18n')('en', './locales');
const i18n = locales.i18n;
i18n('trad.hello'); // 'Hello!'
i18n('trad.hello', 'fr'); // 'Salut!'

Building

You can also build strings, mustache-style:

var string = 'Hello {{ name }}! This is a translation into {{ language }} using {{ package }}';
i18n.build(string, {
		name: 'John',
		language: 'English',
		package: 'simple-locale'
});
// returns 'Hello John! This is a translation into English using simple-locale'

Translate

This is a utility function that allows you to simply build strings only giving the locale. It is a composition of i18n and build.

//en.trad.say_hello = 'Hello we are in {{ country }}!';
//fr.trad.say_hello = 'Salut, on est en {{ country }}!';
var string = i18n.translate('trad.say_hello', {
	[locale]: 'en',
	[placeholder]: 'Hi!',
	values:{
		en: {
			country: 'England'
		},
		fr:{
			country: 'France'
		}
	}
});

string === 'Hello we are in England' // true