如何翻译任何语言的 WordPress 插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12638547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to translate a WordPress plugin in any language?
提问by Frank
I have completed my plugin, now want to provide a multilingual features for my users. I goggled about it, but it's hard to implement.
我已经完成了我的插件,现在想为我的用户提供多语言功能。我对它进行了研究,但很难实施。
I've seen WordPress translationbut need basic steps to follow and translate my plugin.
我看过WordPress 翻译,但需要遵循和翻译我的插件的基本步骤。
I have done these
我做了这些
- downloaded POEdit.
- created 'french.po' file in plugin dir
- complied 'french.po' -> 'french.mo'
- 下载 POEdit。
- 在插件目录中创建了“french.po”文件
- 遵守'french.po' -> 'french.mo'
Need to do
需要做
- How to define msgid& msgstrin po file?
- How to load po/mo file in plugin?
- How to replace labels/text through po/mo file?
- how to use
__e()
&___()
to replace 'msgstr' in plugin pages?
- 如何在 po 文件中定义msgid和msgstr?
- 如何在插件中加载 po/mo 文件?
- 如何通过 po/mo 文件替换标签/文本?
- 如何使用
__e()
&___()
替换插件页面中的“msgstr”?
回答by Teno
With a plugin called, Codestyling Localization, you don't need to use POEdit.
使用名为Codestyling Localization的插件,您无需使用 POEdit。
I'll show you an example of using 'localizationsample' as the text domain. In this case, the language files are in the /lang/ directory. They don't need to be those names in your actual plugin; they are just examples.
我将向您展示一个使用“localizationsample”作为文本域的示例。在这种情况下,语言文件位于 /lang/ 目录中。它们不需要是您实际插件中的那些名称;它们只是示例。
Steps
脚步
Add these lines in the plugin comment header to be recognized by Codestyling Localization.
Text Domain: localizationsample
Domain Path: /langCreate a directory named
lang
in your plugin directory.Install and activate the Codestyling Localizationplugin.
Go to
Tools
->Localization
Find your plugin and click on
Add New Language
Select the language (country) to localize in the radio button and press
Create po-file
At this point make sure a .po file is created in the lang folder.Press
Rescan
->scan now
This is recommended since in my system without doing this, the plugin always shows an error saying "not all items are using the same text domain."Press
Edit
This will bring you another page listing the messages available to be translated. Those messages are the ones passed to the functions__()
and_e()
in the plugin code.Click on
Edit
in the table next toCopy
then you'll get a dialog box to type your translation for each message. Finish translating.Press
generate mo-file
At this point, you should see a .mo file being created in thelang
folder.Change your locale specified in
wp-config.php
to reflect the translation. The default isdefine('WPLANG', '');
在插件注释标题中添加这些行,以便代码样式本地化识别。
文本域:localizationsample
域路径:/langlang
在您的插件目录中创建一个目录。安装并激活Codestyleling Localization插件。
去
Tools
->Localization
找到你的插件并点击
Add New Language
在单选按钮中选择要本地化的语言(国家/地区)并按下
Create po-file
此时确保在 lang 文件夹中创建了一个 .po 文件。按
Rescan
->scan now
这是推荐的,因为在我的系统中不这样做,插件总是显示一个错误,说“并非所有项目都使用相同的文本域”。按下
Edit
这将带您进入另一个页面,列出可供翻译的消息。这些消息是传递给函数__()
和_e()
插件代码的消息。单击
Edit
旁边的表中的 ,Copy
然后您将看到一个对话框,为每条消息键入您的翻译。完成翻译。出版社
generate mo-file
在这一点上,你会看到在正在创建的.mo文件lang
夹。更改您在
wp-config.php
中指定的区域设置以反映翻译。默认是define('WPLANG', '');
Sample plugin
示例插件
/*
Plugin Name: Sample Localization
Description: Demonstrates how to localize your plugin.
Text Domain: localizationsample
Domain Path: /lang
*/
// Localization
add_action('init', 'localizationsample_init');
function localizationsample_init() {
$path = dirname(plugin_basename( __FILE__ )) . '/lang/';
$loaded = load_plugin_textdomain( 'localizationsample', false, $path);
if ($_GET['page'] == basename(__FILE__) && !$loaded) {
echo '<div class="error">Sample Localization: ' . __('Could not load the localization file: ' . $path, 'localizationsample') . '</div>';
return;
}
}
// Add Admin Menu
add_action('admin_menu','localizationsample_menu');
function localizationsample_menu() {
add_options_page(
'Localization Demo', // admin page title
'Localization Demo', // menu item name
'manage_options', // access privilege
basename(__FILE__), // page slug for the option page
'localization_demo_adminpanel' // call-back function name
);
}
function localization_demo_adminpanel() {
echo '<div class="wrap"><div id="icon-themes" class="icon32"></div>';
echo '<h2>' . __('Hi there!', 'localizationsample') . '</h2>';
echo '<p>';
_e('Hello world!', 'localizationsample');
echo '</p>';
echo '</div>'; // end of wrap
}
回答by T.Todua
(THIS EXAMPLE IS A TRANSLATION to DEUTCH. YOU CAN CHANGE the customs to YOUR DESIRED names.)
(这个例子是对 DEUTCH 的翻译。您可以将习俗更改为您想要的名字。)
in every plugins head, there is an unique name. (for example:
在每个插件头中,都有一个唯一的名称。(例如:
/*
Plugin Name: my-pluginname
.......
*/
then, in that plugin's folder, create a folder "languages";
然后,在该插件的文件夹中,创建一个文件夹“languages”;
then, into your plugin .php file (somewhere in the top), insert the initialization code:
然后,在你的插件 .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;
then open any text editor, then insert like this code (NOTE, THAT we are only adding two sample messages, "hello" and "bye", so , you can ADD AS MANY messages AS YOU WANT with the similar lines).
然后打开任何文本编辑器,然后像这样插入代码(注意,我们只添加了两个示例消息,“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"
then save this file as "my-pluginname-en_US.po" (note, that .po is an extension of file, so check that your text editor program has not saved to "my-pluginname-en_US.po.TXT").
然后将此文件保存为“my-pluginname-en_US.po”(注意,.po 是文件的扩展名,因此请检查您的文本编辑器程序是否未保存到“my-pluginname-en_US.po.TXT”)。
then download POEDIT software, and open this file. then edit the "translation" field, and then save as "my-pluginname-de_DE" there will be generated two files ( If poEdit does not generate the second .mo file automatically, just go to File -> Preferences -> Editor and check the box that says "Automatically compile .mo file on save"),
然后下载POEDIT软件,打开这个文件。然后编辑“translation”字段,然后另存为“my-pluginname-de_DE”,会生成两个文件(如果poEdit没有自动生成第二个.mo文件,只需要进入File -> Preferences -> Editor 并检查显示“在保存时自动编译 .mo 文件”的框),
then put those two file into "languages" folder.
然后将这两个文件放入“languages”文件夹中。
after this, open wp-config.php and find this code:
之后,打开 wp-config.php 并找到以下代码:
define ('WPLANG, '');
and change to
并更改为
define ('WPLANG, 'de_DE');
thats all. when wordperss is loaded, it will read your plugins language file, with prefix -de_DE.
就这样。加载 wordperss 时,它将读取您的插件语言文件,前缀为 -de_DE。
so, in the plugin's .php file, instead of:
所以,在插件的 .php 文件中,而不是:
echo "Something string";
you should use:
你应该使用:
echo __("mymessage1", 'my-pluginname');
Finished. Now you should test your plugin.
p.s.used links:
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
完成的。现在你应该测试你的插件。
psused链接: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