Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedaviddias/5426692 to your computer and use it in GitHub Desktop.
Save thedaviddias/5426692 to your computer and use it in GitHub Desktop.

Localisation extension for Handlebars.js

This helper uses a global "window.locale" object to translate the keywords to text strings.

Features:

  • Multi-language support
  • Keyword nesting
  • Automatically sets the language (from the browser properties)

Usage:

In your handlebars template, call the 'l10n' and include the keyword path as a string. Example:

{{l10n "errors.minFileSize"}}
Handlebars.registerHelper('l10n', function(keyword) {
var lang = (navigator.language) ? navigator.language : navigator.userLanguage;
// pick the right dictionary
var locale = window.locale[lang] || window.locale['en-US'];
// loop through all the key hierarchy (if any)
var target = locale;
var key = keyword.split(".");
for (i in key){
target = target[key[i]];
}
//output
return target;
});
window.locale = {
"en-US": {
"errors": {
"maxFileSize": "File is too big",
"minFileSize": "File is too small",
"acceptFileTypes": "Filetype not allowed",
"maxNumberOfFiles": "Max number of files exceeded",
"uploadedBytes": "Uploaded bytes exceed file size",
"emptyResult": "Empty file upload result"
},
"error": "Error",
"start": "Start",
"cancel": "Cancel",
"destroy": "Delete"
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment