Access i18n package data inside the JS with Meteor.js Access i18n package data inside the JS with Meteor.js json json

Access i18n package data inside the JS with Meteor.js


Have you checked the API docs?

As you can see there, you can use TAPi18n.getLanguage() on the client. You are probably triggering the email using a Method. So you can just pass an additional argument with the language:

Meteor.call('sendMail', 'Hi!', TAPi18n.getLanguage())

Rendering Email using Templates

You could also just render the email HTML client-side using Blaze.toHTML. Then you can pass that to the method call.

Meteor.call('sendMail', Blaze.toHTML(Template.myMailTemplate))

You could also use Blaze.toHTMLWithData to pass some data to the email.

Rendering the Emails Server-Side

If you have a user to which you want to send the email to, you can simply save their language preference inside their profile. So whenever you use TAPi18n.setLanguage you'll need to do something like this:

Meteor.users.update(Meteor.userId(), { $set: { 'profile.lang': newLang } })TAPi18n.setLanguage(newLang)

On the server you can then either use meteorhaks:ssr:

server/*.js

var user = // Set this to the object of the user you want to send the mail toTemplate.registerHelper('_', TAPi18n._.bind(TAPi18n))var myEmailHtml = SSR.render('myEmail', { lang: user.profile.lang })

private/myEmail.html

<p>{{ _ 'Hi!' lang }}</p>

Or you could just generate the HTML in JavaScript:

var user = // Set this to the object of the user you want to send the mail tovar myEmailHtml = ['<p>' '</p>'].join(TAPi18n._('Hi!', user.profile.lang))

Update

The TAPi18n._ was renamed to TAPi18n.__.

THX /u/kvnmrz for the hint.