Is there a way to localize role labels?

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.

Leave a Comment