# 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:** 20\
**Page:** 1

<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:** [August 14, 2006, 4:21am UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/1 "2006-08-14T04:21:10Z")

</div>

Hi,

I’m using gettext 1.7.0 and rails 1.1.4.

Gettext won’t translate:

- the subject line of my e-mail
- the view that renders its body.

I have followed the integration guide at:  
[http://manuals.rubyonrails.org/read/chapter/105](http://manuals.rubyonrails.org/read/chapter/105)

Here’s what my application controller looks like:

class ApplicationController \< ActionController::Base  
before\_filter :init\_gettext  
def init\_gettext  
bindtextdomain(“myapp”, request.cgi)  
end

Here’s my ActionMailer:

class OrderMailer \< ActionMailer::Base  
def confirm(order)  
@subject = \_(“Your order with myapp”)

The subject is not translated unless I call bindtextdomain right before.

The rhtml template that generate the body is just not translated at all.  
I can still create localized versions of it:  
cp app/views/order\_mailer/confirm.rhtml  
app/views/order\_mailer/confirm\_fr.rhtml  
but that’s a little bit disapointing.

Any idea how to get that \_() function to work everywhere ?

Thanks,

```
 Stephane.

```

---

<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:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/2 "2006-08-15T14:19:32Z")

</div>

Hi Stephane,

I try gettext using:

class ApplicationController \< ActionController::Base  
init\_gettext “myapp”  
…  
end

and it works.

Alternative with bindtextdomain:

I’m not sure, but when using bindtextdomain for tests it  
was looking for the mo-file in locale/de/myapp.mo and  
not in locale/de/LC\_MESSAGES/myapp.mo so you may have to  
try with different directories.

Or add a path:

add\_default\_locale\_path(  
File.dirname( **FILE** ) + “/locale/%{locale}/LC\_MESSAGES/%{name}.mo” )

May it helps.

Thomas

Am 14.08.2006 um 04:21 schrieb Stephane JAIS:

> [http://manuals.rubyonrails.org/read/chapter/105](http://manuals.rubyonrails.org/read/chapter/105)
> 
> cp app/views/order\_mailer/confirm.rhtml  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Railsi18n-discussion mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/railsi18n-discussion](http://rubyforge.org/mailman/listinfo/railsi18n-discussion)

* * *

Thomas B. - Freiberuflicher Softwarecoach  
fon: +49(40)411 622 35 mobil: +49(173)23 911 43  
fax: +49(40)411 622 36  
thomas.baustert(at)[b-simple.de](http://b-simple.de)

---

<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:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/3 "2006-08-15T14:19:55Z")

</div>

Hi,

It may be a bug of gettext-1.7.0.

Could you try this patch to your gettext/rails.rb ?

— rails.rb.old 2006-08-15 00:31:42.000000000 +0900  
+++ rails.rb 2006-08-15 00:33:08.000000000 +0900  
@@ -60,7 +60,7 @@  
# They are remained for backward compatibility.  
#  
def bindtextdomain(domainname, options = {}, locale = nil, charset  
= nil, with\_model = true)

- 

```
 opt = {:with_helper => true, :with_model => true}

```

- 

```
 opt = {:with_helper => true, :with_model => true, :with_mailer => 

```

true}  
if options.kind\_of? CGI  
# For backward compatibility  
opt.merge!(:cgi =\> options, :locale =\> locale, :charset =\>  
charset, :with\_model =\> with\_model)  
@@ -73,6 +73,7 @@  
ret = \_bindtextdomain(domainname, opt)  
bindtextdomain\_to(ActiveRecord::Base, domainname) if  
opt[:with\_model]  
bindtextdomain\_to(ApplicationHelper, domainname) if  
opt[:with\_helper]

- 

```
 bindtextdomain_to(ActionMailer::Base, domainname) if 

```

opt[:with\_mailer]  
ret  
end

On Mon, 14 Aug 2006 04:21:10 +0200

---

<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:** [August 16, 2006, 3:19am UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/4 "2006-08-16T03:19:14Z")

</div>

Hi,

On Wed Aug 16 05:54:18 JST 2006  
Stephane JAIS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, the patch is not doing anything.  
> Did you try it?

> I’m using version 1.7.0 which is the last version.  
> No mention of ‘with\_mailer’ in the source.

Of course. Because this patch is against 1.7.0.

> > @@ -60,7 +60,7 @@  
> > # They are remained for backward compatibility.  
> > #  
> > def bindtextdomain(domainname, options = {}, locale = nil,  
> > charset  
> > = nil, with\_model = true)
> > 
> > - 
> > 
> > ```
> > opt = {:with_helper => true, :with_model => true}
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > opt = {:with_helper => true, :with_model => true, :with_mailer 
> > 
> > ```

=\>

---

<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:** [August 15, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/5 "2006-08-15T22:54:18Z")

</div>

Well, the patch is not doing anything.

I’m using version 1.7.0 which is the last version.  
No mention of ‘with\_mailer’ in the source.

