如何翻译Meteor中的模板?
已更新 现在我尝试在我的应用程序中执行此操作(thx到Akshat) //普通的如何翻译Meteor中的模板?,meteor,Meteor,已更新 现在我尝试在我的应用程序中执行此操作(thx到Akshat) //普通的 LANG = 'ru'; Dictionary = new Meteor.Collection("dictionary"); //if server Meteor.startup(function () { if (Dictionary.find().count() === 0) { // code to fill the Dictionary } });
LANG = 'ru';
Dictionary = new Meteor.Collection("dictionary");
//if server
Meteor.startup(function () {
if (Dictionary.find().count() === 0) {
// code to fill the Dictionary
}
});
Meteor.publish('dictionary', function () {
return Dictionary.find();
});
//endif
//客户
t = function(text) {
if (typeof Dictionary === 'undefined') return text;
var res = Dictionary.find({o:text}).fetch()[0];
return res && res.t;
}
Meteor.subscribe('dictionary', function(){
document.title = t('Let the game starts!');
});
Template.help.text = t('How to play');
//html
<body>
{{> help}}
</body>
<template name="help">
{{text}}
</template>
{{>帮助}
{{text}}
仍然不能像我们希望的那样工作:当呈现模板时,字典是未定义的。但是
t('How to play')
在控制台中工作得很好)客户端和服务器之间不响应地共享Javascript变量。你必须使用流星收集来存储你的数据,例如
if (Meteor.isServer) {
var Dictionary = new Meteor.Collection("dictionary");
if(Dictionary.find().count() == 0) {
//If the 'dictionary collection is empty (count ==0) then add stuff in
_.each(Assets.getText(LANG+".txt").split(/\r?\n/), function (line) {
// Skip comment lines
if (line.indexOf("//") !== 0) {
var split = line.split(/ = /);
DICTIONARY.insert({o: split[0], t:split[1]});
}
});
}
}
if (Meteor.isClient) {
var Dictionary = new Meteor.Collection("dictionary");
Template.help.text = function() {
return Dictionary.find({o:'Let the game starts!'});
}
}
在上面的例子中,我假设您有autopublish
包(默认情况下,当您创建一个包时,它是在中的,所以这不会真正困扰您,但只是为了防止您删除)
对于您的文档标题,您必须使用稍微不同的实现,因为请记住,数据不会在运行Meteor.startup时下载,因为html和javascript是先下载的&数据是空的,然后数据缓慢进入(然后反应性地填充数据)DICTIONARY.insert({o:split[0],t:split[1]});我替换为Dictionary.insert({o:split[0],t:split[1]});但是在客户端Dictionary.find().count()仍然是zer0您是否删除了自动发布?您在哪里运行
Dictionary.find().count()
?如果它在最初运行的代码中的任何地方(不是在模板帮助程序中)它将返回0,因为数据在运行时(几毫秒后到达)对客户端不可用。在我添加发布和订阅后,我的集合被传输,find({o:smth})起作用。要获取属性t,我使用.fetch()[0]Thankstill并没有像我们想要的那样工作:当呈现模板时,字典是未定义的。你能再分享一点代码吗,我也不知道你的文本文件是如何构造的,而不是。fetch()[0]
你可以使用findOne()
当你运行Dictionary.find().fetch时,你在浏览器javascript控制台中得到了什么()
?