# How to use gettext in plugins?

**URL:** <https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983>\
**Category:** Rails\
**Created:** [May 9, 2006, 7:36am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983 "2006-05-09T07:36:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 7:36am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/1 "2006-05-09T07:36:51Z")

</div>

I use rails-engine as a plugin of the application and I want to localize  
the validate message from the plugin:

validates\_length\_of :login, :within =\> 3…40, :on =\> :create, :message  
=\> N\_("%{fn} is too short (min is %d characters)")

It can not display the according language,where should i set it?  
I have tried to add  
require ‘gettext/rails’  
GetText.bindtextdomain(“myd2d”)  
to init\_engine.rb or somewhere else,it can not work still.

Thanks  
Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 9:07am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/2 "2006-05-09T09:07:59Z")

</div>

Hi charlie,

At first, did you fix your previous problem?

[http://www.ruby-forum.com/topic/64942#new](http://www.ruby-forum.com/topic/64942#new)

If not, fix the problem first.  
Because it may cause this problem.

Then,  
a similer problem that “validates\_lenth\_of doesn’t translate anything”  
are reported by other person today. So I’ll investigate it ASAP.

But if you have other informations about this.  
Report it more detail as you can, please.  
For example, validation in your model(not plugins) works,  
or every other messages are localized correctly, etc.

On Tue May 09 14:36:51 JST 2006

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 9:32am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/3 "2006-05-09T09:32:41Z")

</div>

Hi, the previous problem you mentioned has been fixed,I use cookie to  
set local and it can work,however if i directly set local using the api  
that i used before,it can not work.  
I am still working this problem,and now I am clear that it is the reason  
of validates\_lenth\_of 's not working well,but other validates can work  
in  
PLUGINS,eg:validates\_uniqueness\_of,validates\_confirmation\_of,validates\_presence\_of  
sometimes validates\_lenth\_of can translate,but sometimes not,i am still  
summarizing the rules.

unknown wrote:

> Hi charlie,
> 
> At first, did you fix your previous problem?
> 
> [Gettext not worked in firefox,but done well under IE - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/64942#new)
> 
> If not, fix the problem first.  
> Because it may cause this problem.
> 
> Then,  
> a similer problem that “validates\_lenth\_of doesn’t translate anything”  
> are reported by other person today. So I’ll investigate it ASAP.

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 9:49am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/4 "2006-05-09T09:49:15Z")

</div>

I have tested the validates\_lenth\_of translation both in activemodels  
and plugins,and i think it has some bugs:

validates\_length\_of :login, :minimum =\> 5, :on =\> :create, :message =\>  
N\_("%{fn} is too short (min is %d characters)")

if the string above include “%d”,then validates\_length\_of string will  
not be translated by gettext,or else it can be done.

Thanks  
Charlie

charlie wrote:

> Hi, the previous problem you mentioned has been fixed,I use cookie to  
> set local and it can work,however if i directly set local using the api  
> that i used before,it can not work.  
> I am still working this problem,and now I am clear that it is the reason  
> of validates\_lenth\_of 's not working well,but other validates can work  
> in  
> PLUGINS,eg:validates\_uniqueness\_of,validates\_confirmation\_of,validates\_presence\_of  
> sometimes validates\_lenth\_of can translate,but sometimes not,i am still  
> summarizing the rules.

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 10, 2006, 3:14am UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/5 "2006-05-10T03:14:22Z")

</div>

Masao M. wrote:

> Hi,  
> Thanks for reporting.  
> I fixed them. Could you try it and report the result?

Hi,I have tested ant it works well now,very grateful!

Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/how-to-use-gettext-in-plugins/59983/6 "2006-05-09T18:54:36Z")

</div>

Hi,

On Tue, 9 May 2006 09:49:15 +0200  
charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have tested the validates\_lenth\_of translation both in activemodels  
> and plugins,and i think it has some bugs:
> 
> validates\_length\_of :login, :minimum =\> 5, :on =\> :create, :message =\>  
> N\_(“%{fn} is too short (min is %d characters)”)
> 
> if the string above include “%d”,then validates\_length\_of string will  
> not be translated by gettext,or else it can be done.

Thanks for reporting.

I fixed them. Could you try it and report the result?

1. Get the newest rails.rb Revison 1.34 from:  
[http://rubyforge.org/cgi-bin/viewcvs.cgi/gettext/lib/gettext/rails.rb?root=gettext&view=log](http://rubyforge.org/cgi-bin/viewcvs.cgi/gettext/lib/gettext/rails.rb?root=gettext&view=log)

2. Replace this file to your rails.rb  
(e.g. /usr/lib/ruby/gems/1.8/gems/gettext-1.5.0/lib/gettext/rails.rb)

3. Try validates\_length\_of at models.

4. Then try validates\_length\_of at plugins.
