如何在构造函数中翻译Widget描述?

时间:2014-11-15 作者:Hans Spieß

我正在构建一个本地化的小部件the translation works everywhere but in the language string in the constructor. 我have read 那个

整个插件初始化必须绑定到init挂钩

为了避免这个问题,但我不知道该怎么做,因为如果我尝试用init而不是widgets\\u init注册小部件,则根本没有初始化。

如何解决这个问题?

我的代码:

  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 个回复
最合适的回答,由SO网友:Rarst 整理而成

您有轻微的时间问题,因为您首先生成标题(在构造函数调用中),但将文本域加载推迟到以后(init 默认优先级)。要加载的文本域mus__() 正确翻译。它们不是“懒惰的”,而是在调用的地方生成并返回字符串。

文本域应尽早加载,Codex recommends at plugins_loaded 对于插件。

也许你的类不应该试图同时成为有效的小部件和插件加载器,把它分开。

结束

相关推荐

定制器:何时使用Customize_PREVIEW_INIT与Customize_Controls_EnQueue_Scrips

codex和Twenty14主题代码之间似乎存在一些冲突,关于哪个操作要挂钩以加载您的实时预览。js(例如:customizer.js)在2144主题中,实时预览处理程序customizer。js,使用customize_preview_init:function twentyfourteen_customize_preview_js() { wp_enqueue_script( \'twentyfourteen_customizer\', get_template_directory_u