Internationalize a Array of strings

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?

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 [email protected]:

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 H. [email protected]

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 [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
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.

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 [email protected]:

Thanks I’ll try it :slight_smile:

2010/2/23 Krzysztof K. [email protected]

2010/2/22 Andrés gutiérrez [email protected]:

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 [email protected]:

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.

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
[email protected]escribió:

<%= profile.select :job_category, job_categories, {:prompt =>

en:

In your helper:

edit

[email protected][email protected]
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.