# GetText doesn't work in ActionMailer

**URL:** <https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253>\
**Category:** Rails I18n\
**Created:** [August 14, 2006, 4:21am UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253 "2006-08-14T04:21:10Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Stephane\_JAIS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@Stephane\_JAIS](https://www.ruby-forum.com/u/Stephane_JAIS)\
**Post date:** [March 30, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/21 "2007-03-30T17:54:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Stephane\_JAIS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@Stephane\_JAIS](https://www.ruby-forum.com/u/Stephane_JAIS)\
**Post date:** [March 30, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/22 "2007-03-30T17:30:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stephane\_JAIS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@Stephane\_JAIS](https://www.ruby-forum.com/u/Stephane_JAIS)\
**Post date:** [March 30, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/23 "2007-03-30T18:50:22Z")

</div>

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 - removed\_email\_address@domain.invalid

Web Services Developer

Pure Networks - [http://www.purenetworks.com](http://www.purenetworks.com)

---

<div class="post-metadata">

**Author:** ![Stephane\_JAIS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@Stephane\_JAIS](https://www.ruby-forum.com/u/Stephane_JAIS)\
**Post date:** [March 30, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/24 "2007-03-30T20:09:07Z")

</div>

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 🙂

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 ☹

- NR\_setlocale() is that your custom method setting GetText.locale?

best regards,  
Roland

---

<div class="post-metadata">

**Author:** ![Stephane\_JAIS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@Stephane\_JAIS](https://www.ruby-forum.com/u/Stephane_JAIS)\
**Post date:** [March 30, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/25 "2007-03-30T20:21:36Z")

</div>

Yes and yes.

* * *

Myk OLeary - removed\_email\_address@domain.invalid  
Web Services Developer  
Pure Networks - [http://www.purenetworks.com](http://www.purenetworks.com)

[Previous page](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253.md?page=1)
