最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - How to auto-translate custom user roles?

programmeradmin3浏览0评论

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
  • I've seen that before editing question you used a translated role label, i.e. you used 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
  • You're right, I had the gettext call there but that is not what this question is about. I would like the user roles to be translatable on-the-fly, meaning: If I switch to English the roles are displayed in English, and if I choose German I get the according German translations. All that without having to de- and then reactivate the plugin. I hope that makes sense now. – tfrommen Commented Apr 17, 2014 at 20:20
  • 1 Yep, makes perfectly sense. And I don't think you can do more that what you've posted... on-the-fly translation are always a pain in WordPress. (Some ideas comes into my mind, but nothing better than your code) – gmazzap Commented Apr 17, 2014 at 20:42
Add a comment  | 

3 Answers 3

Reset to default 3

The 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 );
发布评论

评论列表(0)

  1. 暂无评论