I developed a small plugin that adds (or to be more precise: first deletes then adds again) some custom user roles. Each of these roles is set up in a way like this:
function wpdev_141551_add_role_someone() {
$role = 'someone';
remove_role( $role );
$capabilities = array(
'read' => TRUE,
'publish_posts' => TRUE,
'edit_posts' => TRUE,
'delete_posts' => FALSE,
// ...
);
add_role( $role, 'Someone', $capabilities );
}
Now the problem is, these custom user roles don't get translated at all.
The translations of the default user roles (Author, Contributor etc.) are included in WordPress's default text domain. So how/where should I step in and translate these custom role names?
If I add the role with a gettext call for its name __( 'Someone', 'plugin-text-domain' )
then the string that gets written to the database will be translated according to the current language. What I would like to have, however, is a solution that lets me switch languages as I want and have the roles be displayed accordingly—without having to de- and then reactivate the plugin (i.e., delete and add the role, again).
I will post my current solution to this, but I'm still very interested in any other (possibly more efficient) approach.
I developed a small plugin that adds (or to be more precise: first deletes then adds again) some custom user roles. Each of these roles is set up in a way like this:
function wpdev_141551_add_role_someone() {
$role = 'someone';
remove_role( $role );
$capabilities = array(
'read' => TRUE,
'publish_posts' => TRUE,
'edit_posts' => TRUE,
'delete_posts' => FALSE,
// ...
);
add_role( $role, 'Someone', $capabilities );
}
Now the problem is, these custom user roles don't get translated at all.
The translations of the default user roles (Author, Contributor etc.) are included in WordPress's default text domain. So how/where should I step in and translate these custom role names?
If I add the role with a gettext call for its name __( 'Someone', 'plugin-text-domain' )
then the string that gets written to the database will be translated according to the current language. What I would like to have, however, is a solution that lets me switch languages as I want and have the roles be displayed accordingly—without having to de- and then reactivate the plugin (i.e., delete and add the role, again).
I will post my current solution to this, but I'm still very interested in any other (possibly more efficient) approach.
Share Improve this question edited Apr 25, 2014 at 16:23 tfrommen asked Apr 17, 2014 at 11:47 tfrommentfrommen 9,2317 gold badges40 silver badges59 bronze badges 3 |3 Answers
Reset to default 3The translate_user_role
function is just a wrapper for translate_with_gettext_context
, defining the context 'User role'.
In this last function, there is the filter hook gettext_with_context
, which provides the context as well as the currently used domain.
So we could do this:
function wpdev_141551_translate_user_roles( $translations, $text, $context, $domain ) {
$plugin_domain = 'plugin-text-domain';
$roles = array(
'Someone',
'Nobody',
// ...
);
if (
$context === 'User role'
&& in_array( $text, $roles )
&& $domain !== $plugin_domain
) {
return translate_with_gettext_context( $text, $context, $plugin_domain );
}
return $translations;
}
add_filter( 'gettext_with_context', 'wpdev_141551_translate_user_roles', 10, 4 );
In order to make this work, we have to do a dummy gettext call, like so:
_x( 'Someone', 'User role', 'plugin-text-domain' );
We could just put this right after add_role
.
This works, but it doesn't seem to be an efficient approach as every gettext call with a context has to pass our function(s).
You should use :
translate_user_role( __('some','textdomains ') );
example :
add_role(
'wdcp_developer_role',
translate_user_role( __('Developer',$this->textdomains ) ),
array( 'read' => true, 'level_12' => true )
);
https://developer.wordpress/reference/functions/remove_role/ https://developer.wordpress/reference/functions/translate_user_role/
I was facing a similiar problem, I had set some custom roles using the User Role editor, but in my plugin I needed to translate the default wordpress frontend role selector in user_new.php and user_edit.php pages.
after tweaking @tfrommen answer I've finally found my solution doing as follows:
function ni_languages_extra_strings(){
//set some translation strings to be set in loco translate plugin or similar
$extra_strings = array(
__('Vendedor', 'plugin-text-domain'),
__('Empresa', 'plugin-text-domain')
);
}
function ni_translate_user_roles( $translations, $text, $context, $domain ) {
$plugin_domain = 'plugin-text-domain';
$roles = array(
'Vendedor',
'Empresa',
);
if (
$context === 'User role'
&& in_array( $text, $roles )
&& $domain !== $plugin_domain
) {
return translate( $text, $plugin_domain );
}
return $translations;
}
add_filter( 'gettext_with_context', 'ni_translate_user_roles', 10, 4 );
add_role( $role, __('Someone', 'text-domain'), $capabilities );
but then you deleted it... so what's wrong there? – gmazzap Commented Apr 17, 2014 at 19:50