polyglot.js | |
---|---|
Polyglot.js is an I18n helper library written in JavaScript, made to work both in the browser and in Node. It provides a simple solution for interpolation and pluralization, based off of Airbnb's experience adding I18n functionality to its Backbone.js and Node apps. Polylglot is agnostic to your translation backend. It doesn't perform any translation; it simply gives you a way to manage translated phrases from your client- or server-side JavaScript application. | |
!function(root) {
'use strict';
var phrases = {}; | |
Public Methods | |
Polyglot.extend(phrases)Use
The key can be any string. Feel free to call | function extend(morePhrases) {
for (var key in morePhrases) {
if (morePhrases.hasOwnProperty(key)) {
phrases[key] = morePhrases[key];
}
}
} |
Polyglot.clear()Clears all phrases. Useful for special cases, such as freeing
up memory if you have lots of phrases but no longer need to
perform any translation. Also used internally by | function clear() {
phrases = {};
} |
Polyglot.replace(phrases)Completely replace the existing phrases with a new set of phrases.
Normally, just use | function replace(newPhrases) {
clear();
extend(newPhrases);
} |
Polyglot.t(key, interpolationOptions)The most-used method. Provide a key, and
The phrase value is provided first by a call to Pass in an object as the second argument to perform interpolation.
If you like, you can provide a default value in case the phrase is missing. Use the special option key "_" to specify a default. | function t(key, options) {
var result;
options = options || {};
var phrase = phrases[key] || options._ || '';
if (phrase === '') {
warn('Missing translation for key: "'+key+'"');
result = key;
} else {
result = interpolate(phrase, options);
}
return result;
} |
Polyglot.pluralize(noun, count)Polyglot provides a very basic pattern for providing pluralization based on a singular noun. Because varous languages have different nominal forms for zero, one, and multiple, and because the noun can be before or after the count, we have to be overly explicit about the possibile phrases. For pluralizing "car", it assumes you have phrase keys of the form:
The second argument can be a | function pluralize(noun, count) {
if (count != null && count.length != null) {
count = count.length;
}
var key;
if (count === 0) {
key = "pluralize." + noun + ".zero";
} else if (count === 1) {
key = "pluralize." + noun + ".one";
} else {
key = "pluralize." + noun + ".many";
}
return t(key, {count: count});
} |
Polyglot.registerHandlebars(Handlebars)Registers Polyglot's Handlebars helpers on a given
Handlebars context. This is automatically called if we find
a global | function registerHandlebars(handlebars) {
for (var key in handlebarsHelpers) {
if (handlebarsHelpers.hasOwnProperty(key)) {
handlebars.registerHelper(key, handlebarsHelpers[key]);
}
}
} |
Private Methods | |
interpolateDoes the dirty work. Creates a | function interpolate(phrase, options) {
for (var arg in options) {
if (arg !== '_' && options.hasOwnProperty(arg)) { |
We create a new | phrase = phrase.replace(new RegExp('%\\{'+arg+'\\}', 'g'), options[arg]);
}
}
return phrase;
} |
warnProvides a warning in the console if a phrase key is missing. | function warn(message) {
root.console && root.console.warn && root.console.warn('WARNING: ' + message);
} |
Handlebars helpers | var handlebarsHelpers = { |
t
gives: | t: function(key, block) {
return t(key, block.hash);
}, |
pluralize
gives: | pluralize: function(noun, block) {
return pluralize(noun, block.hash.count);
}
}; |
Export public methods | var Polyglot = {
extend: extend,
replace: replace,
clear: clear,
t: t,
pluralize: pluralize,
registerHandlebars: registerHandlebars
}; |
Export for Node, attach to | if (typeof module !== 'undefined' && module.exports) {
module.exports = Polyglot;
} else {
root.Polyglot = Polyglot;
} |
Register Handlebars helpers if Handlebars is found.
If not, you can manually register Handlebars helpers by
calling | if (root.Handlebars && root.Handlebars.registerHelper) {
registerHandlebars(root.Handlebars);
}
}(this);
|