Skip to content

Instantly share code, notes, and snippets.

@zoghal
Forked from davidwkeith/localization_helper.js
Created July 24, 2012 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoghal/3172058 to your computer and use it in GitHub Desktop.
Save zoghal/3172058 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