I am developing a multilingual application in Meteor.js I would like to know, in your opinion, the best way to do this; for example, here is wat, which I'm doing right now (pretty sure that this can be done better);
First, I save the elements in mongodb with properties listed in the root of the language:
{ en: { name: "english name", content: "english content" }, it: { name: "italian name", content: "italian content" }, //since images are the same for both, are not nested images: { mainImage: "dataURL", mainThumb: "dataURL" } }
Then I publish the subscription using the current sessionLang variable:
Meteor.publish("elementsCurrentLang", function(currentLang) { var projection = { images: 1 }; projection[currentLang] = 1; return Elements.find({}, projection); });
I subscribe along the route using Iron Router waitOn hook:
Router.route('/eng/elements', { waitOn: function() { return Meteor.subscribe("municipalitiesCurrentLang", Session.get('currentLang')); }, action: function() { this.layout('ApplicationLayout'); this.render('elements'); } });
Now the first problem: I would like to reuse the same template for each language, but I canβt just add the {{name}} or {{content}} template, since the subscription returns the attributes nested under lang root, so itβs necessary make, for example, {{en.name}} for English or {{it.name}} for Italian; To avoid this, I use a template helper that creates a new object; essentially it removes attributes from the lang root:
Template.elements.helpers({ elements: function() { var elements = Elements.find(); var currentLang = Session.get('currentLang'); var resultList = []; elements.forEach(function(element, index) { var element = { name: element[currentLang].name, content: element[currentLang].nameUrl, images: element.images }; resultList.push(element); }); return resultList; } });
And now in the template, I can access attributes like Wanted:
<h1>{{name}}</h1> <p>{{content}}</p>
Before continuing this approach, I want to listen to the suggestions, as I do not know if this will work well; when will Session.currentLang change, will the subscription restart? is there any way to avoid forEach loop in template helpers?