Internationalize a Array of strings

1,474 views
Skip to first unread message

Andrés gutiérrez

unread,
Feb 22, 2010, 2:47:48 PM2/22/10
to rails...@googlegroups.com
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?

Iain Hecker

unread,
Feb 22, 2010, 3:24:06 PM2/22/10
to rails...@googlegroups.com
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 <andres...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "rails-i18n" group.
> To post to this group, send email to rails...@googlegroups.com.
> To unsubscribe from this group, send email to
> rails-i18n+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rails-i18n?hl=en.
>

Andrés gutiérrez

unread,
Feb 22, 2010, 5:38:07 PM2/22/10
to rails...@googlegroups.com
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 <ia...@iain.nl>



--
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
-----------------------------------------------------------------------------
"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.

Krzysztof Knapik

unread,
Feb 23, 2010, 2:35:24 AM2/23/10
to rails...@googlegroups.com
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 <andres...@gmail.com>:

Andrés gutiérrez

unread,
Feb 23, 2010, 3:19:41 AM2/23/10
to rails...@googlegroups.com
Thanks I'll try it :)

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

Andrés gutiérrez

unread,
Feb 23, 2010, 2:23:19 PM2/23/10
to rails...@googlegroups.com
it worked great !!! The translation key must be a string. Now all perfect.

Thanks ,

Andrés
Reply all
Reply to author
Forward
0 new messages