Can this be translated?

48 views
Skip to first unread message

Redd Vinylene

unread,
Jan 1, 2009, 7:38:28 AM1/1/09
to rails-i18n
I got this in my person.rb:

def him_or_her
h = "him" if gender == "male"
h = "her" if gender == "female"
h
end

Is it possible to translate "him" and "her"?

--
http://www.home.no/reddvinylene

Mickael Faivre-Macon

unread,
Jan 1, 2009, 8:39:36 AM1/1/09
to rails...@googlegroups.com
def him_or_her
if gender == "male"
h = I18n.t(:him)
else
h = I18n.t(:her)
end
h
end

Mickael Faivre-Maçon

Iain Hecker

unread,
Jan 1, 2009, 8:44:02 AM1/1/09
to rails...@googlegroups.com
Hi Redd,

yes

Iain

PS: Just add I18n.t(h) at the last line

Redd Vinylene

unread,
Jan 1, 2009, 9:56:24 AM1/1/09
to rails...@googlegroups.com
Thank y'all so much!

I got the following in applications_helper.rb so I can say t instead of I18n.t:

def t(*args)
translate(*args)
end

How do I make this work for my models as well? Would an "extend
ApplicationsHelper" to person.rb be a wise choice? I've heard some
people don't really like that.

Cheers!
--
http://www.home.no/reddvinylene

Iain Hecker

unread,
Jan 1, 2009, 12:44:59 PM1/1/09
to rails...@googlegroups.com
Maybe using the application helper is not a good idea, because it has
methods specific to views, but you can make your own module and mix it
in.

module TranslateForModels
def t(*args); I18n.t(*args); end
end
ActiveRecord::Base.send :include, TranslateForModels

The proper place would be in the lib directory.

Iain

Redd Vinylene

unread,
Jan 2, 2009, 4:37:52 AM1/2/09
to rails...@googlegroups.com
Once again, thank you so much!

Works perfectly!
--
http://www.home.no/reddvinylene

Redd Vinylene

unread,
Jan 2, 2009, 4:41:40 AM1/2/09
to rails...@googlegroups.com
Actually, no it doesn't. I still have to do I18n.t instead of t in
person.rb, despite TranslateForModels in lib/i18n.rb.
--
http://www.home.no/reddvinylene

Karel Minarik

unread,
Jan 2, 2009, 4:08:39 PM1/2/09
to rails-i18n
Beware of Rails autoloading conventions. Your file should have a
`translate_for_models.rb` name,

Karel

On Jan 2, 10:41 am, "Redd Vinylene" <reddvinyl...@gmail.com> wrote:
> Actually, no it doesn't. I still have to do I18n.t instead of t in
> person.rb, despite TranslateForModels in lib/i18n.rb.
>
>
>
> On Fri, Jan 2, 2009 at 10:37 AM, Redd Vinylene <reddvinyl...@gmail.com> wrote:
> > Once again, thank you so much!
>
> > Works perfectly!
>
> > On Thu, Jan 1, 2009 at 6:44 PM, Iain Hecker <i...@iain.nl> wrote:
>
> >> Maybe using the application helper is not a good idea, because it has
> >> methods specific to views, but you can make your own module and mix it
> >> in.
>
> >> module TranslateForModels
> >>  def t(*args); I18n.t(*args); end
> >> end
> >> ActiveRecord::Base.send :include, TranslateForModels
>
> >> The proper place would be in the lib directory.
>
> >> Iain
>
> >> On Thu, Jan 1, 2009 at 15:56, Redd Vinylene <reddvinyl...@gmail.com> wrote:
>
> >>> Thank y'all so much!
>
> >>> I got the following in applications_helper.rb so I can say t instead of I18n.t:
>
> >>> def t(*args)
> >>>  translate(*args)
> >>> end
>
> >>> How do I make this work for my models as well? Would an "extend
> >>> ApplicationsHelper" to person.rb be a wise choice? I've heard some
> >>> people don't really like that.
>
> >>> Cheers!
>
> >>> On Thu, Jan 1, 2009 at 2:44 PM, Iain Hecker <i...@iain.nl> wrote:
>
> >>>> Hi Redd,
>
> >>>> yes
>
> >>>> Iain
>
> >>>> PS: Just add I18n.t(h) at the last line
>

Yaroslav Markin

unread,
Jan 3, 2009, 5:52:44 AM1/3/09
to rails...@googlegroups.com
Hey,

just added a patch for t/l aliases inclusion in AR models, +1 if you like:

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1689-add-i18n-shortcuts-to-ar-models

--
Yaroslav Markin

Andreas Neuhaus

unread,
Jan 3, 2009, 10:47:01 AM1/3/09
to rails-i18n
Yes it's possible, but if you plan to have more translations in
future, you might want to prevent to translate single words.
Localization is more than just translating words to a different
language. In another language, it might be not as easy as switching
"him" and "her". In the long term, you usually play better by
translating whole sentences (where their meanings can be translated to
another language).

I wrote about it in my blog a few weeks ago.
http://zargony.com/2008/12/01/how-to-use-i18n-string-interpolation

--
Andreas Neuhaus
http://zargony.com/

Redd Vinylene

unread,
Jan 3, 2009, 1:23:08 PM1/3/09
to rails...@googlegroups.com
Thank you for your reply Andreas. Did you by any chance catch Iain's
earlier replies though? He pretty much nailed it, except I'm unable to
use just t as oppose to I18n.t, that's all. Much obliged!
--
http://www.home.no/reddvinylene
Reply all
Reply to author
Forward
0 new messages