David Panart
David Panart

Reputation: 656

Access i18n package data inside the JS with Meteor.js

I'm looking for some way to access to the data of the tap:i18n Meteor.js's package in order to send emails in the correct language to the user.

Unfortunaly, I can't find anything working on the web about that.

I've tried to access the .json a $.getJSON, without success.

Has someone a solution to this problem ? My coworkers are facing the same problem without finding a solution.

Thanks you,

David

Upvotes: 3

Views: 1111

Answers (1)

halbgut
halbgut

Reputation: 2386

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 to
Template.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 to
var myEmailHtml = ['<p>' '</p>'].join(TAPi18n._('Hi!', user.profile.lang))

Update

The TAPi18n._ was renamed to TAPi18n.__.

THX /u/kvnmrz for the hint.

Upvotes: 5

Related Questions