1

I'm trying to load a custom language file for a plugins to avoid any changes for this file when updating the plugin. I tried several snippets in the functions.php of the theme, but none of them worked.

add_filter('load_textdomain_mofile', 'custom_load_textdomain_mofile', 10, 2);
function custom_load_textdomain_mofile( $mofile, $domain){
    if ($domain == 'my-textdomain')
        $mofile = 'path to mo. file';
    return $mofile;
}

I tried it with relative and absolute paths and with WP_LANG_DIR. Nothing worked.

Same here:

add_action('load_textdomain', 'load_custom_language_files_for_my_plugin', 10, 2);

function load_custom_language_files_for_my_plugin($domain, $mofile)
{
    if ('all-in-one-event-calendar' === $domain && plugin_dir_path($mofile) === WP_PLUGIN_DIR.'/all-in-one-event-calendar/languages/')
    {
        load_textdomain('all-in-one-event-calendar', WP_LANG_DIR.'/'.$domain.'-'.get_locale().'.mo');
    }
}

Both snipptes seem to be pretty ok. However, the MO file is not loaded.

Any help would be appreciated!

1

If you will use the translation only in back end, then load the translation file on the hook admin_init. If not, also on front end, then use the hook init.

A example:

class Fb_Example_Class {

    /**
     * Constructor, init the functions inside WP
     *
     * @return  \Fb_Example_Class 
     */
    private function __construct() {

        // load translation files
        add_action( 'admin_init', array( $this, 'localize_plugin' ) );
    }

    /**
     * Localize_plugin function.
     *
     * @uses    load_plugin_textdomain, plugin_basename
     * @access  public
     * @return  void
     */
    public function localize_plugin() {

        load_plugin_textdomain( 'textdomain', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
    }

}

You find also helpful content and examples on the codex page.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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