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?

2 Answers
2

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?

Leave a Comment