Can’t manage to make translate_user_role() work

I’ve been looking into wpse archives to solve my problem but I can’t manage to do such a thing. I built this code :

$args = array('fields' => 'all_with_meta');
$users = get_users($args);
foreach ($users as $user) {
       echo esc_html( translate_user_role( ucfirst ( $user->roles[0] ) ) );
}

I made a print_r()to see what I get. Everything is fine, I mean I grab user role but it does not translate by itself. Do I need to do more?

Related posts

2 comments

  1. I think your function nesting is slightly muddled; call ucfirst after translating, like so:

    esc_html( ucfirst( translate_user_role( $user->roles[0] ) ) );
    

    My bad, completely skipped a beat there. You should instead be using:

    translate_user_role( $GLOBALS['wp_roles']->role_names[ $user->roles[0] ] );
    

    It’s unreliable to assume that all role display names are simply ucfirst( $role key ). If that still does not work, are you sure the language files you’re using have fully translated WordPress?

  2. Please note that translate_user_role doesn’t work in the front-end currently.

    Here is a workaround, you can place this in your theme:

    add_action( 'init', 'load_admin_textdomain_in_front' )
    function load_admin_textdomain_in_front() {
        if ( ! is_admin() ) {
            load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' );
        }
    }
    

Comments are closed.