WordPress-powered sites are visited by millions of people all over the world. For many of them, English is not their native language, which makes using these sites harder. Fortunately this issue can easily be solved by offering users a translation.
由WordPress驱动的网站被全世界数百万人访问。 对于许多人来说,英语不是他们的母语,这使得使用这些网站更加困难。 幸运的是,可以通过为用户提供翻译来轻松解决此问题。
WordPress itself is translated to many languages and people can use it in their preferred language. But this is not the case with themes, plugins, and other front-end customizations. Every theme or plugin developer is responsible for adding translation support for his work. And although adding translation to a theme or plugin is not required and requires some extra work, in the long run it can be a big advantage. Having your theme or plugin translated is a win-win situation in which both sides benefit. You benefit by having a larger audience of people using your theme or plugin, and users benefit by using the theme or plugin in their language.
WordPress本身已翻译成多种语言,人们可以以其首选语言使用它。 但是主题,插件和其他前端自定义情况并非如此。 每个主题或插件开发人员都有责任为其工作添加翻译支持。 并且尽管不需要向主题或插件添加翻译,并且需要一些额外的工作,但从长远来看,这可能是一个很大的优势。 翻译主题或插件是双赢的局面,双方都将从中受益。 您可以通过吸引更多使用主题或插件的用户而受益,而用户可以通过使用其语言使用主题或插件而受益。
The purpose of this article is to show you how to translate properly any theme or plugin by using internationalization and localization methods. In brief, internationalization (i18n) deals with making sure strings of text are wrapped in specific function calls. It is the practice of making text ready for localization, while the localization (L10n) itself is the process of translating text for a specific locale. WordPress handles the localization process by checking for specific translation files and then performing the translation. It certainly sounds easily, so let’s check it out.
本文的目的是向您展示如何使用国际化和本地化方法正确翻译任何主题或插件。 简而言之,国际化(i18n)用于确保将文本字符串包装在特定的函数调用中。 这是使文本准备好进行本地化的一种做法,而本地化(L10n)本身是针对特定语言环境翻译文本的过程。 WordPress通过检查特定的翻译文件然后执行翻译来处理本地化过程。 它听起来确实很容易,所以让我们检查一下。
入门 (Getting Started)
The first thing you need to do is to decide where to store translations. Many themes and plugins put translation files in their root directory. Although translations will work when using this method, it can get a bit messy, especially if you have translation files for more than one language. For the most organized system, it’s better to create a subdirectory in your theme or plugin folder called languages. When you release your theme or plugin, you can store your default translation files in this directory, and when translators send you translation files you simply drop those files in that directory.
您需要做的第一件事是确定翻译的存储位置。 许多主题和插件将翻译文件放在其根目录中。 尽管使用这种方法可以进行翻译,但可能会有些混乱,尤其是当您拥有多种语言的翻译文件时。 对于组织最多的系统,最好在主题或插件文件夹中创建一个名为language的子目录。 释放主题或插件时,可以将默认翻译文件存储在此目录中,而当翻译向您发送翻译文件时,您只需将这些文件拖放到该目录中即可。
The translation process consists of three steps. The first step in making your theme or plugin translatable is to use a special function which tells WordPress to load a translation file if it exists for the user’s language. For theme translations, you can put this function call in your theme’s functions.php file:
翻译过程包括三个步骤。 使主题或插件可翻译的第一步是使用一个特殊功能,该功能告诉WordPress加载翻译文件(如果存在的话是针对用户语言的)。 对于主题翻译,您可以将此函数调用放入主题的functions.php文件中:
<?php
$domain = "myplugin";
$path = "myplugin/languages/";
load_theme_textdomain($domain, $path);
$domain
is a unique string that identifies text in your theme that has been prepared for translation. For organizational purposes, you should give this the same value as the name of your theme directory. $path
is the path to your translation files within the theme.
$domain
是一个唯一字符串,用于标识主题中准备进行翻译的文本。 出于组织目的,应将此值赋予与主题目录名称相同的值。 $path
是主题中翻译文件的路径。
Here is another example, this one sampled from the functions.php
file of WordPress’ default twentyeleven theme:
这是另一个示例,该示例是从WordPress默认二十一主题的functions.php
文件中采样的:
<?php
load_theme_textdomain( 'twentyeleven', TEMPLATEPATH.'/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH."/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
Here, “twentyeleven” is the domain which matches the theme directory’s name. You can use whatever you want, but this is considered best practice. In the second parameter, the TEMPLATEPATH
constant is used in order to get the theme’s root directory and then concatenate to it the “/languages” string to give the correct path to your files. The remaining code tells WordPress to get the translation file from the languages folder.
在此,“ twentyeleven”是与主题目录名称匹配的域。 您可以使用任何想要的方法,但这被认为是最佳实践。 在第二个参数中,使用TEMPLATEPATH
常量来获取主题的根目录,然后将“ / languages”字符串连接到该主题,以提供文件的正确路径。 其余代码告诉WordPress从languages文件夹中获取翻译文件。
In case of a plugin, the function used is similar but takes three parameters:
在使用插件的情况下,所使用的功能类似,但具有三个参数:
<?php
load_plugin_textdomain($domain, $abs_rel_path, $plugin_rel_path);
$abs_rel_path
is actually a deprecated parameter that should no longer be used, so just set this to false or null. $plugin_rel_path
is the relative path to your translation file. You should put this in your plugin file, not functions.php
:
$abs_rel_path
实际上是一个不建议使用的参数,不应再使用,因此只需将其设置为false或null。 $plugin_rel_path
是翻译文件的相对路径。 您应该将其放在您的插件文件中,而不是functions.php
:
<?php
add_action("init", "pluginname_init");
function pluginname_init() {
load_plugin_textdomain("myplugin", false, "myplugin/languages/");
}
使用本地化功能 (Using the Localization Functions)
Next you’ll need to define which text strings will be translated. This is done by wrapping your textual content with functions that identifies the type of localization you want. A lot of localization functions are available, but in fact there are only two which you’ll use most of the time.
接下来,您需要定义要翻译的文本字符串。 这是通过使用标识所需的本地化类型的函数包装文本内容来完成的。 许多本地化功能都可用,但是实际上大多数时候只有两个。
The first function makes your text ready for translation and returns it for use in PHP. Here are two examples:
第一个功能使您的文本准备好翻译,并将其返回以供PHP使用。 这是两个示例:
<?php
$text = "Hello World!";
$domain = "myplugin";
__($text, $domain); // begins with double underscore
<?php
edit_post_link(__("Edit", "twentyeleven"), '<span class="edit-link">', "</span>");
The __()
Function takes a text string as a parameter, and looks for a translated version of $text
within a provided translation file and returns the result, if any. It is used for controlling the display of text already contained within PHP tags. If no localization files other than English exist, then the text will remain the same. The value of $domain
variable enables WordPress to recognize it as a part of your theme’s or plugin’s translation files.
__()
函数将文本字符串作为参数,并在提供的翻译文件中查找$text
的翻译版本,并返回结果(如果有)。 它用于控制PHP标记中已经包含的文本的显示。 如果除英语之外没有其他本地化文件,则文本将保持不变。 $domain
变量的值使WordPress可以将其识别为主题或插件翻译文件的一部分。
The second function you’ll use makes your text ready for translation and prints it out directly to the browser. It takes the same parameters as you just saw:
您将使用的第二个功能使您的文本可以翻译,并直接将其打印输出到浏览器。 它采用与您刚刚看到的相同的参数:
<?php
_e($text, $domain);
_e()
function looks for a translated version of $text
and echoes the result to the screen. This should be used for titles and headings that will display on the screen and are not already contained within PHP tags.
_e()
函数查找$text
的翻译版本,并将结果回显到屏幕上。 这应该用于将显示在屏幕上且尚未包含在PHP标记中的标题和标题。
In some instances you’ll want to use placeholders to insert dynamic information into a text string at runtime. Placeholders are useful because they enable you to translate strings without breaking them apart. The translation functions in WordPress cannot output placeholders on their own, though, so you’ll need to use an appropriate PHP function such as printf()
or sprintf()
.
在某些情况下,您将需要使用占位符在运行时将动态信息插入文本字符串中。 占位符很有用,因为它们使您能够翻译字符串而不会将它们分开。 但是,WordPress中的翻译功能无法自行输出占位符,因此您需要使用适当PHP函数,例如printf()
或sprintf()
。
<?php
printf(__("About %s", "twentyeleven"), get_the_author());
Here, get_the_author()
function returns the author which replaces the placeholder by printf()
.
在这里, get_the_author()
函数返回作者,该作者将printf()
替换为占位符。
Sometimes you need multiple placeholders in one text string. Luckily, both printf()
and sprintf()
handle this wonderfully. It’s best to use numbered placeholders because the order of words in one language may be different in another.
有时,一个文本字符串中需要多个占位符。 幸运的是, printf()
和sprintf()
都能很好地处理此问题。 最好使用带数字的占位符,因为一种语言中的单词顺序可能与另一种语言不同。
<?php
$site_name = get_bloginfo("name");
$comments_count = wp_count_comments();
$mod_comments = $comments_count->moderated
$appr_comments = $comments_count->approved
$all_comments = $comments_count->total_comments
printf(__('There are %3$s comments on %4$s. %2$s approved and %1$s in moderation.', $domain), $mod_comments, $appr_comments, $all_comments, $site_name);
创建翻译文件 (Creating Translations Files)
Now with your theme or plugin files correctly internationalized, you need to create translations files which WordPress will use to lookup the translation.
现在,您的主题或插件文件已正确国际化,您需要创建翻译文件,WordPress将使用这些文件来查找翻译。
The first file you need to create is a POT (Portable Object Template) file. A POT file lists all of the translatable text strings that can be found in your application and serves as a template for individual PO files. The PO (Portable Object) file is the file that maps the text strings to the actual translation. Translators work with this file to provide a translation. The POT and PO files aren’t necessary to run a translation of your plugin, but it’s nice to package it with your plugin download for use by other users that may want to update the translations to suit their needs. Finally, the PO file is compiled into a MO (Machine Object) file which is the finished translation file which WordPress uses to translate internationalized text strings for your theme or plugin.
您需要创建的第一个文件是POT(便携式对象模板)文件。 一个POT文件列出了可以在您的应用程序中找到的所有可翻译文本字符串,并用作单个PO文件的模板。 PO(便携式对象)文件是将文本字符串映射到实际翻译的文件。 译者使用此文件来提供翻译。 POT和PO文件不是运行插件翻译所必需的,但最好将其与插件下载打包在一起,以供其他可能希望更新翻译以适应其需求的用户使用。 最后,PO文件被编译成MO(机器对象)文件,这是最终的翻译文件,WordPress使用该文件来翻译主题或插件的国际化文本字符串。
When translators create translations of your theme or plugin they use your POT file to create two files, $locale.po
and $locale.mo
for a theme, and pluginname-$locale.po
and pluginname-$locale.mo
for a plugin. $locale
here represents a combination of both a language and a country code specified by the GNU gettext framework—you can see the language and country abbreviations in the gettext manual. For example, WordPress uses “en_US” as its default locale. “en” represents English, and US represents the country/dialect.
当翻译创建主题或插件的翻译,他们使用你的POT文件来创建两个文件, $locale .po
和$locale .mo
为主题, pluginname-$locale .po
和pluginname-$locale .mo
一个插件。 $locale
在这里代表GNU gettext框架指定的语言和国家/地区代码的组合,您可以在gettext手册中查看语言和国家/地区的缩写。 例如,WordPress使用“ en_US”作为默认语言环境。 “ en”代表英语,美国代表国家/方言。
A variety of translation tools are available which are open source and free to download. However one of the most common tools is Poedit which is simple and easy to use. Here are the steps to create a POT file using Poedit:
提供了多种翻译工具,这些工具是开源的,可以免费下载。 但是,最常见的工具之一是Poedit ,它简单易用。 以下是使用Poedit创建POT文件的步骤:
Choose File
> New Catalog
. A Settings box appears with three tabs. In the Project info tab, fill in the input boxes that are relevant to your plugin, leaving the Charset box as UTF-8.
选择“ File
>“ New Catalog
。 出现一个设置框,其中包含三个选项卡。 在“项目信息”选项卡中,填写与插件相关的输入框,将“字符集”框保留为UTF-8。
![img02 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img02.jpg)
In the Paths tab, change the Base Path to ../
and add an extra path of .
if you’re placing your translations in the languages
folder of your plugin. If not, then leave this at the default.
在“路径”选项卡中,将“基本路径”更改为../
并添加一个额外的路径.
如果要将翻译内容放置在插件的languages
文件夹中。 如果不是,则将其保留为默认值。
![img03 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img03.jpg)
In the Keywords tab, enter the functions you’ve learned above as keywords.
在[关键字]标签中,输入您在上方学到的函数作为关键字。
![img04 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img04.jpg)
Click OK and save your file as themename.po
or pluginname.po
in your languages
directory. Poedit will scan your PHP files searching for __()
and _e()
wrapped strings, and automatically output them in a dialog window.
单击确定,然后在您的languages
目录pluginname .po
文件另存为themename .po
或pluginname .po
。 Poedit将扫描您PHP文件,以搜索__()
和_e()
包装的字符串,并将其自动输出到对话框窗口中。
![img05 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img05.jpg)
Click OK. Now you will see a window with your strings.
单击确定。 现在,您将看到一个带有字符串的窗口。
![img06 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img06.jpg)
Save the file again and close it. In your languages
directory you will now see the PO and MO files generated by Poedit. Delete the MO file and rename the PO file with the .pot
extension. That’s it. Now your theme or plugin is ready for translation.
再次保存文件并关闭它。 现在,在您的languages
目录中,您将看到Poedit生成的PO和MO文件。 删除MO文件,并使用.pot
扩展名重命名PO文件。 而已。 现在您的主题或插件已准备好进行翻译。
If you have the required skills you can go further and make the actually translation of your files. To do, Choose File
> New catalog from POT file
. Locate the POT file, open it, and then click OK to the subsequent catalog settings (they are the exact same ones you set yourself). Poedit will ask you what you’d like to save your new PO file as – give it an appropriate name, for example es_ES.po
or pluginname-es_ES.po
and save it in the languages
directory.
如果您具备必要的技能,则可以走得更远,并进行文件的实际翻译。 为此,请选择“ File
> New catalog from POT file
。 找到POT文件,将其打开,然后单击“确定”以进行后续的目录设置(它们与您自己设置的完全相同)。 Poedit会询问您要将新PO文件另存为的名称–给它一个适当的名称,例如es_ES.po
或pluginname -es_ES.po
并将其保存在languages
目录中。
Now just click on the string you’d like to translate in Poedit, and type a translation into the box on the bottom of the Poedit window.
现在,只需单击要在Poedit中翻译的字符串,然后在Poedit窗口底部的框中键入翻译内容即可。
![img07 alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2012/04/img07.jpg)
The last thing you need to do in order to see your theme or plugin translated is to set your locale in the wp-config.php
file. This tells WordPress to look for any translations with that locale and load them. If you set your locale to es_ES (Spanish), WordPress would load a file called es_ES.mo
a theme, or pluginname-es_ES.mo
for a plugin.
要查看主题或插件翻译,您需要做的最后一件事是在wp-config.php
文件中设置语言环境。 这告诉WordPress查找具有该语言环境的所有翻译并加载它们。 如果将语言环境设置为es_ES(西班牙语),则WordPress将加载名为es_ES.mo
的主题文件,或插件pluginname -es_ES.mo
的插件。
<?php
define('WPLANG', ''); // this is default for a fresh WordPress installation
define('WPLANG', 'es_ES') // set the second parameter to your locale
摘要 (Summary)
As you can see the translation of WordPress themes and plugins is a pretty straightforward process. There is no need to be a PHP guru; it’s just a matter of following several simple steps in a logical sequence. And now when you have good understanding of these steps, I hope that you will apply this knowledge in your existing or future theme or plugin projects, thus giving WordPress users the ability to use their favorite publishing platform in a language which is natural for them.
如您所见,WordPress主题和插件的翻译是一个非常简单的过程。 不需要有PHP专家。 只需按照逻辑顺序执行几个简单的步骤即可。 现在,当您对这些步骤有了充分的了解后,我希望您可以将这些知识应用于现有或将来的主题或插件项目中,从而使WordPress用户能够使用对他们来说很自然的语言来使用他们喜欢的发布平台。
Image via iQoncept / Shutterstock