ruby-on-railsrubyrails-i18nslim-lang

Text in helper won't translate


So here's the problem: Let's say, I have mails. The text inside is fully translated and everything is just fine. May it look like this (we're using SLIM on this project):

= t('foo.bar')
= t('foo.bar')
= t('foo.bar')
= thank_you_signature

So this thank_you_signature is a helper method from ApplicationMailerHelper. It is rather simple:

  def thank_you_signature
    SIGNATURE_TEMPLATE.render(self)
  end
 SIGNATURE_TEMPLATE = Slim::Template.from_heredoc <<-SLIM
    p.foo.bar #{I18n.t('helpers.mailers.foo')}
    foo.bar #{I18n.t('helpers.mailers.bar')}
  SLIM

So this is how it looks. And the problem is, it does not translate this = thank_you_signature. I have two locales, RU and EN. There are both, but in my mails, even though all the text is in english (as I choose my locale as user), this exact part remains in russian.

The first thing I checked was yml file, of course, but everything seems fine, there are no differences between ru.yml and en.yml files.

I've been on it for almost two days and I really don't get what's the trick here.


Solution

  • When you say:

    SIGNATURE_TEMPLATE = Slim::Template.from_heredoc <<-SLIM
      p.foo.bar #{I18n.t('helpers.mailers.foo')}
      foo.bar #{I18n.t('helpers.mailers.bar')}
    SLIM
    

    The #{...} string interpolation is done by Ruby when it is evaluating the heredoc so Slim::Template just sees a string like this:

    p.foo.bar return-value-from-I18n.t...
    foo.bar return-value-from-I18n.t...
    

    and the I18n.t calls will use the default locale (probably US English).

    You want Slim to handle the string interpolation so that the I18n.t calls are made when the template is rendered (i.e. when the locale is set to what you want it to be). To do that, you need to get the #{...} string interpolation past the heredoc by escaping it as \#{...}:

    SIGNATURE_TEMPLATE = Slim::Template.from_heredoc <<-SLIM
      p.foo.bar \#{I18n.t('helpers.mailers.foo')}
      foo.bar \#{I18n.t('helpers.mailers.bar')}
    SLIM