Is there a way to localize role labels?

I’m using Justin Tadlock’s Members plugin to create new roles and WP Role on Registration to display them on the registration form.

I’ve got a multilang site (English and Spanish) and I’ve localized my custom taxonomies using qTranslate.

Read More

I can’t use qTranslate’s quicktags in the Role Label field on the Member’s plugin because it doesn’t save it properly (even with $new_role_label = strip_tags( $_POST['role-label'] ); commented out on role-new.php) and I can’t go into the database to make the changes there because then it breaks.

I’ve seen very scarce info about the WP function translate_user_role but I’m not sure it. There’s not much else to go on.

Related posts

Leave a Reply

1 comment

  1. I don’t know the function you mention either, but I would venture to guess that that applies to default roles only anyhow.

    Still, the link you posted does contain a pointer in the right direction. It speaks of “dummy gettext calls”, which is exactly what you have to do.

    In the following we will adjust the global $wp_roles object on init and hook a filter to the get_option( 'wp_user_roles' ) call.

    function wpse92529_translatable_role_names() {
        global $wp_roles;
    
        $roles = $wp_roles->roles;
        $role_names = $wp_roles->role_names;
    
        /* define the translatable roles here */
        $translated_roles = array(
            'superuser' => _x( 'Superuser', 'Role Names', 'your-text-domain' ),
            'custom-role' => _x( 'The custom role', 'Role Names', 'your-text-domain' )
        );
    
        foreach ( $roles as $role_slug => $role_info ) {
            if ( array_key_exists( $role_slug, $translated_roles ) ) {
                $roles[$role_slug]['name'] = $translated_roles[$role_slug];
                $role_names[$role_slug] = $translated_roles[$role_slug];
            }
        }
    
        $wp_roles->roles = $roles;
        $wp_roles->role_names = $role_names;
    
        return $roles;
    }
    
    add_action( 'init', 'wpse92529_translatable_role_names', 1 );
    add_filter( 'option_wp_user_roles', 'wpse92529_translatable_role_names', 11 );
    

    The values of the $translated_roles array will now appear in your theme’s / plugin’s .pot file.