About the language globalization-Collection of common programming errors
How to make the meteor application support multiple languages ? for example :Chinese,English.
The first,I try to use the Handlebars.registerHelper and Session ,but I’m failed.
The test.js code :
Handlebars.registerHelper('language',function(arg){ var nalization = Session.get('nalization'); console.log(nalization); var language = Session.get("language")[nalization]; if (!language){ console.log("nalization"+nalization+" is undefined"); return ""; } console.log(arg); return language[arg] ? language[arg] : "undefined"; }); Template.hello.created = function(){ Session.set('nalization','cn'); } Deps.autorun(function (c) { Session.set("language",{ cn : { hello: "你好", language: "Language" }, en : { hello: "Hello", language: "语言" } }); c.stop(); }); Template.hello.events = { "click #language_cn":function(){ Session.get("nalization") !== "cn" ? Session.set("nalization","cn") : 1=1; }, "click #language_en":function(){ Session.set("nalization","en"); console.log(); } }
The test.html code :
{{> hello}}
{{#language "hello"}}{{/language}}
{{#language "language"}}{{/language}} 中文 English
changed the value 'nalization' in Seesion by click events,but the value is not change in registerHeloer .nothing happen on html.
Any idea about the language globalization ? Thanks.
-
Language = new Meteor.Collection(); session.setDeafault("language","EN_US"); Deps.autorun(function(c){ var languages = [ { name:"EN_US", value:{ username:"username", password:"password" } }, { name:"ZH_CN", value:{ username:"yonghu", password:"mima" } } ]; if(Language.find({}).fetch().length) Language.remove({}); for(index in languages){ Language.insert(languages[index]); } c.stop(); }); Handlebars.helpers('language',function(){ return Language.findOne({name:Session.get("language")}).value; }); /** Change language by click events for example: "click #chooseLanguage":function(){ Session.set("language","ZH_CN"); } in the html: {{language.username}} {{language.password}} */
Originally posted 2013-11-09 19:42:32.