Support » Plugins » Hacks » How to use override_load_textdomain ?

  • With the improvements to I18N that were implemented in 4.6, there’s a minority of cases where a plugin author or user still prefers to load the “bundled” translations from the plugin folder, rather than the Polyglot translations that are automatically loaded.

    An example would be a “premium” plugin that uses the same text domain as the free version hosted on, but includes more translation strings.

    The quick answer provided by @swissspidy:

    Create a function that hooks into override_load_textdomain and manually loads translations from another location

    Unfortunately, there seems to be no clear, working example that shows how to use the override_load_textdomain filter. The point of this entry is to provide such a working example 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • My example from the other thread works like a charm:

    add_action( 'init', function () {
    	load_plugin_textdomain( 'my-textdomain', false, plugin_dir_path( __FILE__ ) . 'languages' );
    } );
    add_filter( 'override_load_textdomain', function ( $override, $domain ) {
    	if ( 'my-textdomain' === $domain ) {
    		remove_filter( 'override_load_textdomain', __FUNCTION__ );
    		load_textdomain( $domain, 'path/to/my/' );
    		return true;
    	return $override;
    }, 10, 2 );

    I found a workaround, not using override_load_textdomain, but using load_textdomain_mofile instead. The source is this code sample by @mikejolley.

    Here’s a slightly modified version:

    add_filter( 'load_textdomain_mofile', 'load_custom_plugin_translation_file', 10, 2 );
    function load_custom_plugin_translation_file( $mofile, $domain ) {
      $my_textdomain = 'my-plugin-textdomain';
      if ( $my_textdomain === $domain ) {
        $mofile = WP_PLUGIN_DIR .'/'.$my_textdomain.'/languages/'.$my_textdomain.'-' . get_locale() . '.mo';
      return $mofile;

    You are expected to change my-plugin-textdomain to your actual text domain, and the po/mo files are expected to be inside a “languages” folder inside the plugin folder.

    Still, I’m looking forward to see a solution that uses override_load_textdomain, and I’m wondering what are the differences between the two methods.

    Moderator bcworkz


    For how each action works, examine the load_textdomain() source code.

    You can then see that if a “override_load_textdomain” hook returns TRUE, the normal translation file loading is aborted and “load_textdomain_mofile” would never fire. In Pascal’s code, he is using the “override_load_textdomain” filter as a convenient time to do what he wants to do while at the same time preventing the default code from executing.

    In theory the same end result should happen by using the “load_textdomain_mofile” filter, but as you’ve probably seen in the other thread, it’s failing to work as expected for some reason.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use override_load_textdomain ?’ is closed to new replies.