• Hi,
    I am writing a plugin to create custom user roles and to remove wordpress default user roles except the admin one.

    Everything is ok, but custom roles are not translated in the wordpress back-office (in user profil for example, or in settings=>general=>default role).

    My plugin code is translation ready: i have created a .pot file and .po and .mo myplugin-fr_FR local. Translation files are ok, because i can see translated description and plugin name in admin plugin page.

    /*
    Plugin Name: My Website User Roles
    Description: Custom user roles and capabilities for my website.
    Version:     1.0
    Author:      me
    License:     GPL2
    License URI: https://www.gnu.org/licenses/gpl-2.0.html
    Text Domain: my_website_user_roles
    Domain Path: /languages
    */
    class My_Website_User_Roles {
    
        public function __construct() {
            // on activation
            register_activation_hook( __FILE__, array( 'My_Website_User_Roles', 'activate' ) );
    
            /** other stuff like deactivation, etc... */
    
            // translation
            add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
        }
    
        public function load_textdomain() {
            load_plugin_textdomain( 'my_website_user_roles', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
        }
    
        public static function activate() {
            // remove wp default roles except admin
    	remove_role( 'editor' );
    	remove_role( 'author' );
    	remove_role( 'contributor' );
    	remove_role( 'subscriber' );
    
    	add_role(
    		'basic_contributor',
    		__( 'Occasional Contributor', 'my_website_user_roles' ),
    		array(
    			'read'         => true,
    			'edit_posts'   => true,
    			'delete_posts' => true,
    		)
    	);
    
    	add_role(
    		'expert_contributor',
    		__( 'Expert Contributor', 'my_website_user_roles' ),
    		array(
    			'read'          => true,
    			'edit_posts'    => true,
    			'delete_posts'  => true,
    			'publish_posts' => false,
    			'upload_files'  => true,
    			'edit_others_posts' => true,
    			'edit_published_posts' => true,
    		)
    	);
    
    	add_role(
    		'moderator',
    		__( 'Moderator', 'my_website_user_roles' ),
    		array(
    			'read'                   => true,
    			'edit_posts'             => true,
    			'delete_posts'           => true,
    			'publish_posts'          => true,
    			'edit_published_posts'   => true,
    			'delete_published_posts' => true,
    			'moderate_comments'      => true,
    		)
    	);
        }
    
    }

    On my languages directory, i have created my_website_user_roles-fr_FR.po and my_website_user_roles-fr_FR.mo files that should translate Occasional Contributor to ‘contributeur occasionnel’, Expert Contributor to ‘contributeur expert’ and Moderator to ‘modérateur’.
    But in the wordpress admin back office, custom user roles are in english but not in french.
    Localisation files are loaded because they also translate the plugin description and i can see the description in french on the plugins page.

    How can i see the custom user roles in a local language on the back office ? what am i doing wrong ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • David Massiani

    (@david-massiani)

    Hi,

    have you found a solution?
    I’m interesting because I have the same problem …

    Thank you

    Thread Starter hedii

    (@hedii)

    hi david,
    unfortunately no…
    I had to use a plugin like user role editor, and i think the translation worked with it (website shipped to customer, but i don’t remember)

    David Massiani

    (@david-massiani)

    User Role Editor don’t use translation…
    Ok thanks for your reply.

    Same problem here.

    The problem is that the translate_user_role() function is fixed to the default WordPress translations so the only way to create your own translations is with a hook and some dunny translation strings.

    Here is a sollution: http://wordpress.stackexchange.com/questions/141551/how-to-auto-translate-custom-user-roles

    Barely any plugins use a fix for this.
    For example, even WooCommerce translates it’s roles but nowhere in WordPress is this visible because the translations can’t be found using translate_user_role() by default.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom user roles translation not working’ is closed to new replies.