Skip to content

Instantly share code, notes, and snippets.

@davidwkeith
Created July 11, 2012 21:50
Show Gist options
  • Save davidwkeith/3093861 to your computer and use it in GitHub Desktop.
Save davidwkeith/3093861 to your computer and use it in GitHub Desktop.
Localization Helper for Ember.js's Handlebars
Handlebars.registerHelper('loc', function(property, fn) {
var str;
// we are bound to a value, it is now the context
if (fn.contexts && typeof fn.contexts[0] === 'string') {
str = fn.contexts[0];
// Convention, start all localization keys with _
} else if (property[0] === '_') {
str = property;
// Convention, start all globals with capital
} else if (/[A-Z]/.test(property[0])) {
str = Em.getPath(window, property)
// all other's are local properties
} else {
str = this.getPath(property)
}
return new Handlebars.SafeString((str || '').loc(''));
});
// use:
// {{loc _some_string}}
// {{#bind App.someString}}{{loc App.someString}}{{/bind}}
// {{#bind view.localString}}{{loc view.localString}}{{/bind}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment