0

I'm building a localized widget and the translation works everywhere but in the language string in the constructor. I have read that

the whole plugin initialisation has to be bound to the init hook

to avoid that problem, but i don't know to do this because if i try to register the widget with init instead of widgets_init, there is nothing initialised at all.

How to fix that?

My Code:

  class Pinboard_Linkroll_Widget extends WP_Widget {

    // Constants & Properties go here...
    protected $widget_slug = 'pinboard-linkroll-widget';

    /*--------------------------------------------------*/
    /* Constructor
    /*--------------------------------------------------*/

    /**
    * Includes pinboard API class, 
    * Specifies the classname and description, instantiates the widget,
    * loads localization files, and includes necessary stylesheets and JavaScript.
    */
    public function __construct() {

      // include pinboard api class
      // https://github.com/kijin/pinboard-api
      include_once( dirname(__FILE__). '/lib/pinboard-api.php' );


      // load plugin text domain
      add_action( 'init', array( &$this, 'widget_textdomain' ) );

      // actual widget constructor
      parent::__construct(
        $this->get_widget_slug(),
        __( 'Pinboard Linkroll Widget', $this->get_widget_slug() ),
        array(
          'classname'  => $this->get_widget_slug().'-class',
          'description' => __( 'Returns a list of recent Pinboard Links.', $this->get_widget_slug() )
        )
      );

      // handle token errors in admin area (check for valid token and display errors)
      add_action( 'sidebar_admin_setup', array( &$this, 'handle_token_errors' ) );

      // delete transients when last widget instance is deleted
      // http://wordpress.stackexchange.com/questions/95091/how-to-delete-cached-transients-from-a-widget-instance-properly
      add_action( 'sidebar_admin_setup', array( &$this, 'delete_transient' ) );

    } // end constructor


    /**
     * Returns widget slug
     *
     * @since   0.7.0
     * @return  Plugin slug variable.
     */
    public function get_widget_slug() {
      return $this->widget_slug;
    }


    /**
     * Loads the Widget's text domain for localization and translation.
     *
     * @since   0.7.0
     */
    public function widget_textdomain() {
      load_plugin_textdomain( $this->get_widget_slug(), false, $this->get_widget_slug() . '/lang' );
    } // end widget_textdomain

    // WP Widget API functions & other stuff goes here...

  }

  // register widget
  add_action( 'widgets_init', create_function('', 'return register_widget("Pinboard_Linkroll_Widget");') );

}

1 Answer 1

1

You have slight timing issues because you generate title first (in constructor call), but postpone text domain load to later (init at default priority). Text domain mus be loaded for __() to translate correctly. Those are not “lazy”, they generate and return strings right where they are called.

Text domain should be loaded very early, Codex recommends at plugins_loaded for plugins.

Probably your class shouldn't be trying to be both valid widget and plugin loader, separate it out.

1
  • i guess i'll have to dive a little bit deeper into plugin architecture, thanks for pushing me in that direction :) Commented Nov 16, 2014 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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