0

I'm creating a widget but the title and description is not translatable.

I'm often face this problem with arrays, I use esc_html__() inside the array which is supposed to return the translated string to the $key but for some reason it doesn't work, so I find myself using the esc_html__() outside the array, however how can I do that here:

function __construct() {
    $op = array(
        'name' => esc_html__( 'some title', 'my-plugin' ),
        'description' => esc_html__( 'some description goes here', 'my-plugin' )
    );
    parent::__construct( 'pk_members_widget', '', $op );
}

Most importantly, why it doesn't work when you add it in the array? Thanks.

  • 1
    esc_html_e() and _e() wouldn't work but there's no reason what you've got there shouldn't work. At least in terms of those functions. You've got the name in the wrong place though. The widgets name goes in the 2nd argument of the constructor, not the options array. – Jacob Peattie Dec 2 '17 at 11:46
2

I created simple widget to find out why you have such a problem. Everything is working fine. Below I give you my code as an example.

/**
 * Register widget
 */
function wpse_287570_register_widget()
{
    register_widget('WPSE_287570_Widget');
}

add_action('widgets_init', 'wpse_287570_register_widget');

/**
 * Create widget
 */
class WPSE_287570_Widget extends WP_Widget {

    public function __construct()
    {
         $op = array(
            'name' => esc_html__( 'some title', 'wpse_287570' ),
            'description' => esc_html__( 'some description goes here', 'wpse_287570' )
        );

        parent::__construct( 'wpse_287570_widget', '', $op );
    }
}


/**
 * Load the plugin translations
 */
function wpse_287570_load_textdomain() {

    load_plugin_textdomain( 'wpse_287570', false, basename( dirname( __FILE__ ) ) . '/languages' );
}

add_action( 'plugins_loaded', 'wpse_287570_load_textdomain' );

I keep my translations in languages directory in the main directory of my plugin. My translations files are named from textdomain and locale: wpse_287570-pl_PL.po and wpse_287570-pl_PL.mo.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.