wordpress 多语言_带有Polylang的多语言WordPress网站

wordpress 多语言

WordPress is available in a lot of different languages. However, when you switch to another language in the “General Settings” panel, by default, you can’t use several languages at the same time.

WordPress支持多种语言。 但是,默认情况下,当您在“常规设置”面板中切换到另一种语言时,不能同时使用多种语言。

Sometimes, being able to use several languages is what we’re after. I can illustrate this assertion with my own example: my blog is available in two languages (English and French). If you prefer the French language, you can switch to it without any problem, I don’t need to switch the entire website language preferences.

有时,我们追求的是能够使用多种语言。 我可以用自己的示例来说明这一主张:我的博客提供两种语言(英语和法语)。 如果您更喜欢法语,则可以毫无问题地切换到它,我不需要切换整个网站的语言首选项。

By default, it’s not possible to do this in WordPress, but there are plugins that can help you achieve this goal. In this article I’ll introduce you to an entirely free and complete plugin called Polylang. We’ll look at how to install it, how to configure it and, above all, how to use it to translate the entire website (not only the posts!).

默认情况下,无法在WordPress中执行此操作,但是有些插件可以帮助您实现此目标。 在本文中,我将向您介绍一个名为Polylang的完全免费且完整的插件。 我们将研究如何安装它,如何配置它,以及最重要的是如何使用它来翻译整个网站(不仅是帖子!)。

在WordPress上管理几种语言 (Managing Several Languages on WordPress)

为什么要使用Polylang? (Why Use Polylang?)

Depending on exactly what you want to do, the default WordPress tools can be enough. For example, you can create a post in a given language, and create the same post in another language, attributing the right tag to differentiate it. It’s just an example, but if you want to keep the things simple, you can do that.

根据您要执行的操作,默认的WordPress工具就足够了。 例如,您可以使用给定的语言创建帖子,也可以使用另一种语言创建相同的帖子,并赋予正确的标签以区分它。 这只是一个示例,但是如果您想使事情保持简单,则可以这样做。

Multilingual WordPress Polylang

However, this isn’t a complete solution. The advantage of using Polylang is that the plugin won’t just let you translate your posts. In fact, if your theme is translated in the right language, your visitors will see an entire translated website. Using my example, when you read a French post on my blog, you’ll see a translated sidebar and footer.

但是,这不是一个完整的解决方案。 使用Polylang的好处是该插件不仅可以翻译您的文章。 实际上,如果以正确的语言翻译了主题,那么访问者将看到整个翻译后的网站。 以我的示例为例,当您在我的博客上阅读法语文章时,您会看到翻译的侧边栏和页脚。

Polylang is a powerful tool that helps you create a multi-language website. Once you’ve configured it, using Polylang to translate your posts is as easy as the example we saw above. Maybe even easier!

Polylang是一个功能强大的工具,可以帮助您创建多语言的网站。 配置完成后,使用Polylang翻译您的帖子就像我们上面看到的示例一样简单。 甚至更容易!

安装Polylang (Installing Polylang)

You can download the Polylang plugin for free via WordPress.org. It can be installed and activated like any other plugin.

您可以通过WordPress.org免费下载Polylang插件。 可以像其他任何插件一样安装和激活它。

Installing Polylang

管理可用语言 (Managing the Available Languages)

Polylang has its own options panel, accessible from the “Languages” entry in the “Settings” menu of your administration panel. We’ll look at the first tab of this panel to start with, “Languages”. As the name suggests, it lets you manage the languages you want to use on your website.

Polylang有其自己的选项面板,可从管理面板的“设置”菜单中的“语言”条目访问。 我们将以“语言”开头查看该面板的第一个标签。 顾名思义,它使您可以管理要在网站上使用的语言。

To add a new language, the simplest way is by using the dropdown list labeled “Choose a language”. There are dozens of available languages by default and chances are that you’ll find the one you want. If you can’t, you can still use it by creating your own (its name, locale, code and direction).

要添加新语言,最简单的方法是使用标记为“选择语言”的下拉列表。 默认情况下,有数十种可用的语言,您很可能会找到所需的一种。 如果不能,您仍然可以通过创建自己的名称(区域名称,代码和方向)来使用它。

You can also choose the order in which your languages must appear in the list. The “Order” text input excepts a number. Languages will be sorted using this number, in ascending order.

您还可以选择语言在列表中出现的顺序。 “订单”文本输入除外,数字。 语言将使用此数字以升序排序。

Once a language has been added, you can edit its information. These options can be seen by hovering over the language in the list.

