Common Polylang Questions for WordPress Multilingual Development

Polylang is a clean and easy-to-use WordPress multilingual plugin. Its backend settings are simple, and the overall usage flow is smooth. But for developers who are using Polylang for the first time, getting started can still be a little difficult, and some common questions tend to come up. The following notes summarize several of the questions we have run into while building multilingual WordPress sites with Polylang.

How do you determine the current language of the theme?

WordPress provides theme and plugin developers with two functions that can be used to determine the current language:

  • get_locale(), which returns the current language code in a format such as en_US
  • get_bloginfo('language'), which returns the current language name in a format such as en-US

Note that the difference between the two return values is the use of _ versus -. You can refer to the two forum topics below for more discussion of that difference.

Return the current language as variable for your template
How to translate/switch specific contents on templates

In addition, Polylang provides the pll_current_language function, which can return those different formats as well.

How do you add user-translatable strings in a theme or plugin?

You need to register the strings that require translation in the dashboard with pll_register_string, and then output them on the front end with pll__ or pll_e.

Register a translatable string:

pll_register_string('name', 'String to translate');

Display the string:

pll_e('String to translate');

How do you translate custom post types and custom taxonomies?

Polylang has built-in support for custom post types and custom taxonomies. You only need to register them on the init action.

After registering the custom post types and taxonomies, just enable support for them in the Polylang settings. In addition, you can use the pll_get_post_types or pll_get_taxonomies filters for finer control.

How do you add content in different languages instead of only in the current language?

Polylang adds language information to the main WordPress query, but you can still output content in other languages. For example, you can list the latest five French posts on an English page. You only need to add a lang parameter to your custom query.

$posts = get_posts(array(
    'post_type' => 'post',
    'lang' => 'fr', // Use the language alias in the query.
    'showposts' => 5
));

The lang parameter can be used not only with posts, but also with categories and comments. You just need to pass lang into get_terms or get_comments.

Can you query content in multiple languages?

Yes, of course. For example:

$posts = get_posts(array(
    'post_type' => 'post',
    'lang' => 'de,fr', // Query posts in German and French.
    'showposts' => 5
));

Can you query content in all languages?

Yes, that is also possible. For example:

$posts = get_posts(array(
    'post_type' => 'post',
    'lang' => '', // Query posts in all languages.
    'showposts' => 5
));

How do you display links to post translations inside the loop?

Example:

<?php while ( have_posts() ) : the_post(); ?>
<ul class='translations'>
    <?php pll_the_languages(array('post_id' =>; $post->ID)); ?>
</ul>
<?php the_content(); ?>
<?php endwhile; ?>

How do you load the Polylang API in front-end AJAX requests?

Polylang can automatically detect an AJAX request on the front end and then load the current language. You can set the lang parameter in the request, whether it is a POST or GET request, by using the language code.

When does Polylang load the language?

There are two cases:

  1. If the language is set in the content itself, which is the default behavior, Polylang needs to delay language loading. It then loads the language inside a function hooked to wp with a priority of 5.
  2. If the language code is added to all URLs, there is no need to delay language loading, and the behavior is effectively the same as if Polylang were not enabled.

In the first case, plugin authors should not translate strings before the wp action runs. Polylang provides a new action called pll_language_defined, which runs immediately after the language has been defined, regardless of how the user has configured language detection.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *