• I’m struggeling with this for two days, and Google, nor my “WordPress Plugin Development” handbook can help me. I’m really out of options.

    Summarized, I’m building a (FAQ) plugin and I have trouble loading the text domain.
    If I load it the ‘traditional’ functional way, it works fine.
    If I put it in the constructor of a class, it fails.
    The code below is trimmed to loading the text domain only, but the same constructor also creates a custom post type and a custom taxonomy, and that works just fine. So the problem in the constructor is restricted to loading the text domain only.

    For your perception: This is the first time that I build a plugin the OO way. So it is also quite possible that I have an error in my code. However I haven’t found it yet.

    This works fine (functional method):

    /** Load text domain */
    add_action('plugins_loaded', 'wdfaq_load_textdomain');
    
    function wdfaq_load_textdomain() {
    load_plugin_textdomain( 'wd-faq', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
    }

    This doesn’t load the text domain (OO method):

    class wd_faq {	
    
    	/** Constructor */
    	public function __construct() {
    		add_action('plugins_loaded', array($this,'wdfaq_load_textdomain'), 0 ); // Load text domain
    	}
    
    	/** Load text domain */
    	public function wdfaq_load_textdomain() {
    		load_plugin_textdomain( 'wd-faq', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
    	}
    }
    $faq = new wd_faq();

    Neither does this (OO method simplified):

    class wd_faq {	
    
    	/** Constructor */
    	public function __construct() {
    		load_plugin_textdomain( 'wd-faq', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
    	}
    }
    $faq = new wd_faq();

    I have checked the file names and they are OK:
    wd-faq-en_US.mo
    wd-faq-nl_NL.mo

    (Btw, if they were not OK, the functional method would fail too of course)

Viewing 1 replies (of 1 total)
  • I don’t think the two OO versions are the same, even though they look similar. The add_action changes the behavior or activity during execution. See reference here. It also looks like you are changing the default priority of 10, to 0. I would suggest changing this to the default of 10, or omitting.

Viewing 1 replies (of 1 total)

The topic ‘Text domain not loading in constructor’ is closed to new replies.