GetText doesn't work in ActionMailer

Myk OLeary wrote:

Making a mailer in your root application solves that issue as well. The
.rhtml I’m using is full of _() that work fine.

Well I alread have a mailer model:

app/models/signup_mailer.rb

and it does not work.

Do you have template files per language? e.g. template_en.rhtml,
template_xy.rhtml?

Making a mailer in your root application solves that issue as well. The
.rhtml I’m using is full of _() that work fine.

-Myk

On Fri, 30 Mar 2007 6:58 am, Roland wrote:

Myk O’Leary wrote:

This will create a file RAILS_ROOT/app/model/mailer.rb

Use the _() in this new model after setting your locale, just as in a
controller.

Looks like a misunderstanding: The problem is that the _() does not
work
in rhtml templates used by ActionMailer.

–From the road on my Sidekick

I do not.

I’ll enclose snippets from my setup with what may help you out with
getting it to work. There are snippets not included for the actual
setting of the locale which I’ll assume you have since you have this
working elsewhere.

One note is that I have my controller, model and view one directory
level deeper than you likely do. This won’t affect success, but does
affect the directory paths, etc that you see below.

RAILS_ROOT/app/controllers/application.rb:

require ‘gettext/rails’

RAILS_ROOT/app/controllers/netreport/report_controller.rb:

def emailreport

  ...

  Netreport::Reporter.deliver_emailreport(@user, @locale)

  ...

end

RAILS_ROOT/app/models/netreport/reporter.rb:

class Netreport::Reporter < ActionMailer::Base

def emailreport(user, locale)

  require 'iconv'

ActionMailer::Base.default_charset = “iso-8859-1”

  C = Iconv.new('ISO-8859-1', 'UTF-8')

  ...

  NR_setlocale(locale)

  @subject = C.iconv(_("My subject"))

  hash_body = {"user" => user, "locale" => locale}

  @body = C.iconv(render_message("emailreport", hash_body))

  ...

end

RAILS_ROOT/app/views/netreport/reporter/emailreport.rhtml:

  NR_setlocale(@locale)

  _("Report for ")

Non gettext related notes are that we use iconv for translated emails at
present because several web mail providers are very inconsistent with
presentation of UTF8 character encoded emails. This did and will work
w/o needing to use iconv, but I don’t have the time today to revert back
to our previous code to make sure the snippets I post are working. I
know the above are, as they are in production right now. Make sure you
set the locale in the view, or you could very well not get the proper
translations.


Myk OLeary - [email protected]

Web Services Developer

Pure Networks - http://www.purenetworks.com

Myk O’Leary wrote:

I’ll enclose snippets from my setup with what may help you out with
getting it to work. There are snippets not included for the actual
setting of the locale which I’ll assume you have since you have this
working elsewhere.

Thank you very much :slight_smile:

two questions:

  • This means to get gettext working with ActionMailer you have to pass
    the current locale from the controller over to the Mailer-model and even
    down to the view? right?

This doesn’t look like DRY :frowning:

  • NR_setlocale() is that your custom method setting GetText.locale?

best regards,
Roland

Yes and yes.


Myk OLeary - [email protected]
Web Services Developer
Pure Networks - http://www.purenetworks.com