使用自定义MU插件代码进行POEDIT

时间:2017-05-15 作者:Tom Auger

我在mu插件中有一个非常严格的目录结构,但我希望POEdit能够帮助我为这个结构中的特定代码生成翻译文件,同时忽略其他一些代码。

目前,POEdit很难找到有效代码。我已经购买了Pro版本,但我认为我的结构过于定制,无法从Pro的自动化功能中获益。我也试过使用空白的WordPress。pot文件位于https://github.com/fxbenard/Blank-WordPress-Pot 作为一个起点,但我仍然得到类似的结果。

以下是我的结构:

- mu-plugins
   - vendor [don\'t translate]
   - languages [.mo, .po and .pot files should go here]
   - client [translate some of these]
      - Custom_Class
         - Custom_Class.php [the file that is automatically loaded by _loader.php]
      - Custom_Class_2
         - Custom_Class_2.php [file that is automatically loaded by _loader.php]
         - includes
             - Included_File_1.php [files included by Custom_Class_2.php]
      - react_app [all these files should be EXCLUDED, especially node_modules/*]
   - _loader.php [contains the WordPress plugin headers, and includes all the other php files within the other folders]
_loader.php 具有以下特性:

define( \'MY_TEXTDOMAIN\', \'MY_TEXTDOMAIN\' );
function load_mu_plugin_textdomain(){
    load_plugin_textdomain( MY_TEXTDOMAIN, false, plugin_dit_path( __FILE__ ) );
}
add_action( \'plugins-loaded\', \'load_mu_plugin_textdomain\' );
注意:上面的代码中有一些拼写错误,我在下面的回答中提到了这些错误。把这个留在这里记录问题

我正在使用define() 专用于IDE代码完成。如您所见,字符串与常量相同,因此POEdit在查找以下内容时应该不会有问题__( \'my string\', MY_TEXTDOMAIN );

我意识到从POEdit的角度来看,有很多非标准的方法,但我觉得我应该能够手动定制我的。处理此问题的pot文件。这是我的。现有pot文件:

# WordPress Blank Pot
# Copyright (C) 2014 ...
# This file is distributed under the GNU General Public License v2 or later.
msgid ""
msgstr ""
"Project-Id-Version: MY_PROJECT\\n"
"POT-Creation-Date: 2012-10-05 10:50+0100\\n"
"PO-Revision-Date: \\n"
"Last-Translator: Your Name <[email protected]>\\n"
"Language-Team: Your Team <[email protected]>\\n"
"Report-Msgid-Bugs-To: Translator Name <[email protected]>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=n != 1;\\n"
"X-Textdomain-Support: yes"
"X-Generator: Poedit 1.6.4\\n"
"X-Poedit-SourceCharset: UTF-8\\n"
"X-Poedit-KeywordsList: __;_e;esc_html_e;esc_html_x:1,2c;esc_html__;esc_attr_e;esc_attr_x:1,2c;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2;\\n"
"X-Poedit-Basepath: ../client/\\n"
"X-Poedit-SearchPath-0: .\\n"
"X-Poedit-Language: English\\n"
"X-Poedit-Country: UNITED STATES\\n"
"X-Poedit-Bookmarks: \\n"

1 个回复
最合适的回答,由SO网友:Tom Auger 整理而成

我不能百分之百确定这是唯一一个阻止POEdit正确解析我的代码的问题(我不确定它是否真的关心WordPress方面的问题),但修复了这些(愚蠢的)错误并从重新运行POEdit。下面的pot文件似乎生成了正确的翻译字符串:

define( \'MY_TEXTDOMAIN\', \'MY_TEXTDOMAIN\' );
function load_mu_plugin_textdomain(){
    load_plugin_textdomain( MY_TEXTDOMAIN, false, plugin_dir_path( __FILE__ ) );
}
add_action( \'muplugins_loaded\', \'load_mu_plugin_textdomain\' );
更改自我的原始帖子:

muplugins_loaded 而不是plugins-loaded (无论如何,这不是一项行动:plugins_loaded 是正确的动作挂钩-请注意下划线而不是破折号)。

plugin_dir_path() 而不是打字错误:plugin_dit_path()

这是最新的。我加载的pot文件,然后为其生成新的翻译:

# WordPress Blank Pot
# Copyright (C) 2014 ...
# This file is distributed under the GNU General Public License v2 or later.
msgid ""
msgstr ""
"Project-Id-Version: MY_PROJECT\\n"
"POT-Creation-Date: 2012-10-05 10:50+0100\\n"
"PO-Revision-Date: \\n"
"Last-Translator: Your Name <[email protected]>\\n"
"Language-Team: Your Team <[email protected]>\\n"
"Report-Msgid-Bugs-To: Translator Name <[email protected]>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=n != 1;\\n"
"X-Textdomain-Support: yes"
"X-Generator: Poedit 1.6.4\\n"
"X-Poedit-SourceCharset: UTF-8\\n"
"X-Poedit-KeywordsList: __;_e;esc_html_e;esc_html_x:1,2c;esc_html__;esc_attr_e;esc_attr_x:1,2c;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2;\\n"
"X-Poedit-Basepath: ../client/\\n"
"X-Poedit-SearchPath-0: .\\n"
"X-Poedit-Language: English\\n"
"X-Poedit-Country: UNITED STATES\\n"
"X-Poedit-Bookmarks: \\n"

结束