添加语言后,您可以编辑其信息。 将鼠标悬停在列表中的语言上即可看到这些选项。

配置Polylang (Configuring Polylang)

By visiting the “Settings” tab in the Polylang options panel, you’ll find some useful options to personalize your multi-language experience.

通过访问Polylang选项面板中的“设置”选项卡,您将找到一些有用的选项,以个性化您的多语言体验。

The first option, and probably the most important one, is the default language you want for your website. It can be any language you’ve added. If you have visitors using a language you don’t support, they’ll see this default language.

第一种选择,也许是最重要的选择,是您想要网站使用的默认语言。 它可以是您添加的任何语言。 如果您的访客使用了您不支持的语言,他们将看到该默认语言。

If your website is not a new one, chances are great that you already have some published posts. Since you’ve installed Polylang after creating them, existing posts won’t have a language set. You can give them all the default language setting from this tab.

如果您的网站不是一个新网站,则很可能已经有一些已发布的帖子。 由于您是在创建Polylang后安装的,因此现有帖子不会设置语言。 您可以从此选项卡为他们提供所有默认语言设置。

In this tab you’ll also find a few more options you’ll want to check, in particular your URL preference.

在此标签中,您还将找到其他一些要检查的选项,特别是URL首选项。

完全翻译的网站 (A Fully Translated Website)

主题翻译 (Theme Translation)

As mentioned above, Polylang goes further than only translating our posts. Our visitors can have an entire experience in their preferred language.

如上所述,Polylang不仅仅是翻译我们的帖子。 我们的访客可以使用他们喜欢的语言来获得完整的体验。

To do this, you’ll need a translated theme. If the theme you use is translation-ready, you should find in it (probably in a subfolder like languages) a POT file. This file contains all the strings used in the theme. That way you can translate them into all the languages you want.

为此,您需要翻译后的主题。 如果您使用的主题已准备好翻译,则应在其中找到一个POT文件(可能在languages的子文件夹languages )。 该文件包含主题中使用的所有字符串。 这样,您可以将它们翻译成所需的所有语言。

Now, when Polylang displays the right language on your website, it won’t only display the posts in this language, but also the theme. That way, your visitors won’t see French text in an English page for example.

现在,当Polylang在您的网站上显示正确的语言时,它不仅会显示该语言的帖子,还会显示主题。 这样,您的访客就不会在英语页面上看到法语文本。

Note that if you use a WordPress default theme, like Twenty Fifteen, when you add a new language in Polylang, it tries to download and install the translations of this theme in this language.

请注意,如果您使用WordPress默认主题(如“二十一十五”),则在Polylang中添加新语言时,它将尝试下载并安装该主题在该语言中的翻译。

动态字符串翻译 (Dynamic String Translation)

We can’t translate everyhting with a simple POT file. For example, widgets’ titles are strings that are created by the user (you), so naturally we can’t use a POT file to translate these. However, there’s another way to translate them thanks to Polylang.

我们无法使用简单的POT文件来翻译所有内容。 例如,小部件的标题是由用户(您)创建的字符串,因此自然地我们不能使用POT文件来翻译它们。 但是,由于Polylang,还有另一种翻译方法。

Go in the second tab of Polylang options panel. This tab is called “Strings Translation” and contains various strings that are display on your website. By default, Polylang generates several dynamic strings. You’ll find strings such as site name and description, you can even translate time formats.

进入Polylang选项面板的第二个选项卡。 此选项卡称为“字符串转换”,包含在您的网站上显示的各种字符串。 默认情况下,Polylang会生成几个动态字符串。 您会发现诸如网站名称和描述之类的字符串,甚至可以转换时间格式。

Polylang will also retrieve all the widgets you are using. Their titles and content (for the Text widget for example) can then be translated with dynamic strings. It’s worth noting that when you create a widget when Polylang is active, you can now select a language in the widget setting.

Polylang还将检索您正在使用的所有小部件。 然后可以使用动态字符串翻译其标题和内容(例如,用于“文本”小部件)。 值得注意的是,在启用Polylang的情况下创建窗口小部件时,现在可以在窗口小部件设置中选择一种语言。

In the “Strings Translation” tab, Polylang lists all the registered dynamic strings, in their original language. You can then translate them into the languages you use. Not only your theme can be translated, but also your dynamic content.

在“字符串转换”选项卡中,Polylang以其原始语言列出所有已注册的动态字符串。 然后,您可以将它们翻译成您使用的语言。 不仅可以翻译主题,还可以翻译动态内容。

