使用 Meteor.js 访问 JS 中的 i18n 包数据

Access i18n package data inside the JS with Meteor.js

我正在寻找访问 tap:i18n Meteor.js 包数据的方法,以便以正确的语言向用户发送电子邮件。

不幸的是,我在网上找不到任何与此相关的内容。

我尝试访问 .json a $.getJSON,但没有成功。

有人解决了这个问题吗?我的同事也面临着同样的问题,但没有找到解决方案。

谢谢你,

大卫

你检查过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 提示。