Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Plugin translation - load_plugin_textdomain

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?

like image 243
jekey123 Avatar asked Aug 05 '17 18:08

jekey123


People also ask

How do I translate a plugin?

Let's say you want to translate a plugin called my-plugin. A properly built plugin would have two translated file in the directory wp-content/languages/plugins/ called my-plugin-fr_FR.po and my-plugin-fr_FR.mo. If your plugin has this structure, the steps below will override the translations, if not you can try and see anyway:

How do I load a translation override in WP core?

Load your override file first. This is important to load it first, because already defined translations will be skipped when you'll load another language file for this text domain (see WP core ). Load the original translation file, which will in fact load all the untranslated strings of the override file. This works only with compiled mo file.

How to merge two translations from different text domains?

Load a .mo file into the text domain $domain. If the text domain already exists, the translations will be merged. If both sets have the same string, the translation from the original value will be taken. On success, the .mo file will be placed in the $l10n global by $domain and will be a MO object. (string) (Required) Text domain.


1 Answers

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.

like image 65
jekey123 Avatar answered Nov 15 '22 07:11

jekey123