注册我们自己的动态字符串 (Registering Our Own Dynamic Strings)

If you’re developing a theme or a plugin, you can make it “Polylang-ready” by registering your own dynamic strings. For example, it can be useful if you need to display a string users can define by themselves. By registering these strings, these same users will be able to provide them into several languages.

如果您正在开发主题或插件,则可以通过注册自己的动态字符串将其设置为“ Polylang-ready”。 例如,如果您需要显示用户可以自己定义的字符串,它可能会很有用。 通过注册这些字符串,这些相同的用户将能够以多种语言提供它们。

To register a string, use the pll_register_string() function. As with any other function defined by another plugin, you should check if this function exists with the following test:

要注册字符串,请使用pll_register_string()函数。 与其他插件定义的任何其他功能一样,您应该通过以下测试检查该功能是否存在:

if (function_exists('pll_register_string')) {
    // The function exists, do whatever you want!
}

Even if Polylang is installed and activated, this function can be missing. For example, it’s possible to try to call it too soon, before it is created. To ensure that you don’t call it too soon, be sure to call it after the action “plugins_loaded” occurs. For example, this is the case if you register your strings directly in the functions.php file of your theme.

即使安装并激活了Polylang,也可能缺少此功能。 例如,可以在创建之前尝试过早调用它。 为了确保您不会过早调用它,请确保在执行“ plugins_loaded”操作后调用它。 例如,如果您直接在主题的functions.php文件中注册字符串,就是这种情况。

Now that we’ve seen how to call this function, it’s time to see its parameters. This function excepts four parameters, but the last two are optional.

现在我们已经了解了如何调用此函数,是时候查看其参数了。 此函数除四个参数外,但最后两个是可选的。

The first parameter is the name of the string. This name is a way to describe it. Refer to your current “Strings Translation” tab for examples of its use. For instance, a string like “l, F j, Y” is not always clear, but with the name “Date Format”, it is.

第一个参数是字符串的名称。 此名称是描述它的一种方式。 有关其用法的示例,请参考当前的“字符串转换”选项卡。 例如,诸如“ l,F j,Y”之类的字符串并不总是很清楚,但名称为“日期格式”却很清楚。

The second required parameter is the string to translate. You can write this in any language you want, but try to be consistent with the rest of your theme or plugin.

第二个必需参数是要转换的字符串。 您可以使用所需的任何语言来编写此代码,但请尝试与主题或插件的其余部分保持一致。

pll_register_string('My awesome string', 'This is a translated string.');

The third parameter represents the group in which your string must be placed. By default, this group is named “polylang” but you can define your own group, by indicating your theme’s name for example. Group names can be seen by your users in the registered strings list. It’s another way for your users to better understand where your string is displayed (if several plugins use Polylang, your users will thank you for indicating that a string is for your plugin and not for another).

第三个参数表示必须在其中放置字符串的组。 默认情况下,该组名为“ polylang”,但是您可以通过指定主题名称来定义自己的组。 用户可以在注册的字符串列表中看到组名。 这是让用户更好地了解字符串显示位置的另一种方式(如果多个插件使用Polylang,则用户将感谢您指出字符串是用于您的插件而不是其他字符串)。

The last parameter won’t be useful for everyone, but it might be for some. It’s a boolean set to false by default. If you set if to true, then Polylang will provide a multiline textarea to let the user translating the string, instead of a one line text input.

最后一个参数对每个人都没有用,但可能对某些人有用。 默认情况下,它是设置为false的布尔值。 如果将if设置为true ,则Polylang将提供多行文本区域,以允许用户翻译字符串,而不是单行文本输入。

检索注册的字符串 (Retrieving Registered Strings)

Once your strings are registered, your users can translate them in the “Strings Translation” tab. But you still need a way to retrieve them, in order to display the right translation!

一旦注册了字符串,您的用户就可以在“字符串翻译”标签中对其进行翻译。 但是您仍然需要一种检索它们的方法,以显示正确的翻译!

There are essentially two functions you can use for that. The first one is pll__() (note the two underscores for the __() WordPress function). You give it the string to translate, and it returns the same string, translated into the current language.

您基本上可以使用两个功能。 第一个是pll__() (请注意__() WordPress函数的两个下划线)。 您给它提供要翻译的字符串,然后它返回相同的字符串,并将其翻译成当前语言。

$translated_string = pll__('This is a translated string.');

If you don’t want to do anything with this translated string (like injecting it in a function like sprintf() for example), you can directly echo it with pll_e().

