如何翻译任何语言的WordPress插件? [英] How to translate a WordPress plugin in any language?
问题描述
我已经完成了插件,现在想为我的用户提供多语言功能.我对此深思,但是很难实现.
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.
我看过 WordPress翻译,但是需要遵循一些基本步骤来翻译我的插件.
I've seen WordPress translation but need basic steps to follow and translate my plugin.
我已经完成了
- 下载了POEdit.
- 在插件目录中创建了"french.po"文件
- 完成了'french.po'->'french.mo'
需要做
- 如何定义 msgid & msgstr 在po文件中?
- 如何在插件中加载po/mo文件?
- 如何通过po/mo文件替换标签/文本?
- 如何使用
__e()
&___()
替换插件页面中的"msgstr"?
- How to define msgid & msgstr in 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?
推荐答案
使用名为代码样式本地化,您无需使用POEdit.
With a plugin called, Codestyling Localization, you don't need to use POEdit.
我将向您展示一个使用"localizationsample"作为文本域的示例.在这种情况下,语言文件位于/lang/目录中.它们不需要是您实际插件中的那些名称.它们只是示例.
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.
步骤
-
在插件注释标题中添加以下行,以供代码样式化本地识别.
Add these lines in the plugin comment header to be recognized by Codestyling Localization.
文本域:localizationsample
域路径:/lang
Text Domain: localizationsample
Domain Path: /lang
在插件目录中创建一个名为lang
的目录.
Create a directory named lang
in your plugin directory.
安装并激活代码样式本地化插件.
转到Tools
-> Localization
找到您的插件,然后单击Add New Language
Find your plugin and click on Add New Language
在单选按钮中选择要本地化的语言(国家/地区),然后按Create po-file
.这时,请确保在lang文件夹中创建了一个.po文件.
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.
按Rescan
-> scan now
,这是推荐操作,因为在我的系统中,未执行此操作的插件始终显示错误消息,提示并非所有项目都使用相同的文本域".
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."
按Edit
键,这将为您带来另一个页面,其中列出了可以翻译的消息.这些消息就是在插件代码中传递给函数__()
和_e()
的消息.
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.
单击Copy
旁的表中的Edit
,然后将出现一个对话框,为每条消息键入翻译.完成翻译.
Click on Edit
in the table next to Copy
then you'll get a dialog box to type your translation for each message. Finish translating.
按generate mo-file
此时,您应该看到在lang
文件夹中创建的.mo文件.
Press generate mo-file
At this point, you should see a .mo file being created in the lang
folder.
更改在wp-config.php
中指定的语言环境以反映翻译.默认值为define('WPLANG', '');
Change your locale specified in wp-config.php
to reflect the translation. The default is define('WPLANG', '');
示例插件
/*
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
}
这篇关于如何翻译任何语言的WordPress插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!