wordpress插件
In a previous post, I covered the fundamentals of WordPress internationalization (abbreviated as i18n
); how to install a localized version of WordPress and how an existing WordPress site can be easily converted to a localized version.
在上一篇文章中,我介绍了WordPress国际化的基本知识 (缩写为i18n
)。 如何安装WordPress的本地化版本以及如何将现有WordPress网站轻松转换为本地化版本。
In this article, I’m going to walk you through the process of Internationalizing WordPress plugins. The process isn’t difficult and once the knowledge is acquired, you can have your WordPress plugins easily translated to other languages.
在本文中,我将引导您完成国际化WordPress插件的过程。 这个过程并不困难,一旦获得了知识,就可以轻松地将WordPress插件翻译成其他语言。
国际化与本地化之间的差异 (Difference Between Internationalization and Localization)
Over the years, developers tend to misconstrue the meaning of these terms – Internationalization
and Localization
.
多年来,开发人员往往会误解这些术语的含义- Internationalization
和Localization
。
Internationalization is the process of developing your plugin so it can easily be translated into other languages.
国际化是开发插件的过程,因此可以轻松地将其翻译成其他语言。
Localization describes the subsequent process of translating an internationalized plugin to a new language.
本地化描述了将国际化插件翻译成新语言的后续过程。
It is worthy of note that Internationalization
is often abbreviated as i18n
(because there are 18 letters between the ‘i’ and the ‘n’) and Localization
is abbreviated as l10n
(because there are 10 letters between the ‘l’ and the ‘n’.)
值得注意的是, Internationalization
通常缩写为i18n
(因为'i'和'n'之间有18个字母),而Localization
则缩写为l10n
(因为'l'和'n之间有10个字母'。)
为什么要国际化? (Why Internationalize?)
The answer is simple; WordPress is used all over the world in many different languages. When plugins are internationalized, they attract a larger audience from other parts of the world who would obviously benefit by using the plugin in their own language.
答案很简单; WordPress在世界各地以许多不同的语言使用。 插件国际化后,它们会吸引来自世界其他地区的更多受众,而这些受众显然会受益于以自己的语言使用插件。
As a developer, you may not have time to provide localized versions of your plugin because you do not speak other languages. However, when you internationalize your plugin, you leave the door open for others to create localization without necessarily modifying the source code.
作为开发人员,您可能没有时间提供插件的本地化版本,因为您不会说其他语言。 但是,当您对插件进行国际化时,您无需其他人即可创建本地化,而无需修改源代码。
插件国际化 (Plugin Internationalization)
Now that we are familiar with the concept of plugin internationalization and localization, let’s dive into the process of making plugins ready for translation.
现在我们熟悉了插件国际化和本地化的概念,让我们深入研究使插件准备好进行翻译的过程。
设置翻译标题 (Set Translation Headers)
The first step to take in making a plugin translatable is to include the translation headers among the plugin headers.
使插件可翻译的第一步是在插件头中包括翻译头。
The translation headers are the Text Domain
and the Domain Path
.
转换标题为“ Text Domain
和“ Domain Path
。
The Text Domain
is used to denote all text belonging to a plugin. It is a unique identifier which ensures WordPress can distinguish between all loaded translations. This increases portability and plays better with already existing WordPress tools.
Text Domain
用于表示属于插件的所有文本。 它是唯一的标识符,可确保WordPress可以区分所有加载的翻译。 这样可以提高可移植性,并可以与现有的WordPress工具一起更好地发挥作用。
The text domain must match the slug of the plugin. For example, if your plugin is a single file called sample-plugin.php
or it is contained in a folder called sample-plugin
the text domain should be sample-plugin
.
文本域必须与插件的标签匹配。 例如,如果您的插件是一个名为sample-plugin.php
文件,或者包含在一个名为sample-plugin
的文件夹中,则文本域应为sample-plugin
。
关于文本域的注释 (A Note on Text Domain)
The text domain name must use dashes and not underscores.
文本域名必须使用破折号而不是下划线。
Remember I said the text domain must match with the plugin slug? That may not be true after all. I carried out a quick experiment with one of my plugins, instead of the plugin slug, I used a unique text and it worked without any problems.
还记得我说过的文本域必须与插件一致吗? 毕竟那可能不正确。 我对其中的一个插件进行了快速实验,而不是插件插件,而是使用了唯一的文本,并且该插件没有任何问题。
Moral: ensure the Text Domain
is unique so it doesn’t clash with that of other plugins.
道德:确保Text Domain
是唯一的,以免与其他插件冲突。
The Domain Path
is the folder WordPress will search for the .mo
translation files.
Domain Path
是WordPress将搜索.mo
翻译文件的文件夹。
By default, WordPress searches the plugin directory for the translation files to use. Having the translation file at the root folder of your plugin could disorganize your plugin structure.
默认情况下,WordPress在插件目录中搜索要使用的翻译文件。 将转换文件放在插件的根文件夹中可能会使插件结构混乱。
If you wish to keep the translation files in a folder, for example; /languages
, you need to inform WordPress about it using the Domain Path
header.
例如,如果您希望将翻译文件保存在文件夹中; /languages
,您需要使用Domain Path
标头将其告知WordPress。
Below is a typical header of a WordPress plugin including that of the translation.
以下是WordPress插件的典型标头,包括翻译的标头。
<?php
/*
Plugin Name: Enable Shortcode and PHP in Text widget
Plugin URI: http://w3guy.com/shortcode-php-support-wordpress-text-widget/
Description: Enable shortcode support and execute PHP in WordPress's Text Widget
Author: Agbonghama Collins
Version: 1.2
Author URI: http://w3guy.com
Text Domain: espw-plugin
Domain Path: /languages/
*/
载入文字网域 (Load Text Domain)
Now, we would use the load_plugin_textdomain() function to tell WordPress to load a translation file if it exists for the user’s language.
现在,我们将使用load_plugin_textdomain()函数告诉WordPress加载翻译文件(如果存在的语言适用于用户的语言)。
Below is the function synopsis.
下面是功能简介。
<?php load_plugin_textdomain( $domain, $abs_rel_path, $plugin_rel_path ) ?>
The first parameter $domain
should be the text domain; the $abs_rel_path
has been deprecated and should be set to false
; finally, $plugin_rel_path
is the relative path to translation files.
第一个参数$domain
应该是文本域; $abs_rel_path
已被弃用,应设置为false
; 最后, $plugin_rel_path
是翻译文件的相对路径。
If translation MO files is in the plugin’s own directory, use as follows:
如果翻译MO文件位于插件自己的目录中,请按以下方式使用:
load_plugin_textdomain( 'espw-plugin', false, dirname( plugin_basename( __FILE__ ) ) );
If translation MO files is in the plugin’s languages subdirectory. Use as follows:
如果翻译MO文件位于插件的languages子目录中。 用法如下:
load_plugin_textdomain( 'espw-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
You don’t just call the load_plugin_textdomain
function, it should be called in your plugin as early as the plugins_loaded
action like this:
您不仅要调用load_plugin_textdomain
函数,还应在plugins_loaded
操作中尽早调用此函数,如下所示:
function load_plugin_textdomain() {
load_plugin_textdomain( 'espw-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'load_plugin_textdomain' );
深入了解I18n插件 (Deep Dive into Plugin I18n)
Now that the Text Domain and the Domain Path header is set, it’s time to learn how to internationalize a plugin.
既然已经设置了“文本域”和“域路径”标头,是时候学习如何使插件国际化了。
This segment of the tutorial will be divided into the following:
本教程的这一部分将分为以下几部分:
- Strings translation 字符串翻译
- Using Placeholders 使用占位符
- HTML translation HTML翻译
- Dealing with Plurals 处理复数
- Disambiguation by context 通过上下文进行歧义消除
- Escaping translation strings 转义翻译字符串
Please note: The string espw-plugin
will be use as text domain in this tutorial.
请注意:在本教程中,字符串espw-plugin
将用作文本域。
1.字符串翻译 (1. Strings Translation)
To make a string translatable in your plugin, wrap the original string in a __()
function call as follows:
要使字符串在您的插件中可翻译,请将原始字符串包装在__()
函数调用中,如下所示:
$text = __( 'Hello, SitePoint Readers!', 'espw-plugin' );
If you want to echo the string to the browser, instead of the echo
language construct, use the _e
function:
如果要向浏览器回显字符串,而不是echo
语言构造,请使用_e
函数:
_e( 'Hello, SitePoint Readers!', 'espw-plugin' );
2.使用占位符 (2. Using Placeholders)
As PHP and WordPress developers, I assume you know what placeholders
are. You can quickly skim through the sprintf and printf() PHP documentation for further information.
作为PHP和WordPress开发人员,我假设您知道什么是placeholders
。 您可以快速浏览sprintf和printf() PHP文档以获得更多信息。
If you are using variables in strings like the example below, you should use placeholders.
如果要在字符串中使用变量(例如下面的示例),则应使用占位符。
echo 'Your city is $city.'
The right way is to use the printf()
function as follows:
正确的方法是如下使用printf()
函数:
printf(
__( 'Your city is %s.', 'espw-plugin' ),
$city
);
Going through the code of some plugins hosted in the WordPress plugin repository, I do see things like this:
浏览WordPress插件存储库中托管的一些插件的代码,我确实看到了类似这样的信息:
echo __('Your city is $city', 'espw-plugin');
_e('Your city is $city', 'espw-plugin');
Although the strings are now translatable, the PHP variable $city
also becomes translatable.
尽管字符串现在可以翻译了,但是PHP变量$city
也可以翻译。
This is a bad practice because a translator could mistakenly alter the variable or inject malicious code into the plugin code base, and this will ultimately make the plugin malfunction.
这是一种不好的做法,因为翻译人员可能会错误地更改变量或将恶意代码注入插件代码库中,这最终会使插件发生故障。
The sprintf
function is similar to the printf
in that they format string using placeholders while printf
outputs a formatted string, the sprintf
return the string.
sprintf
函数与printf
相似,因为它们使用占位符格式化字符串,而printf
输出格式化的字符串,而sprintf
返回该字符串。
Example: the following code assign a formatted string to the variable $text
.
示例:以下代码将格式化的字符串分配给变量$text
。
$text = sprintf( __('Your city is %s.', 'espw-plugin'), $city );
3. HTML翻译 (3. HTML Translation)
Including HTML in translatable strings depends on the context.
在可翻译字符串中包含HTML取决于上下文。
An example is a link (separated from text surrounding it):
一个示例是链接(与周围的文本分开):
<div class="site-info">
<a href="http://wordpress.org/" >< ?php _e( 'Proudly powered by WordPress.', 'espw-plugin' ); ?></a>
</div>
Another example is a link in a paragraph (not separated from text surrounding it):
另一个示例是段落中的链接(未与其周围的文本分开):
<?php
$url = 'http://example.com';
$link = sprintf( __( 'Check out this link to my <a href="%s">website</a> made with WordPress.', 'espw-plugin' ), $url );
echo $link;
?>
4.处理复数 (4. Dealing with Plurals)
A string that changes when the number of items changes can be internationalized using the _n()
function.
可以使用_n()
函数将在项目数更改时更改的字符串国际化。
This function accepts 4 arguments, namely:
此函数接受4个参数,即:
Singular – the singular form of the string
奇 -字符串的单数形式
Plural – the plural form of the string
复数 –字符串的复数形式
Count – the number of objects, which will determine whether the singular or the plural form should be returned
计数 –对象的数量,这将确定应返回单数还是复数形式
Text Domain – the plugins text domain
文本域 –插件文本域
Let’s see some examples to comprehend how the _n()
function works.
让我们看一些示例,了解_n()
函数的工作方式。
In English you have "One comment"
and "Two comments"
. In other languages you can have multiple plural forms.
在英语中,您有"One comment"
和"Two comments"
。 在其他语言中,可以有多种复数形式。
The code below demonstrates how to handle such scenario using the _n()
function.
下面的代码演示了如何使用_n()
函数处理这种情况。
printf(
_n(
'One comment',
'%s comments',
get_comments_number(),
'espw-plugin'
),
number_format_i18n( get_comments_number() )
);
Code Explanation the code above consist of these three functions – printf
, _n
and number_format_i18n
.
代码说明上面的代码由这三个函数组成printf
, _n
和number_format_i18n
。
For easy assimilation, the function code will be dissected with each function component explained.
为了易于吸收,将对功能代码进行分解,并解释每个功能组件。
_n(
'One comment',
'%s comments',
get_comments_number(),
'espw-plugin'
)
The first argument passed to the _n
function is the text to be displayed when the number of comments is singular.
传递给_n
函数的第一个参数是注释数为单数时要显示的文本。
The second is the text displayed when the number of comments is greater than one.
第二个是注释数大于一个时显示的文本。
The placeholder %s
will contain the value of number_format_i18n( get_comments_number() )
which will be explained shortly.
占位符%s
将包含number_format_i18n( get_comments_number() )
的值, number_format_i18n( get_comments_number() )
将对其进行说明。
The third argument get_comments_number()
is assumed to be a function that returns the comment count.
假定第三个参数get_comments_number()
是一个返回注释计数的函数。
If it returns 1
, the first argument One comment
get outputted by printf
otherwise the second argument %s comments
is returned if it is greater than 1
.
如果返回1
,则第一个参数One comment
由printf
输出,否则,第二个参数%s comments
大于1
返回。
Please note: The placeholder %s
gets replaced by the integer returned by number_format_i18n( get_comments_number() )
which is the second argument passed to the printf
function.
请注意:占位符%s
替换为number_format_i18n( get_comments_number() )
返回的整数,该整数是传递给printf
函数的第二个参数。
Finally, the fourth argument is the translation text domain.
最后,第四个参数是翻译文本域 。
The function number_format_i18n()
converts the comment count to format based on the locale. See the documentation for more information.
函数number_format_i18n()
将注释计数转换为基于语言环境的格式。 请参阅文档以获取更多信息。
Similar to the number_format_i18n()
is the date_i18n that retrieves the date in localized format, based on timestamp.
与number_format_i18n()
类似的是date_i18n ,它基于时间戳以本地化格式检索日期。
Still on the _n()
function, below is another demonstration of how the function works.
仍在_n()
函数上,下面是该函数如何工作的另一个演示。
printf( _n( 'We deleted one spam message.', 'We deleted %d spam messages.', $count, 'my-text-domain' ), $count );
If the variable $count
returns 1
, the text We deleted one spam message
will be displayed; but if it is greater than 1
, We deleted %d spam messages
will be displayed with the placeholder %d
replaced by the integer value of $count
.
如果变量$count
返回1
,将显示文本We deleted one spam message
; 但是如果它大于1
,则将显示%d
的占位符由$count
的整数值替换的“ We deleted %d spam messages
。
5.按上下文消除歧义 (5. Disambiguation by Context)
Sometimes one term is used in several contexts, although it is one and the same word in English it has to be translated differently in other languages.
有时在多个上下文中使用一个术语,尽管该术语是英语中的同一单词,但在其他语言中的翻译也有所不同。
For example the word Post
can be used both as a verb as in "Click here to post your comment"
and as a noun "Edit this post"
.
例如,单词Post
既可以用作动词(如"Click here to post your comment"
的动词),也可以用作名词"Edit this post"
。
In such cases the _x or _ex function should be used.
It is similar to __()
and _e()
, but it has an extra argument — $context
.
它类似于__()
和_e()
,但是有一个额外的参数$context
。
_x( 'Post', 'noun', 'espw-plugin' );
_x( 'Post', 'verb', 'espw-plugin' );
Using this method in both cases we will get the string Comment
for the original version, but the translators will see two Comment
strings for translation, each in the different context.
在这两种情况下都使用此方法,我们将获得原始版本的字符串Comment
,但是翻译者将看到两个Comment
字符串用于翻译,每个字符串都在不同的上下文中。
When the strings made translatable by the function _x()
get parsed by a translation tool such as Poedit, the context argument provides a hint to the translator on the context the string/text was used.
当通过函数_x()
翻译的字符串被翻译工具(例如Poedit _x()
解析时,context参数会向使用该字符串/文本的上下文的翻译者提供提示。
In German, the Post as a noun is Beitrag
while as a verb is verbuchen.
在德语中,Post作为名词是Beitrag
而作为动词是verbuchen。
Below is a screenshot of Poedit translating the string Post
to German with the context squared-bracketed.
以下是Poedit的屏幕截图,该字符串将方括号括起来的字符串翻译为Post
到德语。
While _x()
retrieve translated string, _ex()
displays it.
_x()
检索翻译后的字符串时, _ex()
显示它。
6.转义翻译字符串 (6. Escaping Translation Strings)
WordPress has a number of functions for validating and sanitizing data.
WordPress具有许多用于验证和清除数据的功能。
Among the list are functions for escaping translation texts – esc_html(), esc_html_e(), esc_html_x(), esc_attr(), esc_attr_e() and esc_attr_x(). You can get more information on each of these functions at WordPress Codex.
列表中包括用于转义翻译文本的函数-esc_html(),esc_html_e(),esc_html_x(),esc_attr(),esc_attr_e()和esc_attr_x()。 您可以在WordPress Codex上获得有关这些功能的更多信息。
I don’t need to explain every one of these, but what they do is basically escape translatable texts.
我不需要解释其中的每一个,但是它们所做的基本上是转义可翻译文本。
结语 (Wrap Up)
One of the goals of WordPress is to make it easy for users across the world to publish content. As a plugin developer, you can help to further ease the publishing process for users when you internationalize your plugins.
WordPress的目标之一是使全世界的用户都可以轻松发布内容。 作为插件开发人员,您可以在国际化插件时帮助进一步简化用户的发布过程。
The first part of this tutorial was essentially about everything you need to know about plugin i18n.
本教程的第一部分实质上涉及您需要了解的有关插件i18n的所有信息。
The concluding part will be a walk-through on how to make a plugin translation ready as well as learning how to localize a plugin to a new language.
最后一部分将是有关如何做好插件翻译准备以及如何将插件本地化为新语言的演练。
I do hope you have learned something new from this tutorial.
我希望您从本教程中学到了一些新知识。
Happy coding!
祝您编码愉快!
翻译自: https://www.sitepoint.com/wordpress-i18n-make-your-plugin-translation-ready/
wordpress插件