Home >CMS Tutorial >WordPress >Multilingual WordPress Websites with Polylang
Multi-language support for WordPress: Easily create multilingual websites with Polylang plugin
WordPress itself supports multiple languages, but by default, multiple languages cannot be used simultaneously after switching languages in the General Settings panel.
Sometimes, we need to use multiple languages at the same time. For example, my blog is available in both English and French. If you prefer French, you can switch directly to the French version without changing the language settings of the entire website.
WordPress does not support this feature by default, but some plugins can help you achieve this. This article will introduce a completely free and powerful plugin: Polylang. We will learn how to install, configure it, and how to use it to translate the entire website (not just articles!).
Key points:
Manage multiple languages on WordPress
Depending on your specific needs, WordPress’s default tool may suffice. For example, you can create an article in a specific language, then create another language version of the same article, and distinguish it with the appropriate tags. This is just an example, if you want to keep it simple, you can do so.
However, this is not a complete solution. The advantage of using Polylang is that it can not only translate your articles. In fact, if your topic has been translated into the correct language, your visitors will see the entire translated website. Take my blog as an example, when you read the French article on my blog, you will see the translated sidebar and footer.
Polylang is a powerful tool that can help you create multilingual websites. Once the configuration is complete, it is as simple as the example above, and even simpler to translate the article using Polylang!
You can download the Polylang plugin for free through WordPress.org. It is installed and activated in the same way as other plugins.
Polylang has its own options panel, which can be accessed through the "Language" entry in the "Settings" menu of the admin panel. Let's first look at the first tab of the panel, "Language". As the name implies, it allows you to manage the language you want to use on your website.
The easiest way to add a new language is to use the drop-down list marked "Select Language". By default, there are dozens of languages available and you're likely to find the language you want. If not found, you can still use it by creating your own language (name, locale, code, and orientation).
You can also select the order in which the language is displayed in the list. The "Sequential" text input accepts a number. The language will use this number to sort in ascending order.
After adding the language, you can edit its information. These options can be viewed by hovering over the language in the list.
Configure Polylang
Visit the Settings tab in the Polylang Options panel and you will find some useful options to personalize your multilingual experience.
The first option, and probably the most important option, is the default language you want your website to use. It can be any language you add. If your visitors use a language you do not support, they will see this default language.
If your site is not a new site, you may have posted some articles. Since you installed Polylang after you created the article, there will be no language set for existing articles. You can set the default language for all articles from this tab.
In this tab, you will also find some other options that you need to check, especially your URL preferences.
Full translated website
As mentioned above, Polylang's function is more than just translation of articles. Your visitors can have a full experience in their preferred language.
To do this, you need a translated topic. If the theme you are using supports translation, you should find a POT file in it (probably in a subfolder like languages). This file contains all the strings used in the topic. This way, you can translate them into any language you want.
Now when Polylang displays the correct language on your website, it will not only show articles for that language, but also topics. This way, your visitors won't see French text in the English page, for example.
Note that if you use WordPress default theme (such as Twenty Fifteen), when you add a new language in Polylang, it tries to download and install the translation of this language for this theme.
We can't translate everything with just a simple POT file. For example, the title of a widget is a string created by the user (you), so we naturally cannot use a POT file to translate these. However, because of Polylang, there is another way to translate them.
Go to the second tab of the Polylang Options panel. This tab is called String Translation and contains various strings displayed on your website. By default, Polylang generates multiple dynamic strings. You will find strings like website name and description, and you can even translate the time format.
Polylang also retrieves all widgets you are using. Its title and content (such as text widgets) can then be translated using dynamic strings. It is worth noting that when creating a widget while Polylang is active, you can now choose a language in the widget settings.
In the String Translation tab, Polylang lists all registered dynamic strings and their original languages. You can then translate them into the language you are using. Not only your topic can be translated, but your dynamic content can be translated too.
If you are developing a theme or plugin, you can make it "Polylang ready" by registering your own dynamic string. For example, this is very useful if you need to display strings that users can define themselves. By registering these strings, these same users will be able to provide them to multiple languages.
To register a string, use the pll_register_string()
function. Like any other function defined by other plugins, you should check if this function exists using the following test:
<code class="language-php">if (function_exists('pll_register_string')) { // 函数存在,执行任何操作! }</code>
This function may be missing even if Polylang is installed and activated. For example, it is possible to call it too early before creating it. To make sure you don't call it too early, make sure to call it after the "plugins_loaded" operation occurs. For example, this happens if you register a string directly in the functions.php file of the topic.
Now that we have understood how to call this function, it is time to understand its parameters. This function accepts four parameters, but the last two are optional.
The first parameter is the name of the string. This name is the method that describes it. Please refer to your current String Translation tab for an example of its usage. For example, strings like "l, F j, Y" are not always clear, but using the name "date format" it becomes clear.
The second required parameter is the string to be translated. You can write this string in any language, but try to be consistent with the rest of the theme or plugin.
<code class="language-php">pll_register_string('My awesome string', 'This is a translated string.');</code>
The third parameter indicates the group to place the string. By default, this group is named "polylang", but you can define your own group, such as indicating your topic name. Users can see the group name in the registered string list. This is another way to give users a better understanding of where strings are displayed (if multiple plugins use Polylang, your user will thank you for pointing out that a certain string is for your plugin and not for others).
The last parameter is not useful for everyone, but may be useful for some. It is a boolean value that defaults to false. If set to true, Polylang will provide a multi-line text area that allows the user to translate the string instead of single-line text input.
After registering strings, users can translate them in the String Translation tab. But you still need a way to retrieve them so that the correct translation is displayed!
You can basically use two functions to achieve this. The first one is pll__()
(note the two underscores of the __()
WordPress function). You provide it with the string to be translated, it returns the same string and translates it into the current language.
<code class="language-php">if (function_exists('pll_register_string')) { // 函数存在,执行任何操作! }</code>
If you don't want to do anything with this translated string (such as injecting it into a function like sprintf()
), you can just use pll_e()
to echo it.
<code class="language-php">pll_register_string('My awesome string', 'This is a translated string.');</code>
You can also use the third function: pll_translate_string()
. The last function takes two parameters: the string to be translated and the language you want to translate. For example, this is useful when you want to display text like "View [any language] of this article" in the target language.
<code class="language-php">$translated_string = pll__('This is a translated string.');</code>
Article translation
After setting up Polylang, all your existing articles should use the default language. This means they won't appear in other languages.
In the article list, you will find a new column indicating the translation status of each added language. If the "plus sign" is displayed in the article line, it means that the corresponding translation does not exist. You can click this "plus" to add a translation.
After this, we don't need to do anything else. You can write your translation like other articles, that's it! Do you have other words to describe the word instead of “awesome”?
Please note that you can also add translations when editing articles. Polylang has its own box in the editor listing the existing translations of the current article. From this list, you can edit existing translations and add additional translations.
So far, we have translated our topics, widgets, and articles, but there are some things we haven't covered: taxonomy, such as classifications and tags. We can translate them by visiting the Categories or Tags pages of the admin panel. You can translate them like you would translate articles.
After translating categories and tags, you can use them in article translations. However, here is another Polylang option that I want to share with you: Synchronization between translations.
At the end of the page of the Settings tab, you will find some check boxes. By selecting some of these check boxes, you can activate synchronization of corresponding elements between all translations of the article.
For example, suppose you have a category called "My category" which is translated in French as "Ma catégorie". You will write an article in this category in English and wish to translate it into French. In this translation, do not indicate any classification: If you have activated taxonomy synchronization, the "Ma catégorie" category will be automatically selected!
Conquer the world!
In this article, I describe most of the major and useful options in Polylang, but there are some other options you definitely want to check out. Please don't hesitate to test Polylang in your local environment and test some variants before using this plugin for your production website.
If you want to learn more about the options, features and filters that Polylang offers, you can access Polylang's official documentation, which is very complete.
FAQs (FAQs) for Multilingual WordPress Websites with Polylang
Translation of custom strings in Polylang is an easy process. First, you need to register your string using the pll_register_string
function. This function allows you to make the string translateable. After registering strings, you can use the String Translation table under the Language menu of WordPress dashboard to translate them. Remember to save changes after translating the string.
pll_translate_string
only returns string translations in the default language? pll_translate_string
function returns the translation of the string in the current language. If it returns only string translations for the default language, it may be because the current language is not set correctly. Make sure you have set the current language correctly in Polylang settings. If the problem persists, it may be caused by conflicts with other plugins or themes. Try deactivating another plugin or switching to the default theme to see if the issue is resolved.
The function reference in Polylang is a list of functions provided by the plugin that you can use in your code. Each function is accompanied by a document with its description, parameters, and return values. To use a function, just include it in your code if necessary. For example, to get the current language, you can use the pll_current_language
function like this: echo pll_current_language();
.
To install the Polylang plugin, go to your WordPress dashboard, navigate to "Plugins" > "Add new plugin", search for Polylang, and click "Install now". Once the plugin is installed, click Activate to start using it. Once activated, you can access Polylang's settings under the Language menu in the dashboard.
To add a new language in Polylang, go to your WordPress dashboard, navigate to Language> Add New Language, fill in the necessary details such as language name, ISO code and logo, and then single Click "Add New Language". You can then translate articles, pages, and widgets in new languages.
To translate an article or page in Polylang, edit the article or page to be translated, click the plus icon next to the language you want to translate to under the "Language" box, and enter your translation in the editor. When you are done, click Publish or Update to save your translation.
To display the language switcher in Polylang, go to your WordPress dashboard, navigate to Appearance > Widgets, drag the Language Switcher widget to the widget you want area, and then configure widget settings as needed. The Language Switcher will appear on your website, allowing your visitors to switch between languages.
To translate menus in Polylang, go to your WordPress dashboard, navigate to Appearance > Menu, select the menu you want to translate, click the plus icon next to the language you want to translate to, and then Enter a translation of the menu item. When you are finished, click Save Menu to save your translation.
To translate widgets in Polylang, go to your WordPress dashboard, navigate to Appearance >Widgets, select the widgets to be translated, and click the plus sign next to the language you want to translate to Icon and enter your translation in widget settings. When you are finished, click Save to save your translation.
Handling SEO for multilingual websites in Polylang involves setting hreflang tags, translating metadata, and creating multilingual XML sitemaps. Polylang will automatically add the hreflang tag to your page, but you need to manually translate the metadata using SEO plugins like Yoast SEO. To create multilingual XML sitemaps, you can use plugins like Google XML Sitemaps.
This revised output maintains the original meaning, rephrases sentences, and uses synonyms to create a more unique text while preserving the image formatting and locations. The FAQs section has been significantly condensed to avoid repetition.
The above is the detailed content of Multilingual WordPress Websites with Polylang. For more information, please follow other related articles on the PHP Chinese website!