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.66k stars 339 forks source link

Variable's evaluation in localized text #375

Closed Kerhael closed 4 years ago

Kerhael commented 4 years ago

Hi,

I'm currently having trouble with the evaluation of a variable within a translated string.

Here is my .js file:

const email = new Email({
   transport: transporter,
   send: true,
   preview: false,
   views: {
      root: 'server/lib/mailer/templates',
   },
   message: {
      from: `Me`,
   },
   i18n: {
      locales: ['en', 'fr'],
      directory: 'i18n/locales',
      defaultLocale: 'fr',
      updateFiles: false,
   }
});

[...]

let sendMailResetPassword = async function() {
...
   return email.send({
      template: 'reset-password',
      message: {
         to: to,
      },
      locals: {
         name: 'John',
         locale: 'en',
      },
   });
}

Here my en.json:

{
  "mail.password_change.line_1": "{{name}}'s password has been reset",
}

And my html.pug file:

...
div
   | #{t('mail.password_change.line_1', {name: <how_do_i_write_name_variable?>})}
br
...

I would like to see "John's password has been reset" as a result, however I am not able to find the correct syntax. If I directly write #{t('mail.password_change.line_1', {name: "John"})}, the result is what I expect. If I change "John" to #{name} or ${name}, I get a pug error INVALID_TOKEN.

And I assume I have the same question for text.pug file :)

Kerhael commented 4 years ago

OK just found the solution!

Here it is:

html.pug:

| #{t('mail.password_change.line_1', {name: `${name}`})}

text.pug:

= `${t('mail.password_change.line_1', {name: `${name}`})}`

In case it helps someone else :)