forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

i18n variable text doesn't translate #261

Closed claylua closed 6 years ago

claylua commented 6 years ago

Hi Guys,

It's me again. And with another issue that i faced. Assuming i have a text variable on nodejs as follow,

var text = time + " seconds";

now, i'll sent an email like this

                email.send({
                  template: 'example',
                  message: {
                    to: "help@me.please",
                  },
                  locals: {
                    locale: 'en',
                    time: text,
                  },
                }).then(function(result) {
                  console.log('email sent');
                }).catch(function(err) {
                  console.error(err);
                });

now with the pug like below

$(t(`tell me the time right now!`)} ${time}

everything works fine, other than the 'seconds' here in the locals 'time' doesn't translate to another language.

i tried the following on nodejs code

var text = time + " ${t(`seconds`)}";

but the email i receive would be

tell me the time right now! ${t(`seconds`)}

now, the reason why i need these to be in the variable is because there are days, minutes, hours and seconds. These keywords are placed on nodejs rather than pug and i'm trying to find ways to separate these keywords so that it also translate normally like other text in pug.

any ways to make the text in each variable translate or workaround for such issue? Thanks!

OmgImAlexis commented 6 years ago

You need to use backticks instead of double quotes when you're using ${}.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

For example.

var text = `${time} ${t('seconds')}`;
claylua commented 6 years ago

@OmgImAlexis thanks for the super quick response! apparently using backticks will evaluate ${} but it will throw the following exception

ReferenceError: t is not defined

since t() is only available in pug under this library. But without this, email-templates library won't translate the text. any thoughts?

niftylettuce commented 6 years ago

t is available through https://github.com/ladjs/i18n - does that solve your issue @claylua ?

Freundschaft commented 6 years ago

I also have a question on this, is it possible to render i18n without sending email, e.g. just by using email.render?

niftylettuce commented 6 years ago

@Freundschaft Thank you for the comment! I had overlooked this when I was crafting the API methods. I've now moved all the logic for localization/i18n from the send method to the render method. It should work as you'd expect now! You can now render i18n without sending an email simply by using email.render as you suggested. Please try v3.3.1 available on npm now:

npm install email-templates@latest

or with yarn:

yarn add email-templates@latest

cc @claylua

Freundschaft commented 6 years ago

thanks a lot!