如何翻译任何语言的WordPress插件? [英] How to translate a WordPress plugin in any language?

查看:123
本文介绍了如何翻译任何语言的WordPress插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了插件,现在想为我的用户提供多语言功能.我对此深思,但是很难实现.

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.

我已经完成了

  1. 下载了POEdit.
  2. 在插件目录中创建了"french.po"文件
  3. 完成了'french.po'->'french.mo'

需要做

  1. 如何定义 msgid & msgstr 在po文件中?
  2. 如何在插件中加载po/mo文件?
  3. 如何通过po/mo文件替换标签/文本?
  4. 如何使用 __e() & ___() 替换插件页面中的"msgstr"?
  1. How to define msgid & msgstr in po file?
  2. How to load po/mo file in plugin?
  3. How to replace labels/text through po/mo file?
  4. 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.

步骤

  1. 在插件注释标题中添加以下行,以供代码样式化本地识别.

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