$ grep -rs with\_mailer /usr/local/lib/ruby/gems/1.8/gems/gettext-1.7.0/  
$

Any other idea ?

Thanks a lot,

```
 Steph.

```

Masao M. wrote:

> Hi,
> 
> It may be a bug of gettext-1.7.0.
> 
> Could you try this patch to your gettext/rails.rb ?
> 
> — rails.rb.old 2006-08-15 00:31:42.000000000 +0900  
> +++ rails.rb 2006-08-15 00:33:08.000000000 +0900  
> @@ -60,7 +60,7 @@  
> # They are remained for backward compatibility.  
> #  
> def bindtextdomain(domainname, options = {}, locale = nil, charset  
> = nil, with\_model = true)
> 
> - 
> 
> ```
> opt = {:with_helper => true, :with_model => true}
> 
> ```
> 
> - 
> 
> ```
> opt = {:with_helper => true, :with_model => true, :with_mailer => 
> 
> ```
> 
> true}  
> if options.kind\_of? CGI  
> # For backward compatibility  
> opt.merge!(:cgi =\> options, :locale =\> locale, :charset =\>  
> charset, :with\_model =\> with\_model)  
> @@ -73,6 +73,7 @@  
> ret = \_bindtextdomain(domainname, opt)  
> bindtextdomain\_to(ActiveRecord::Base, domainname) if  
> opt[:with\_model]  
> bindtextdomain\_to(ApplicationHelper, domainname) if  
> opt[:with\_helper]
> 
> - 
> 
> ```
> bindtextdomain_to(ActionMailer::Base, domainname) if 
> 
> ```
> 
> opt[:with\_mailer]  
> ret  
> end
> 
> On Mon, 14 Aug 2006 04:21:10 +0200

---

<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:** [November 29, 2006, 1:09am UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/6 "2006-11-29T01:09:19Z")

</div>

Hi,

On Tue, 28 Nov 2006 18:06:39 +0100  
Mihnea C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> translated automatically. Was it supposed to, or does it need to be  
> split in one version per language?

I coudn’t understand your problem.

You might need to try the tutorial once below.  
You can know what Ruby-GetText produces.  
[http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html](http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html)

At least, you should try the newest version of Ruby-GetText(1.8.0).  
Then tell me your problem concretely.

---

<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:** [November 28, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/7 "2006-11-28T18:06:39Z")

</div>

Masao M. wrote:

> Hi,
> 
> It may be a bug of gettext-1.7.0.
> 
> Could you try this patch to your gettext/rails.rb ?  
> […]

Hello, I’ve experienced the same problem and tried the patch. It works,  
in as much as it translates Subject etc. However the view doesn’t get  
translated automatically. Was it supposed to, or does it need to be  
split in one version per language?

---

<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:** [November 30, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/8 "2006-11-30T16:27:27Z")

</div>

Masao M. wrote:

> Hi,
> 
> On Tue, 28 Nov 2006 18:06:39 +0100  
> Mihnea C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > translated automatically. Was it supposed to, or does it need to be  
> > split in one version per language?
> 
> I coudn’t understand your problem.

The problem is (was) that the ‘\_’ function was doing nothing in  
ActionMailer views.

This was happening in 1.7 until I called bindtextdomain. Don’t know  
about 1.8.

---

<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:** [November 30, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/9 "2006-11-30T16:28:26Z")

</div>

Michael wrote:

> I’m having issues with ActionMailer too. The problem is that I’m calling  
> ActionMailer with a cron task, for example:  
> ./script/runner Somerecord.find\_stuff\_and\_mail\_it
> 
> So I can’t set the locale in a controller like I’m used to.
> 
> I tried
> 
> init\_gettext ‘myapp’, ‘nl’
> 
> but that isn’t allowed in the model. Couldn’t find anything better  
> though.

Try  
bindtextdomain ‘myapp’  
in your model, and  
\<%= bindtextdomain ‘myapp’ -%\>  
in each of your views.

---

<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:** [November 30, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/10 "2006-11-30T16:33:20Z")

</div>

> Try  
> bindtextdomain ‘myapp’  
> in your model, and  
> \<%= bindtextdomain ‘myapp’ -%\>  
> in each of your views.

Thanks.

But how would I go about setting the language? There is no user session  
when the mail is getting sent, so I need to retrieve the language from  
the record itself and send the mail in the appropriate language.

---

<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:** [November 30, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/11 "2006-11-30T17:46:23Z")

</div>

Hi,

