0

I try to localize my plugin.

in the plugin php file my-plugin.php I have the textdomain

Text Domain: my-plugin

and the action on "plugins_loaded"

// load textdomain on plugin
function my-plugin_load_textdomain() {
  echo '<script>console.log("fired");</script>'; 
  if (load_plugin_textdomain( 'my-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' )) {
       echo '<script>console.log("ok");</script>';
  } else {
       echo '<script>console.log("not");</script>';
  }
}
add_action( 'plugins_loaded', 'my-plugin_load_textdomain' );

The plugin itselfs displays a shortcode and loads the my-plugin-output.php which has following echos

<h4><?php _e( 'Ausgabe', 'my-plugin' ); ?></h4>

I have in the folder languages the my-plugin-de.mo files which I translated via poedit to my-plugin-en.mo. The translation is complete. Following files are in the languages folder:

my-plugin-de.mo

my-plugin-de.pot

my-plugin-en.mo

my-plugin-en.po

I have read several tutorials, however many of them say I should add a define WP-LANG, but this is not needed in WordPress 4.0 and above?

My site language is now selected as english. The basic code in the plugin is german, but it should display the english language.

When I activate the plugin the console gives me the message that the load_plugin_textdomain returns false, on codex I see this

Returns false, if the language file could not be loaded (it is not readable or the mo file reader can not understand it).

What does this mean? I translated the files with poedit, and I can open it in poedit without problems. If I validate the en.po file in poedit, it says everything is ok. The language folder is also correct.

Am I missing here something? Thanks very much

EDIT:

The output from debug translations:

Debug Translations (Version 2013.09.05)

Locale: en_US


Domain:    default
File:      C:\xampp\htdocs\projekte\WP/wp-content/languages/en_US.mo (not found)
Called in: C:\xampp\htdocs\projekte\WP\wp-settings.php line 306 load_default_textdomain

Domain:    twentyfourteen
File:      C:\xampp\htdocs\projekte\WP/wp-content/themes/twentyfourteen/languages/en_US.mo (not found)
Called in: C:\xampp\htdocs\projekte\WP\wp-content\themes\twentyfourteen\functions.php line 68 load_theme_textdomain

Domain:    twentyfourteen
File:      C:\xampp\htdocs\projekte\WP/wp-content/languages/themes/twentyfourteen-en_US.mo (not found)
Called in: C:\xampp\htdocs\projekte\WP\wp-content\themes\twentyfourteen\functions.php line 68 load_theme_textdomain

My plugin is not listed at all? It is activated and does also output on the page? This is strange

4
  • 1
    Try Debug Translations.
    – fuxia
    Commented Jun 15, 2015 at 10:11
  • Thanks! I edited my question with the output from the debug translations. I do not understand why the the plugin translation is not listed
    – Owl
    Commented Jun 15, 2015 at 10:51
  • 1
    Hmm, function my-plugin_load_textdomain() is not a vlid function name. - is not allowed here.
    – fuxia
    Commented Jun 15, 2015 at 11:14
  • Thanks, no sorry it is my_plugin_load_textdomain, this was just a dummy name
    – Owl
    Commented Jun 15, 2015 at 11:19

1 Answer 1

2

The load_plugin_textdomain() function had not the same textdomain as given in the PHP file of the plugin. I had written dummy names for this thread, therefore it could not be seen.

But I changed it. Also the .mo and .po files have to have the localization code as set in the WordPress database at WP_LANG. Strangely, there was no localization code in the database, the field was empty. I added it and named the files new, and now it is working!

This article helped a lot: https://localise.biz/help/wordpress/loading-translations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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