Localize / Translate different values for one model field

570 Views Asked by At

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:

  • SuperAdmin
  • Admin
  • Teacher
  • Parent

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:

  • Teacher (en)
  • Professor (pt)

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.

1

There are 1 best solutions below

6
LHH On

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