Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Make sure role labels are translated where the plugin outputs them. F…
Browse files Browse the repository at this point in the history
…ixes: #157

Note that fixing this ticket brought up another issue, which is that core WP outputs the role in some places.  This is out of scope for this commit, so a second ticket was created: #158
  • Loading branch information
justintadlock committed Sep 27, 2017
1 parent 8917cf3 commit 573dd36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion admin/class-meta-box-content-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function meta_box( $post ) {
<li>
<label>
<input type="checkbox" name="members_access_role[]" <?php checked( is_array( $roles ) && in_array( $role, $roles ) ); ?> value="<?php echo esc_attr( $role ); ?>" />
<?php echo esc_html( translate_user_role( $name ) ); ?>
<?php echo esc_html( members_translate_role( $role ) ); ?>
</label>
</li>
<?php endforeach; ?>
Expand Down
20 changes: 18 additions & 2 deletions inc/functions-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function members_register_default_roles( $wp_roles ) {
foreach ( $wp_roles->roles as $name => $object ) {

$args = array(
'label' => $object['name'],
'label' => members_translate_role_hook( $object['name'], $name ),
'caps' => $object['capabilities']
);

Expand Down Expand Up @@ -247,7 +247,23 @@ function members_sanitize_role( $role ) {
function members_translate_role( $role ) {
global $wp_roles;

return apply_filters( 'members_translate_role', translate_user_role( $wp_roles->role_names[ $role ] ), $role );
return members_translate_role_hook( $wp_roles->role_names[ $role ], $role );
}

/**
* Hook for translating user roles. I needed to separate this from the primary
* `members_translate_role()` function in case `$wp_roles` was not yet available
* but both the role and role label were.
*
* @since 2.0.1
* @access public
* @param string $label
* @param string $role
* @return string
*/
function members_translate_role_hook( $label, $role ) {

return apply_filters( 'members_translate_role', translate_user_role( $label ), $role );
}

/**
Expand Down

1 comment on commit 573dd36

@mvirenius
Copy link

@mvirenius mvirenius commented on 573dd36 Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I tried this fix, but it is not working, because localized role labels are not available when members_register_default_roles is called. I guess that localization files are loaded during some later hook.

So I think that only option is translate role labels when they are printed.

EDIT: WordPress roles are registered in the line 147 in the wp-settings.php file:https://github.com/WordPress/WordPress/blob/5f7a5c1246e3e6a76c5746e27d3b51a5ed51bfce/wp-settings.php#L147

Default textdomain is loaded later, on line https://github.com/WordPress/WordPress/blob/5f7a5c1246e3e6a76c5746e27d3b51a5ed51bfce/wp-settings.php#L396

so translated role labels can't be available when members_register_default_roles is called.

Please sign in to comment.