0
votes

I'm making a handlebars helper to simplify translation for an Ember app. It needs to be able to bind to a count option, because the count will change over time, and the translation needs to be plural or singular based on the count.

For example:

{{i18n translation.key countBinding='numberCount'}}

will go to the language file and see

translation: {
  key: {
    'one': 'widget',
    'other': 'widgets'
  }
}

and will return 'widget' or 'widgets' depending on the value of numberCount

Here is the code for the helper:

Ember.Handlebars.registerBoundHelper('i18n', function(property, options) {
  var params = options.hash,
    self = this;

  // Support variable interpolation for the options
  Object.keys(params).forEach(function(key) {
    params[key] = Em.Handlebars.get(self, params[key], options);
  });

  // Coerce number-like strings into numbers
  if (params.count && !isNaN(params.count) && typeof(params.count) == "string") {
    params.count = Number(params.count);
  }

  // I18n is the library that powers the translation
  return I18n.t(property, params);
});

If I use Ember.Handlebars.registerHelper instead of registerBoundHelper, it works fine, it just won't bind to the option. But when I try to make it a bound helper (as in the code above), the app will not load and the console gives me the error "Uncaught TypeError: Object # has no method 'split' "

Here are the docs for Ember.Handlebars.registerBoundHelper: http://emberjs.com/api/classes/Ember.Handlebars.html#method_registerBoundHelper

1

1 Answers

0
votes

Would it be work if we replace

var params = options.hash

with

var params = options.hash.boundOptions

And

{{i18n translation.key countBinding='numberCount'}}

with

{{i18n translation.key count=numberCount}}

? It works in my test environment.