0

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.

lbramos
  • 327
  • 1
  • 6
  • 19

1 Answers1

0

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

LHH
  • 3,233
  • 1
  • 20
  • 27
  • That would work if I only have the "teacher" value. But "name" filed could be "superadmin", "admin", "teacher" or "parent". It can have these 4 different values... – lbramos Mar 13 '16 at 19:34
  • Based on your answer I was able to translate the roles. I then got a new issue: all validations (for example, current_user.has_role? :teacher) stop working because the app now looks for "professor" instead of "teacher". Is there a way to force the "locale" in rolify/cancancan validations? – lbramos Mar 14 '16 at 13:08
  • Looking into this a little bit deeper, the problem is that with Globalize, the field "name" in the "Roles" table remains nil. It adds automatically the role name to the roles_translations table, leaving the main "Roles" table with only the id. I just need to figure out how to force the main table to have also the names. – lbramos Mar 14 '16 at 15:36
  • i think better if you create one more field in Role table that contains actual role (not translated for rolify) and use display_name or something for translation. – LHH Mar 14 '16 at 16:31
  • I'm still looking for a better solution than crating a new field... there must be a way to write to the original table. I can force it with Navicat but that's not a solution... l'll wait a little bit more to see if any other solution appears! – lbramos Mar 16 '16 at 23:12
  • This is not very clean but it worked: Role.where(id: Role.first.id).update_all(name: 'superadmin') --> credits: https://github.com/globalize/globalize/issues/497 – lbramos Mar 17 '16 at 00:33