Search code examples
phpwordpressgettext

Plugin Localization not working


I've set up everything according to the docs and compiled my translations using loco translate. For some Reason, I can't get the translations to work. It always prints out english. How can I get the translations to work? I posted this question on stackoverflow because I'm not sure whether this is an issue with gettext, wordpress or my code.

<?php
/**
 * Plugin Name: myplugin
 * Description: myplugindescription
 * Author: Tom McLean
 * Version: 1.0.0
 * Text Domain: myplugin-domain
 * Domain Path: /languages/
 */

function loadMyPluginTextDomain() {
    load_plugin_textdomain(
        'myplugin-domain',
        false,
        dirname(plugin_basename( __FILE__ )) . '/languages/'
    );
}
add_action( 'plugins_loaded', 'loadMyPluginTextDomain');
_e('Learn More', 'myplugin-domain');

.pot file

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: myplugin\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-05-18 12:06+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: \n"
"Language: \n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Loco https://localise.biz/"

msgid "Learn more"
msgstr ""

.po file

msgid ""
msgstr ""
"Project-Id-Version: myplugin\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-05-18 12:06+0000\n"
"PO-Revision-Date: 2018-05-18 12:12+0000\n"
"Last-Translator: Tom <tomm1996@gmail.com>\n"
"Language-Team: Deutsch\n"
"Language: de_DE\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Loco https://localise.biz/"
msgctxt "Link text"
msgid "Learn more"
msgstr "Erfahren Sie mehr."

WP Config

define('WPLANG', 'de_DE');
  • Wordpress Version: 4.9.6
  • PHP Version: php-5.6.4

Solution

  • The problem was, that I used a hook to load the text-domain.

    By changing this:

    <?php
    add_action( 'plugins_loaded', 'loadMyPluginTextDomain');
    _e('Learn More', 'myplugin-domain');
    

    to this:

    <?php
    loadMyPluginTextDomain();
     _e('Learn More', 'myplugin-domain');
    

    I got it running. I'm just not sure whether not using the hooks has any negative effects on the plugin queue.