如果您不希望使用此转换后的字符串做任何事情(例如将其注入到sprintf()类的函数中),则可以直接使用pll_e()回显它。

pll_e('This is a translated string.');

You can also use a third function: pll_translate_string(). This last one excepts two parameters: the string to translate and the language in which you want the translation. This can be useful if you want to display text like “View this post in [any language]” in the targeted language, for example.

您还可以使用第三个函数: pll_translate_string() 。 最后一个除外两个参数:要翻译的字符串和要翻译的语言。 例如,如果您要以目标语言显示诸如“以任何语言查看此帖子”之类的文字,这将非常有用。

echo pll_translate_string('View this post in French', 'fr');

帖子翻译 (Posts Translation)

翻译文章 (Translating a Post)

Now that you’ve set up Polylang, all your existing posts should be in the default language. That means they won’t appear in other languages.

现在,您已经设置了Polylang,所有现有帖子都应该使用默认语言。 这意味着它们不会以其他语言显示。

In the posts list, you’ll find new columns representing the translation state in every added language. If a “plus” is shown in a post line, that means that the corresponding translation doesn’t exist. You can add a translation by clicking on this “plus”.

在帖子列表中,您将找到代表每种添加语言的翻译状态的新列。 如果发布行中显示“加号”,则表示相应的翻译不存在。 您可以通过单击此“加号”来添加翻译。

There’s nothing else we need to after this point. You write your translation like any other post and that’s it! Do you have another word for this other than “awesome”?

在此之后,我们不需要任何其他操作。 您可以像其他帖子一样撰写翻译,仅此而已! 除了“真棒”之外,您还有别的意思吗?

Note that you can also add a translation to a post when you are editing it. Polylang has its own box in the editor, listing the existing translations for the current post. From this list, you can edit existing translations and add others.

请注意,您还可以在编辑帖子时为其添加翻译。 Polylang在编辑器中有其自己的框,列出了当前帖子的现有翻译。 在此列表中,您可以编辑现有翻译并添加其他翻译。

翻译类别和标签 (Translating Categories and Tags)

So far, we’ve translated our theme, widgets and posts, but there are still a few things we haven’t covered: taxonomies like categories and tags. We can translate these, by visiting the “Categories” or “Tags” page of your administration panel. You’ll be able to translate them exactly the same as we did for posts.

到目前为止,我们已经翻译了主题,小部件和帖子,但是仍然有一些我们没有涉及的内容:分类法(例如类别和标签)。 我们可以通过访问管理面板的“类别”或“标签”页面来翻译这些内容。 您将可以像翻译帖子一样完全翻译它们。

Once your categories and tags are translated, you can use them in your posts translations. However, there is a last Polylang option I want to share with you here: the synchronization between translations.

类别和标签翻译完成后,您可以在文章翻译中使用它们。 但是,我想在这里与您分享最后一个Polylang选项:翻译之间的同步。

In the “Settings” tab, at the end of the page, you’ll find some checkboxes. By checking some of them, you can activate the synchronization of the corresponding element between all the translations of your posts.

在页面末尾的“设置”标签中,您会找到一些复选框。 通过检查其中一些,您可以激活帖子的所有翻译之间相应元素的同步。

For example, assume that you have a category named “My category”, translated in French by “Ma catégorie”. You’ll write a post in this category, in English, and you want to translate it in French. In this translation, don’t indicate any category: if you’ve activated the taxonomies synchronization, then the “Ma catégorie” category will be automatically selected!

例如,假设您有一个名为“我的类别”的类别,该类别由“ Macatégorie”翻译成法语。 您将以英语撰写此类别的帖子,并希望将其翻译为法语。 在此翻译中,请勿指示任何类别:如果您已激活分类同步,则将自动选择“ Macatégorie”类别!

征服世界! (Conquer the World!)

In this article I’ve described most of the main options that I find useful in Polylang, but there are others that you’ll surely want to check out. Don’t hesitate to test Polylang in a local installation, and test some of the variations before using this plugin on your production website.

在本文中,我描述了在Polylang中有用的大多数主要选项,但是您肯定要检查其他选项。 在您的生产网站上使用此插件之前,请不要犹豫在本地安装中测试Polylang,并测试一些变体。

If you want to know more about the options, functions and filters provided by Polylang, you can visit the official Polylang documentation, which is very complete.

如果您想了解有关Polylang提供的选项,功能和过滤器的更多信息,可以访问非常完整的Polylang官方文档

翻译自: https://www.sitepoint.com/multilingual-wordpress-websites-with-polylang/

wordpress 多语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值