(此示例是对 DEUTCH 的翻译。您可以将习惯更改为您想要的名称。)
在每个插件头中,都有一个唯一的名称。
(例如:
/*
Plugin Name: my-pluginname
.......
*/
然后,在该插件的文件夹中,创建一个文件夹“languages”;
然后,在您的插件 .php 文件中(在顶部的某处),插入初始化代码:
class load_language
{
public function __construct()
{
add_action('init', array($this, 'load_my_transl'));
}
public function load_my_transl()
{
load_plugin_textdomain('my-pluginname', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}
}
$zzzz = new load_language;
然后打开任何文本编辑器,然后像此代码一样插入(注意,我们只添加两个示例消息,“hello”和“bye”,因此,您可以使用类似的行添加任意数量的消息) .
# English translations for PACKAGE package.
# Copyright (C) 2012 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2012.
#
msgid ""
msgstr ""
"Project-Id-Version: my-pluginname 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-08-06 13:46-0400\n"
"PO-Revision-Date: 2013-03-21 11:20+0400\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"X-Poedit-SourceCharset: iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 1.5.4\n"
#: mypluginindex.php:87 (it is just a line of a note, to remind where our code appears)
msgid "mymessage1"
msgstr "Hello"
#: mypluginindex.php:88
msgid "mymessage2"
msgstr "Bye"
然后将此文件保存为“my-pluginname-en_US.po”(注意,.po 是文件的扩展名,因此请检查您的文本编辑器程序是否未保存到“my-pluginname-en_US.po.TXT” ")。
然后下载POEDIT软件,打开这个文件。然后编辑“翻译”字段,然后保存为“my-pluginname-de_DE”
将生成两个文件(如果 poEdit 没有自动生成第二个 .mo 文件,只需转到 File -> Preferences -> Editor 并选中“Automatically compile .mo file on save”复选框),
然后将这两个文件放入“语言”文件夹。
在此之后,打开 wp-config.php 并找到这段代码:
define ('WPLANG, '');
改成
define ('WPLANG, 'de_DE');
仅此而已。
当 wordperss 被加载时,它会读取你的插件语言文件,前缀为 -de_DE。
所以,在插件的 .php 文件中,而不是:
echo "Something string";
你应该使用:
echo __("mymessage1", 'my-pluginname');
完成的。现在您应该测试您的插件。
p.s.使用的链接:
https://codex.wordpress.org/I18n_for_WordPress_Developers
http://codex.wordpress.org/Translating_WordPress
https://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Installing_WordPress_in_Your_Language