0

I am using an image that contains text inside an email I send to users

For example: enter image description here

I have a User table with a user_country attribute (for example France) and a method I use on the website to induce the language :

module LocaleSetter

    DefaultLocale = 'en'
  extend ActiveSupport::Concern

    included do
        before_filter :set_locale 
    end

  def set_locale
    I18n.locale = extract_locale_from_country
  end

  def extract_locale_from_country       
    case I18nData.country_code(set_country)
      when 'US'
        'en'
      when 'FR'
        'fr'
      when 'ES'
        'es'
      when 'DE'
        'de'
      when 'NL'
        'nl'
      else
        DefaultLocale # default locale if no other is set

    end
  end

end

If I write the letter in different languages (french, english, german...), how to tell my app to condtionnaly choose the right image's src image it points to i.e the picture with french text if it's an email to French users, but the picture with german text if it goes to a German user ?

<td style="margin: 0 auto;padding: 0;display: block;max-width: 600px;clear: both; color: #454545;">
          <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff" >
            <tbody>
              <tr>
                <td>
                  <a href="<%= t("mail.useful_adresses.homepage") %>" rel="nofollow" target="_blank"> 
                    <img style="display: block;" width="590px" height="163px" border="0" align="left" alt="<%= raw t("mail.subscription_mails.welcome_notifier_mail.welcome") %>" src="http://example.com/emails-images/optimized-mail-top.png">
                  </a>
                </td>
              </tr>  
            </tbody> 
            </table>   
        </td> 

Thanks

PS: all translations of texts with t("texte") through i18n works well in the emails. My problem involves images src

Mathieu
  • 4,587
  • 11
  • 57
  • 112

1 Answers1

1

The easiest solution is to name your image files with country code prefix like en-mail-top.png or de-mail-top.png and then just build image source url using current locale:

<td style="margin: 0 auto;padding: 0;display: block;max-width: 600px;clear: both; color: #454545;">
  <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff" >
    <tbody>
      <tr>
        <td>
          <a href="<%= t("mail.useful_adresses.homepage") %>" rel="nofollow" target="_blank"> 
            <img style="display: block;" width="590px" height="163px" border="0" align="left" alt="<%= raw t("mail.subscription_mails.welcome_notifier_mail.welcome") %>" src="http://example.com/emails-images/<%= I18n.locale %>-optimized-mail-top.png">
          </a>
        </td>
      </tr>  
    </tbody> 
  </table>   
</td>

You can also create a helper method that will generate this full image url so email template can be more clear.

Michał Młoźniak
  • 5,466
  • 2
  • 22
  • 38