lbramos
lbramos

Reputation: 327

Localize / Translate different values for one model field

I use to have roles as an Enum and translate them was easy...

I've adopted rolify and now things got more complicated...

Rolify adds a table "Roles" to the RoR app, where you have, for example, the field "name" of the role.

So I have 4 roles:

What I would like to do is to translate these four roles into different languages. I've looked at solutions like the gem "globalize" but it only seems to allow to translate one field value, so for example I could say that:

But I can't seem to figure out how to translate more than one value for the same field.

Any idea on how I can do this?

EDIT Just a little clarification. Roles are stored in a "name" field, and as I have 4 roles, "name" can have 4 different value (Superadmin, admin, teacher, parent). My problem it's to translate different values for the same field.

Upvotes: 0

Views: 574

Answers (1)

LHH
LHH

Reputation: 3323

As per globalize gem

First save all English(en) values

I18n.locale = :en
Role.create(name: 'superadmin')
Role.create(name: 'admin')
Role.create(name: 'teacher')

and so on......

Allows you to translate the attributes per locale:

Role.find_each do |role|
  I18n.locale = :pt  ##set another locale
  ##find role using id and save accordingly.
  role.update_attributes(name: 'Professor') ## it will create role with translated name in roles_translation table.
  and so on......

 ##set more locale and save values accordingly.
end

for more check here https://github.com/globalize/globalize#model-translations

Upvotes: 0

Related Questions