Reputation: 656
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
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())
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.
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))
The TAPi18n._
was renamed to TAPi18n.__
.
THX /u/kvnmrz for the hint.
Upvotes: 5