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");') );
}