插件本地化无效

时间:2018-05-18 12:21:44

标签: php wordpress gettext

我根据文档设置了所有内容,并使用loco translate编译了我的翻译。由于某些原因,我无法使翻译工作。它总是打印出英文。如何让翻译工作?我在stackoverflow上发布了这个问题,因为我不确定这是gettext,wordpress还是我的代码的问题。

<?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文件

#, 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文件

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版本:4.9.6
  • PHP版本:php-5.6.4

1 个答案:

答案 0 :(得分:0)

问题是,我使用了一个钩子来加载文本域。

通过更改:

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

到此:

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

我跑了。我不确定是否使用钩子对插件队列有任何负面影响。