1
votes

I have a list of i18n translation strings within an meteor-i18n object that I'm iterating over. Instead of creating a Template Helper for each string manually though, which would soon become redundant and repetitive, I would like to create the Helpers dynamically, within a loop, like so:

for (var namespace in Meteor.i18nMessages) {
  for (var msg in Meteor.i18nMessages[namespace]){
    //Template[namespace][msg] = __(namespace + "." + msg); // <- works but is not reactive
    Template[namespace][msg] = function() { // <- Doesn't work: always returns last value from object
      return __(namespace + "." + msg);
    }
  }
}

However when I do, I lose reactivity. How would one go about solving this? I'm a fan of best-practices and elegant code.

Thanks.

2

2 Answers

2
votes

You need to precompile the handlebars templates with

Meteor._def_template("templateName", function () { 
    return "your html"
});

This creates a template at Template.templateName which will be reactive with the helpers defined for it.

EventedMind has some screencasts that show how this works in good detail:

0
votes

My text editor keeps calling me "evil" for using eval but this works:

for (var namespace in Meteor.i18nMessages) {
  var obj = {};
  for (var msg in Meteor.i18nMessages[namespace]) {
    var str = 'obj["' + msg + '"] = function() { return __("' + namespace + '.' + msg + '"); }';
    console.log(str);
    eval(str);
  } 
  Template[namespace].helpers(obj);
}