6

I'm working on a custom Wordpress plugin but I cannot get it multi-lang ready.

It does load the .mo file of the main language properly, but when switching languages (using WPML), it always shows the translation of the main language (in this case German). So when I am on English, it still shows the German translations.

Here's my code:

in the header:

/*
Plugin Name: MM Jobs
Plugin URI: http://example.com/
Description: Custom Jobs Plugin to create new Jobs
Version: 1.3.84
Author: Jekey
Author URI: http://example.com/
Text Domain: mm-jobs
Domain Path: /languages
*/

then:

function mm_jobs_plugins_loaded() {
        load_plugin_textdomain( 'mm-jobs', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    }
    add_action( 'plugins_loaded', 'mm_jobs_plugins_loaded', 0 );

.mo files are correct, as it already loads the German translation. Named: mm-jobs-en_US.mo or mm-jobs-de_DE.mo under /plugins/mm-jobs/languages/

You have any idea what's causing the problem?

jekey123
  • 91
  • 1
  • 6

2 Answers2

3

In case someone having the same problem. I had

get_plugin_data( __FILE__ );

in my code. This caused to run a wp_core function where it loads the textdomain, so my en_US.mo was overriden by de_DE.mo

I don't know why get_plugin_data() took the wrong lang-file. It seems to have picked the right one for different plugins that use the function.

jekey123
  • 91
  • 1
  • 6
  • Official doc: https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#loading-text-domain – Motahar Hossain Dec 23 '19 at 22:39
1

Use the init action hook.

Loading the plugin translations should not be done during plugins_loaded action since that is too early and prevent other language related plugins from correctly hooking up with load_textdomain() function and doing whatever they want to do. Calling load_plugin_textdomain() should be delayed until init action.

add_action( 'init', 'wpdocs_load_textdomain' );

/**
 * Load plugin textdomain.  
 */
function wpdocs_load_textdomain() {
    load_plugin_textdomain( 'wpdocs_textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

Reference: https://developer.wordpress.org/reference/functions/load_plugin_textdomain/#div-comment-1568

Thiago Santos
  • 333
  • 3
  • 6