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

Dav*_*art 3 javascript json internationalization meteor

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

不幸的是,我无法在网上找到任何有关此事的信息.

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

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

谢谢,

大卫

Kri*_*tig 5

你检查过API文档了吗?

正如您在那里看到的,您可以TAPi18n.getLanguage()在客户端上使用.您可能正在使用方法触发电子邮件.所以你可以用语言传递一个额外的参数:

Meteor.call('sendMail', 'Hi!', TAPi18n.getLanguage())
Run Code Online (Sandbox Code Playgroud)

使用模板呈现电子邮件

您也可以使用HTML客户端呈现电子邮件Blaze.toHTML.然后你可以将它传递给方法调用.

Meteor.call('sendMail', Blaze.toHTML(Template.myMailTemplate))
Run Code Online (Sandbox Code Playgroud)

您还可以使用Blaze.toHTMLWithData将一些数据传递给电子邮件.

呈现电子邮件服务器端

如果您有要向其发送电子邮件的用户,则只需将其语言首选项保存在其个人资料中即可.因此,无论何时使用,TAPi18n.setLanguage您都需要执行以下操作:

Meteor.users.update(Meteor.userId(), { $set: { 'profile.lang': newLang } })
TAPi18n.setLanguage(newLang)
Run Code Online (Sandbox Code Playgroud)

在服务器上,您可以使用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 })
Run Code Online (Sandbox Code Playgroud)

private/myEmail.html

<p>{{ _ 'Hi!' lang }}</p>
Run Code Online (Sandbox Code Playgroud)

或者您可以在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))
Run Code Online (Sandbox Code Playgroud)

更新

TAPi18n._更名为TAPi18n.__.

THX / u/kvnmrz的提示.