Translate JS
Lightweight library for managing translations and localization with simple api. Works client and server side. Less then 1.5KB.
Installation
npm install translate-js --save
Basic usage
// add items to translate registrytranslate; // get translations; // outputs "Project title"; // outputs "Click me George!"; // outputs "Please click me!"; // outputs "1 item"; // outputs "2 items"; // outputs "Deep nested label"
Advanced usage
// add items to specific localetranslate;translate; // add items to specific locale and prefix keys with custom namespacetranslate;translate; // get translations; // outputs "Project title"; // outputs "Título del Proyecto!"; // outputs "Value is not a number" // set locale for future translate callstranslate;translate; // outputs "es"; // outputs "El valor no es un número" // change what happens when item is not in registrytranslate { return key + ':undefined:' + locale};; // outputs "errors.dateInput:undefined:es" // change how translate interpolates template stringstranslate;translate;; // outputs "Hello George" // clear / empty translate registrytranslateclear; // create new translate registrytranslate;
API
translate
;
Translates string stored under specified key to current locale. Interpolates template string if templateData is given.
Specific locale translation can be specified via options.locale.
Pluralization is done in reference to "count" template data value when multiple variables are passed to translate function (override with options.pluralizeTo).
translate.add
translate;
Imports items (plain or nested object) to translate registry under specific locale (defaults to "en"). Prefix on item keys can optionally be added as keyPrefix parameter; Plural translation form variations must me supplied as array.
translate.setLocale
translate;
Sets current locale for future translate calls.
translate.getLocale
translate;
Gets current locale
translate.interpolateWith
translate;
Sets regular expression for template strings interpolation.
translate.whenUndefined
translate {};
Define custom handler for use case when requested item is not in registry.
translate.clear
translateclear;
clear / empty all items in translate registry
translate.setPluralizationRule
translate;
Set locale specific pluralization rule function to determine plural form variation index.
translate.createRegistry
translate;
Creates new isolated translate registry