Forum: Rails I18n Internationalize a Array of strings

Posted by Andrés Gutiérrez (andresgutgon)
on 2010-02-22 20:49
(Received via mailing list)
Hi, i have a model User. Inside this model i have an Array:
  JOB_CATEGORIES = ['Undergraduate student', 'Graduate student' ,
'Pre-doctoral student', 'Postdoctoral student','Asociate profesor',
'Assistant profesor', 'Research profesor', 'Technician']

Then i use this array in a view in a Drop down select In the user's edit
form.

How can i internalionalize this array?
Posted by Iain Hecker (Guest)
on 2010-02-22 21:25
(Received via mailing list)
Hi Andrés,

How about:

en:
  job_categories:
    undergraduate: Undergraduate student
    graduate: Graduate student
    predoctoral: Pre-doctoral student
    postdoctoral: Postdoctoral student
    associate_prof: Asociate professor
    assistent_prof: Assistant professor
    research_prof: Research professor
    technician: Technician


And in your model:

class Job < ActiveRecord::Base

  def translated_job_category
    I18n.t(job_category, :scope => :job_categories)
  end

end

In your helper:

module JobsHelper
  def job_categories
    I18n.t(:job_categories).map { |key, value| [ value, key ] }
  end
end

And in your view:

<%= f.select :job_category, job_categories %>


The helper method creates an array that the select-helper understands.
It saves shortened keys ("undergraduate", "graduate", etc) in the
database, as some sort of enumerable. Be sure to keep those the same
in all languages.

When you want to show which job category an object has, you can do
@job.translated_job_category to get the translated value back.

I hope this helps,

Iain


2010/2/22 Andrés gutiérrez <andresgutgon@gmail.com>:
Posted by Andrés Gutiérrez (andresgutgon)
on 2010-02-22 23:40
(Received via mailing list)
Worked almost all!!!

The drop down don't show the option that the user save SELECTED in the 
drop
down. This is my view:

<%= profile.select :job_category, job_categories, {:prompt =>
I18n.t("prompt_job_categories")} %>

Thanks

2010/2/22 Iain Hecker <iain@iain.nl>

>    associate_prof: Asociate professor
>    I18n.t(job_category, :scope => :job_categories)
> end
>
> >   JOB_CATEGORIES = ['Undergraduate student', 'Graduate student' ,
> > "rails-i18n" group.
> "rails-i18n" group.
> To post to this group, send email to rails-i18n@googlegroups.com.
> To unsubscribe from this group, send email to
> rails-i18n+unsubscribe@googlegroups.com<rails-i18n%2Bunsubscribe@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/rails-i18n?hl=en.
>
>


--
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
Posted by Krzysztof Knapik (Guest)
on 2010-02-23 08:36
(Received via mailing list)
Probably because traslation key is a symbol, when user attr is a string, 
so:

 def job_categories
   I18n.t(:job_categories).map { |key, value| [ value, key.to_s ] }
 end

Regards,
KK

2010/2/22 Andrés gutiérrez <andresgutgon@gmail.com>:
Posted by Andrés Gutiérrez (andresgutgon)
on 2010-02-23 09:21
(Received via mailing list)
Thanks I'll try it :)

2010/2/23 Krzysztof Knapik <krzysztof.knapik@gmail.com>

> 2010/2/22 Andrés gutiérrez <andresgutgon@gmail.com>:
> >
> >>    predoctoral: Pre-doctoral student
> >>
> >>    I18n.t(:job_categories).map { |key, value| [ value, key ] }
> >> database, as some sort of enumerable. Be sure to keep those the same
> >> 2010/2/22 Andrés gutiérrez <andresgutgon@gmail.com>:
> >> >
> >> >
> >> http://groups.google.com/group/rails-i18n?hl=en.
> > Edward V. Berard, ingeniero informático.
> >
>
>


--
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
Posted by Andrés Gutiérrez (andresgutgon)
on 2010-02-23 20:25
(Received via mailing list)
it worked great !!! The translation key must be a string. Now all 
perfect.

Thanks ,

Andrés

El 23 de febrero de 2010 09:19, Andrés gutiérrez
<andresgutgon@gmail.com>escribió:

>>
>> > <%= profile.select :job_category, job_categories, {:prompt =>
>> >> en:
>> >>
>> >> In your helper:
>> >>
>> >>
>> edit
>> >> > rails-i18n+unsubscribe@googlegroups.com<rails-i18n%2Bunsubscribe@googlegroups.com>
>> >> To unsubscribe from this group, send email to
>> >
>> > To unsubscribe from this group, send email to
>> To unsubscribe from this group, send email to
>
> -----------------------------------------------------------------------------
> "Caminar sobre el agua y desarrollar software a partir de unas
> especificaciones es fácil. si ambas están congeladas."
> Edward V. Berard, ingeniero informático.
>



--
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.