如何做好翻译WordPress插件的准备?

时间:2013-01-12 作者:Nabil Kadimi

创建可翻译插件的最佳方法是什么?

它不必从一开始就被翻译,但它必须易于翻译,以便来自不同文化的其他开发人员可以参与插件的本地化过程。

2 个回复
最合适的回答,由SO网友:Nabil Kadimi 整理而成

1. Write with localization in mind

Don\'t use echo or print() to produce text output, instead use the WordPress functions __() and _e():

/** Not localization friendly */
echo "Welcome to my plugin";    
// OR
print("Welcome to my plugin");

/** Localization friendly */
_e(\'Welcome to my plugin\', \'my-plugin\');
// OR
$my_text = __(\'Welcome to my plugin\', \'my-plugin\');
echo $my_text;

_e() and __() will provide the translation — in the current language — of the text provided as the first parameter. _e() will output the text whereas __() will return it.

The second parameter is the text domain, you will be using it to tell WordPress that the text provided as the first parameter belongs to this plugin, you can use any name you want but I prefer to use the same name as I used for the plugin file of directory, I find it more intuitive.

How to output dynamic text like: "Hello <username>"?

With __() and sprintf():

/** Get the username */
$username = \'Magictrick\';

/** Not localization friendly */
echo "Hello $username";     

/** Localization friendly */
printf(__(\'Hello %s\', \'my-plugin\'), $username);
// OR 
$my_text = sprintf(__(\'Hello %s\', \'my-plugin\'), $username);
echo $my_text;

2. Prepare the .pot/.po/.mo files

Definitions

  • The .pot file: is put at your disposal by the plugin developer and it\'s used as a starting point to create new translations, WordPress doesn\'t use it.
  • A .po file: is a translation file you or someone else started, and maybe completed, WordPress doesn\'t use it.
  • A.mo file: is automatically created by Poedit whenever you save a .po file, all you can do with these files is to upload or re-upload them whenever you create or update a .po file. WordPress gets translations from .mo files.

Open Poedit and create a new catalog (File › New Catallog...) with these settings:

  • Project info: Use your (or your team) information, the language and country should match your plugin default language
  • Paths:
    • Base path: .
    • Paths : remove all & add .., (we will store language file in a plugin subdirectory called languages)
  • Keywords : remove all & add __ and _e

Save the catalog as /my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin.pot and scan your plugin files for translatable text by pressing the update button. When the update is finished close that catalog, you won\'t need to update that file unless you add new translatable strings (i.e enclosed in __() or _e()) to your plugin.

Now let\'s create the first translation (I will use fr_FR):

Using Podeit, create a catalog from a POT file (File › New catalog from POT file...):

  • Project info: Use your (or your team) information, change the language and country, I will use French and France
  • Paths:Don\'t change
  • Keywords : Don\'t chage

Save the catalog as /my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin-fr_FR.po. Translate some or all the of the strings, save the .po file again, upload both the .po and .mo files.

Note that whenever you save a .po file a .mo file is generated with the same name, the filename of the .po file is crucial, it\'s composed of the concatenation of the plugin text domain (my-plugin) and the language locale (fr_FR), always name your .po files for plugins like this: [textdomain]-[locale].po, here are some examples:

  • Italian/Italy: wpcf7-it_IT.po
  • Portuguese/Brazil: wpcf7-pt_BR.po
  • Arabic: wpcf7-ar.po... Yes!

Whenever the plugin is updated with new text, update the po file, translate new strings and reupload the .po and .mo files

3. Instruct the plugin to load translated text for the current language

Somewhere in your plugin, you must tell WordPress to use your .mo file, you can do it by using this code in the beginning of your plugin file:

function my_plugin_init() {
  load_plugin_textdomain( \'my-plugin\', false, \'my-plugin/languages\' );
}
add_action(\'init\', \'my_plugin_init\');

Replace my-plugin with your plugin name in the 1st and 3rd parameter of the load_plugin_textdomain function.

4. Test and troubleshoot

Some reasons it may not work:

  • Strings are not imported into the .pot or .po file
    • → Wrong catalog settings (path or keywords or both)
  • Text is not translated on the WordPress site
    • → .mo file for that language missing or has a wrong file name
    • → Text domain not used (replace _e(\'my text\') with _e(\'my text\', \'my-plugin\'))
    • → Text domain not loaded (use example above with the right parameters, WP will not warn you about mistakes)
SO网友:thespacecamel

Nabil的回答相当完整,但提供了一个简单的变体:

你的插件在WordPress上。组织插件库

您愿意要求您的插件只能与WordPress 4.6或更高版本配合使用。

步骤如下:

插件自述中的。txt文件,添加Requires at least: 4.6. 看见https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/

如果还没有,请将插件上载到WordPress插件库。看见https://wordpress.org/plugins/developers/add/.

查找插件的slug/text域。要做到这一点,请转到WordPress插件库中的插件页面。URL如下所示https://wordpress.org/plugins/your-plugin-slug/. URL的最后一部分“你的插件slug”是你插件的slug。这就是翻译函数的文本域所使用的内容。

在插件中使用WordPress的翻译功能(如__e(‘hello’, ‘my-plugin-domain’);). 只需确保使用在上一步中获得的正确插件文本域。看见https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/ 了解更多信息。

如果您执行上述步骤,WordPress将负责:

通过插件解析所有可翻译字符串(无需安装和运行Poedit或任何东西)

  • 任何人都可以轻松地在translate上提供插件的翻译。wordpress。org(不需要你自己的网站专门翻译你的插件,也不需要有一个自定义流程让翻译人员向你提交翻译)
  • 当有人使用你的插件时,WordPress会负责检查它是否翻译成他们的语言,如果是这样,用他们的语言显示(他们或你无需将翻译文件加载到他们的网站上)
  • (我的博客帖子中的答案如下:https://cmljnelson.blog/2019/01/01/the-really-lazy-way-to-translate-a-wordpress-plugin/)

    结束

    相关推荐

    Plugin Localization

    我刚刚为wp构建了我的第一个插件,即使它不是一个伟大的“代码诗意”;)它正常工作。这是一个使用GalleryView 3.0 jquery插件转换默认wp库的插件(http://spaceforaname.com/galleryview).我唯一不能做的就是本地化。此插件的本地化意味着转换管理界面,在这里可以配置jquery插件选项来更改结果库的外观。我试着关注网络上数百万的教程,在论坛上阅读了很多关于这个问题的帖子,并遵循了codex的指南。。。但仍然没有运气。这就是我所做的:每个文本行都位于gette