How to translate wordpress widget name?

I have correctly created a custom widget, evreything is translating well with a correct .po file, except the title.

Here is my code :

Read More
$concert_widget_name = __('Tour Dates', 'concerts');
wp_register_sidebar_widget (
    'tourdates',                 // your unique widget id
    $concert_widget_name,         // widget name
    'tourdates_widget_display',  // callback function to display widget
    array(                       // options
        'description' => 'Displaying upcoming tour dates'
    )
);

Is there an error ? An other way to translate the widget name ?

Related posts

Leave a Reply

1 comment

  1. I usually register my widgets using the register_widget function. In the constructor of the widget class I place the following code:

    class TourDates extends WP_Widget
    {
        public function __construct()
        {
            $options = array('classname' => 'tour-dates', 'description' => __('Display upcoming tour dates'));
            parent::__construct('tour_dates', __('Tour Dates'), $options);
        }
    }
    

    You can also check out the Widgets API on the WordPress Codex site. Hopefully this helps you in creating your custom widget.

    Also what I usually do is merge my translations with the default ones loaded from WordPress, like so:

    function loadTextDomain() {
        $locale = get_locale();
        $languageDir = dirname(__FILE__) . '/languages';
    
        $domain = 'default';
        $mofile = $languageDir . '/theme.' . $locale . '.mo';
    
        global $l10n;
        $mo = new MO();
        if (!$mo->import_from_file($mofile)) {
            return false;
        }
    
        if (isset($l10n[$domain]) && !empty($l10n[$domain]->entries)) {
            $l10n[$domain]->merge_with($mo);
        } else {
            $l10n[$domain] = $mo;
        }
    }
    add_action('init', 'loadTextDomain');
    

    This code looks similar to the load_textdomain function from WordPress but it avoids all the filters that do exist in the original function, which helps in avoiding any WordPress hook from altering your $domain and $mofile variables.

    But I will leave that up to you. Maybe the load_textdomain() function from WordPress will work just as fine, but in case it doesn’t this function should do the trick.

    Now if your using the loadTextDomain() function I pasted above you can just place a languages folder in the same folder as your functions.php resides, in that new folder you could place theme.nl_NL.mo or theme.de_DE.mo files depending on the language your using. This should allow translation of your website and also the admin area.