On Thu, 30 Nov 2006 16:19:39 +0100  
Michael [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having issues with ActionMailer too. The problem is that I’m calling  
> ActionMailer with a cron task, for example:  
> ./script/runner Somerecord.find\_stuff\_and\_mail\_it
> 
> So I can’t set the locale in a controller like I’m used to.
> 
> I tried
> 
> init\_gettext ‘myapp’, ‘nl’

init\_gettext can’t accept lang value as 2nd parameter.  
Call GetText.locale= before initialize gettext.

GetText.locale = “nl”  
init\_gettext ‘myapp’

BTW, I don’t know it works with cron.  
If it doesn’t work with init\_gettext, try bindtextdomain as  
Mihnea said.

GetText.locale = “nl”  
bindtextdomain ‘myapp’

Anyway, try 1.8.0 not 1.7.0.

---

<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:** [November 30, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/12 "2006-11-30T16:19:39Z")

</div>

I’m having issues with ActionMailer too. The problem is that I’m calling  
ActionMailer with a cron task, for example:  
./script/runner Somerecord.find\_stuff\_and\_mail\_it

So I can’t set the locale in a controller like I’m used to.

I tried

init\_gettext ‘myapp’, ‘nl’

but that isn’t allowed in the model. Couldn’t find anything better  
though.

---

<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:** [November 30, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/13 "2006-11-30T17:46:24Z")

</div>

Hi,

On Thu, 30 Nov 2006 16:27:27 +0100  
Mihnea C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem is (was) that the ‘\_’ function was doing nothing in  
> ActionMailer views.
> 
> This was happening in 1.7 until I called bindtextdomain. Don’t know  
> about 1.8.

So, why don’t you try 1.8 ?

---

<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:** [November 30, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/14 "2006-11-30T18:04:49Z")

</div>

Masao M. wrote:

> So, why don’t you try 1.8 ?

Hm. That’s un/fortunately how a pragmatic user thinks. I got it to work  
with 1.7, so until I schedule a wholesale update of the libraries that I  
use, I’ll just focus on other stuff that still doesn’t work.

---

<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:** [November 30, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/15 "2006-11-30T18:09:47Z")

</div>

Masao M. wrote:

> init\_gettext can’t accept lang value as 2nd parameter.  
> Call GetText.locale= before initialize gettext.
> 
> GetText.locale = “nl”  
> init\_gettext ‘myapp’
> 
> BTW, I don’t know it works with cron.  
> If it doesn’t work with init\_gettext, try bindtextdomain as  
> Mihnea said.
> 
> GetText.locale = “nl”  
> bindtextdomain ‘myapp’
> 
> Anyway, try 1.8.0 not 1.7.0.

I’m using 1.8.0.

Using init\_gettext generates an error message. The second suggestion  
does work, but only within the model itself, so far.

Here’s what I have in the model:

require ‘gettext/rails’  
class Mymailer \< ActionMailer::Base  
def send\_a\_mail(email, language)  
GetText.locale = language  
bindtextdomain ‘my app’  
@subject = \_(‘You have a winning combination!’)  
@recipients = email  
end

This succesfully translates the subject, but the body of the email (a  
view), doesn’t get translated. Can I use translatable strings in the  
actionmailer views or should I created seperate files (like  
mail\_nl.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:** [November 30, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/16 "2006-11-30T18:13:54Z")

</div>

A quick update: using seperate view files DOES work. (mail\_nl.rhtml,  
mail\_de.rhtml and so on)

It would still be nice if it worked with .po/.mo files though, because  
that would make it easier to translate the app to more languages.

---

<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 29, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/17 "2007-03-29T19:07:55Z")

</div>

This problem is still existant with Rails 1.2.3 and gettext 1.9.0.

Strings in ActionMailer don’t get translated with the \_() method, they  
don’t appear at all (not even the base language).

This is very annoying because it forces me to hardcode all text blurbs  
in language dependent files (that works as reported before) e.g.  
text\_en.rhtml, text\_de.rhtml, …

Is there maybe a way to fix it?

thank you in advance,  
best regards

---

<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 29, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/18 "2007-03-29T21:06:20Z")

</div>

There are plenty of issues if you try to do this with ActionMailer in  
places like LoginEngine, etc; however, if you create a mailer in your  
root app, it does it mighty fine with no hassles.

$RAILS\_ROOT\> script/generate mailer Mailer

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.

Other benefits include much easier sweeping of the tagged strings into  
your stock .po files.

* * *

Myk Oleary  
Web Services Developer  
Pure Networks  
removed\_email\_address@domain.invalid

---

<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:** [December 1, 2006, 5:12am UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/19 "2006-12-01T05:12:43Z")

</div>

Hi,

On Thu, 30 Nov 2006 18:13:54 +0100  
Michael [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A quick update: using seperate view files DOES work. (mail\_nl.rhtml,  
> mail\_de.rhtml and so on)
> 
> It would still be nice if it worked with .po/.mo files though, because  
> that would make it easier to translate the app to more languages.

Yes. It should be so.

How about this ?

1. Remove GetText.locale= and bindtextdomain anywhere on your script.
2. Add the lines into app/controller/applications.rb

class ApplicationController \< ActionController::Base  
init\_gettext “myapp”  
after\_init\_gettext { #HERE  
GetText.set\_locale\_all(“nl”) #HERE  
} #HERE  
end

---

<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, 3:33pm UTC](https://www.ruby-forum.com/t/gettext-doesnt-work-in-actionmailer/102253/20 "2007-03-30T15:33:21Z")

</div>

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.

e.h

mailer\_template.rthml  
\<%= \_(“string”) %\>

=\> nothing

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