2.0.2 • Published 4 years ago

node-locale v2.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Localization for NodeJS

A simple yet powerful module that loads a locale from a JSON file. This is a perfect if you looking to make your nodejs project multilingual.

Usage

const T = require('node-locale');

var t = new T({
    directories: ['./locales'], // An array containing path(s) to the "locale" folders.
    locale: 'en', // The locale directory to look into
    modules: ['users'], // The locale file(s) to load (omit the .json from the filename users.json)
});

// With the above settings, it will look for a locale file at: ./locales/en/users.json

// Outputs a simple string
console.log(t._('simple'));
console.log(t._('greetings'));


// Can also be used for nested JSON
console.log(t._('simple').a.b);

// Outputs a formatted string
console.log(t._('advanced', ['soubhik', 'angular']));

// Change the locale at run-time
t.locale = 'fr'

// Add a module name at run-time
t.addModule('users');

// or chain the method
t.addModule('users')
 .addModule('roles');

// Remove a module
t.removeModule('users');

// Get all modules
console.log(t.modules);

// Get locale string
console.log(t._('greetings'));

Checkout the Example

Options

Option NameDescriptionData TypeOptionalDefault Value
localeThe local folder nameStringNo-
modulesThe module(s) to loadArrayNo-
directoriesCustom directory path(s) from where the locale should be loaded.ArrayYes./resources/locale