Custom role translation

Hi,

Is it possible to translate our custom role name?
For instance ‘Client’ in this snippet:

return [
  'name'    => 'Client',
  'default' => false,
  'panel'   => false
];

No, not at the moment. Because the name is basically only displayed in the Panel when editing users/creating new users, we didn’t implement multi-language names for role names.

What is your use case for translating roles names? Do you really need different role names per panel user language or rather let the user decide how to call that role?

I work on a SaaS project in which I have defined some custom roles in the panel.
So these custom roles are part of the panel and if a customer is for instance German, he will probably choose to switch his panel in German. If his panel is in German but the role select menu displays the roles in English, it’s not very professional.

I see the problem. An idea would be to set an array of role names in your config:

c::set('clientrole', array(
  'de' => 'Klient',
  'en' => 'Client'
));

and use this in your role definition like this:

return [
  'name'    => c::get('clientrole')[site()->users()->findBy('role', 'admin')->language()],
  'default' => false,
  'panel'   => false
];

Note that the value saved in the user account file is the filename of the role file, e.g. editor.php gets saved as ‘role: editor’ no matter what name you use in the role file.

Thanks Sonja for this solution and the warning.

Maybe for Kirby 2.5, it could be nice to have something like:

return [
  'label' => 'Client' (translatable)
  'name'    => 'client', (used as an id in our code)
  'default' => false,
  'panel'   => false
];

And as an afterthought, probably use a fallback in case the key of the admin language does not exist in your array.

  'name'        => c::get('clientrole')[site()->users()->findBy('role', 'admin')->language()]? c::get('clientrole')[site()->users()->findBy('role', 'admin')->language()]: 'Client',

Edit: I just realized that the role value saved to the user account file is the name of the role file, so you will have to use that for checking user roles.

Edit 2: Please feel free to add a feature request issue on GitHub.