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

时间:2015-08-10 14:19:32

标签: javascript json meteor internationalization

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

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

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

有人解决这个问题吗?我的同事在没有找到解决方案的情况下面临同样的问题。

谢谢你,

大卫

1 个答案:

答案 0 :(得分:5)

您检查了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提示。