【发布时间】:2015-11-02 12:19:57
【问题描述】:
我正在寻找某种方法来访问 tap:i18n Meteor.js 包的数据,以便以正确的语言向用户发送电子邮件。
很遗憾,我在网上找不到任何关于此的内容。
我尝试访问 .json 和 $.getJSON,但没有成功。
有人解决这个问题吗?我的同事也面临同样的问题,但没有找到解决方案。
谢谢你,
大卫
【问题讨论】:
标签: javascript json meteor internationalization
我正在寻找某种方法来访问 tap:i18n Meteor.js 包的数据,以便以正确的语言向用户发送电子邮件。
很遗憾,我在网上找不到任何关于此的内容。
我尝试访问 .json 和 $.getJSON,但没有成功。
有人解决这个问题吗?我的同事也面临同样的问题,但没有找到解决方案。
谢谢你,
大卫
【问题讨论】:
标签: javascript json meteor internationalization
你检查过API docs吗?
如您所见,您可以在客户端上使用TAPi18n.getLanguage()。您可能正在使用方法触发电子邮件。所以你可以用语言传递一个额外的参数:
Meteor.call('sendMail', 'Hi!', TAPi18n.getLanguage())
您也可以使用Blaze.toHTML 在客户端呈现电子邮件 HTML。然后你可以将它传递给方法调用。
Meteor.call('sendMail', Blaze.toHTML(Template.myMailTemplate))
您也可以使用Blaze.toHTMLWithData 将一些数据传递到电子邮件。
如果您想向某个用户发送电子邮件,您只需将他们的语言偏好保存在他们的个人资料中即可。因此,每当您使用 TAPi18n.setLanguage 时,您都需要执行以下操作:
Meteor.users.update(Meteor.userId(), { $set: { 'profile.lang': newLang } })
TAPi18n.setLanguage(newLang)
然后你可以在服务器上使用meteorhaks:ssr:
server/*.js
var user = // Set this to the object of the user you want to send the mail to
Template.registerHelper('_', TAPi18n._.bind(TAPi18n))
var myEmailHtml = SSR.render('myEmail', { lang: user.profile.lang })
private/myEmail.html
<p>{{ _ 'Hi!' lang }}</p>
或者您可以只在 JavaScript 中生成 HTML:
var user = // Set this to the object of the user you want to send the mail to
var myEmailHtml = ['<p>' '</p>'].join(TAPi18n._('Hi!', user.profile.lang))
TAPi18n._ 已重命名为 TAPi18n.__。
THX /u/kvnmrz 的提示。
【讨论】: