Multilingual in Meteor -
i'm developing multilingual app in meteor.js know best way in opinion that; example here wat i'm doing right (pretty sure can done better);
first save items in mongodb properties neted in language root:
{ en: { name: "english name", content: "english content" }, it: { name: "italian name", content: "italian content" }, //since images same both, not nested images: { mainimage: "dataurl", mainthumb: "dataurl" } }
then publish subscription using currentlang session variable:
meteor.publish("elementscurrentlang", function(currentlang) { var projection = { images: 1 }; projection[currentlang] = 1; return elements.find({}, projection); });
i subscribe on 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 first problem: reuse same template every language, can't put in template {{name}} or {{content}} since subscription returns attributes nested under lang root, needed example {{en.name}} english or {{it.name}} italian; avoid use template helper buids new object; removes attributes 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 in template can access attributes wanted:
<h1>{{name}}</h1> <p>{{content}}</p>
before continuing approach want listen suggestions, since don't know if work well; when session.currentlang change, subscription reloaded? there way avoid foreach loop in template helpers?
i'm developping multilangage web app , advise use package, 1 : https://atmospherejs.com/tap/i18n
you can change langage reactively. have same template langages, want ! can put parameter in route. personnaly use session variable , in user profile !
if use package, can export app, or part of it, more many developpers use same code.
you put words in json files :
en.i18n.json: { "hello": "hello" } fr.i18n.json: { "hello": "bonjour" }
and
{{_ "hello" }}
will write hello or bonjour depending of langage set. can set :
tapi18n.setlanguage(getuserlanguage()) //getuserlanguage() <- function current langage in user profile or 1 used navigator
Comments
Post a Comment