Skip to content

Instantly share code, notes, and snippets.

@jgillich
Last active January 4, 2016 16:49
Show Gist options
  • Save jgillich/8649432 to your computer and use it in GitHub Desktop.
Save jgillich/8649432 to your computer and use it in GitHub Desktop.
Handlebars localisation helper, inspired by https://gist.github.com/tracend/3261055
Handlebars.registerHelper('loc', function(keyword) {
var lang = navigator.language || navigator.userLanguage;
// If lang is not found, try without subtag and fall back to en
lang = locale[lang] || (lang.indexOf('-') !== -1 && locale[lang.split('-')[0]]) || locale['en'] ;
return lang && _.template('<%= ' + keyword + ' %>', lang) || keyword;
/* without underscore
var keys = keyword.split(".");
for (var i in keys) {
lang = lang[keys[i]];
}
return lang || keyword;
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment