akoutmos / mjml_eex

Create emails that WOW your customers using MJML and EEx
MIT License
84 stars 12 forks source link

How to use gettext inside the mjml template? #20

Open drselump14 opened 1 week ago

drselump14 commented 1 week ago

Hi, I'm not familiar with mjml syntax, but I was wondering if it's possible to use gettext inside the template?

mrdotb commented 3 days ago

I wanted to do the same today. I could not figure out how to make the gettext macro work directly in the template directly so I ended up passing assigns instead.

<mj-section padding-top="0">
  <mj-column>
    <mj-text mj-class="sans" font-weight="bold">
      <%= @hi %>
    </mj-text>

    <mj-text mj-class="display">
      <%= @message %>
    </mj-text>
...
  defp html_body(customer, action_url) do
    assigns =
      [
        action_url: action_url,
        hi: dgettext("emails", "Hi, %{email}", email: customer.email),
        message: dgettext("emails", "We noticed that you haven't started your free 30-day trial yet."),
        ...
      ]

    render(assigns)
  end

Extract and fill the gettext for the locale you support.

Before rendering set the locale in my case i have a locale field on my customer

  def new(customer) do
    if customer.locale do
      Gettext.put_locale(DotbWeb.Gettext, customer.locale)
    end

    Email.new(%{
      subject: subject(),
      html_body: html_body(customer, @action_url),
      text_body: text_body(customer, @action_url)
    })
  end

The mail in french image