0.0.3 • Published 9 years ago
intl-i18n v0.0.3
Intl I18n
A Simple and powerfull tool for Internationalization & Localization.
Installation
npm install intl-i18n
How It Works
This package has a single default class with an static function addLocaleData
for adding locale datas.
Main class, I18n
get default locale as an argument and return 5 functions:
__
messagePath, values
Formats complex messages, including plural and select arguments. (using ICU Message syntax)dt
datetime, options
Formats dates/times.rdt
datetime, options
Generates relative dates/times. (e.g. "2 mins ago").nm
number, options
Formats numbers.setLocale
localeName
Changes locale.
Example:
// Requirements
var I18n = require('intl-i18n');
var frLocaleData = require('intl-i18n/locales/fr');
var faLocaleData = require('intl-i18n/locales/fa');
// Configure
I18n.addLocaleData(frLocaleData, {
'fr-FR': {
formats: {
datetime: {
'my-format-name': { year: 'numeric', month: 'long', day: 'numeric', weekday: 'short' }
},
number: {
'CFP': { style: 'currency', currency: 'CFP' }
}
}
}
});
I18n.addLocaleData(faLocaleData, {
'fa-IR': {
formats: {
datetime: {
'my-format-name': { year: 'numeric', month: 'long', day: 'numeric', era: 'long', weekday: 'short' }
}
},
messages: {
'hello': { 'world': 'سلام جهان' },
'messages': '{messages, plural, =0 {هیچ پیامی} =1 {یک پیام} other {# پیام}} {messages, plural, =0 {ندارید} other {دارید}}.'
}
}
})
// Build (Set default locale here)
var i18n = new I18n('fr-FR');
var date = Date.UTC(1994, 1, 26, 0, 0, 0);
console.log(dt(date, { format: 'my-format-name' })); // "sam. 26 février 1994"
// Change locale to persian
i18n.setLocale('fa-IR');
console.log(dt(date, { format: 'my-format-name' })); // "شنبه ۷ ۱۲ ۱۳۷۲ هجری شمسی"
console.log(__('hello.world')); // "سلام جهان"
console.log(__('messages', { messages: 0 })); // "هیچ پیامی ندارید."
// Change locale to french
i18n.setLocale('fr-FR');
console.log(i18n.rdt(Date.now() - (1000 * 60 * 60 * 2))); // "il y a 2 heures"
console.log(i18n.nm(500, { format: 'CFP' })); // "500,00 CFP"