Make your Oxygen site multilingual, using Polylang translation strings

When creating multilingual sites, Polylang is my plugin of choice. The way it handles translations of pages and the functionality it has in the free version is very impressive, and the Pro version even more so. I haven't tried competitor plugins like WPML, because Polylang has always done the job well for me.

When it comes to using Polylang with Oxygen, there are two ways to go about translating your templates:

  1. Translate the entire template
  2. Use translation strings for static text

Option 1: Translating the Oxygen Template with Polylang

Option 1 is the method that most Polylang users will be familiar with. Simply, you need to enable the translation of templates using the following option under Languages > Settings:

Make your Oxygen site multilingual, using Polylang translation strings 1

Once you've done this and saved changes, go to your template. You'll see a new metabox containing the Language options for the page. Click the plus icon to create another version of the template, which you can then edit as required. If you've already created the alternative template, you can search for it in the empty white box:

Make your Oxygen site multilingual, using Polylang translation strings 2

However, this approach has a couple of major issues:

  • If you change anything on the template for one language, you have to manually change it on the other template. Even if you're using reusable elements and CSS classes, there will still be quite some double-work to do.
  • Once the site grows beyond two languages, it'll get exponentially harder to manage as for every template you'll need to create translated versions for each. If your site is translated into 3 languages, and you have 10 templates, that means 30 templates to manage.

For these reasons, I prefer Option 2.

Option 2: Translating Oxygen Templates with Polylang Translation Strings

The free version of Polylang comes with a translation string module included. It enables you to specify strings of texts that are used in your templates, and then dynamically display the correct translation depending on the locale that is being viewed.

The translation strings module looks like this:

Make your Oxygen site multilingual, using Polylang translation strings 3

If you have plugins with translatable elements installed (e.g. Rank Math, Cookie Notice, etc), then you can filter the strings to show only the ones specific to these plugins. Just click View All Groups and choose the appropriate option.

Registering translatable strings with Polylang

Before adding translatable strings in your template, you'll need to register them so that they appear among the Polylang strings translations.

From here on in, we'll be relying on some PHP functions outlined in the excellent Polylang function reference document.

  1. Install and activate the Code Snippets plugin.
  2. Create a new snippet titled 'Polylang - Translation Strings'.
  3. Inside the snippet, add the following:
add_action('init', function() {
  if( function_exists('pll_register_string') ) { // only run if function exists
    pll_register_string('Test String', 'Test String US', 'test-group', false);
    pll_register_string('Test String 2', 'Test String USA', 'test-group', false);
  }
});

As we can see on the Polylang documentation, the function pll_register_string takes 4 arguments:

pll_register_string($name, $string, $group, $multiline);
  • The name of the string
  • The string to translate, written in the default language
  • The group to add your string to (this will be filterable in the strings list)
  • Whether you want the string to be on more than one line
  1. Save and activate the snippet, then go back to Languages > Strings translations. Filter by your created group (test-group) and voila, you will see your strings ready to be translated:
Make your Oxygen site multilingual, using Polylang translation strings 4
  1. Add the correct translation into each field and click Save Changes. Now you're ready to add the string to the template.

Adding a Polylang translation string to your Oxygen template

  1. Open your template in Oxygen, and create a Code Block where you want your translated string to appear. Inside the PHP section, paste the following:
<?php
if( function_exists('pll_e') ) { // only run if function exists
  pll_e('Test String US');
}
?>

The function 'pll_e' will echo (i.e. output) the string in the language the visitor is using, assuming that the string has been already registered and translated as in the previous steps.

  1. Duplicate the Code Block and replace 'Test String US' with 'Test String USA'.
  2. If your template doesn't have a language switcher yet, you will want to add one for testing purposes. This article in the Polylang documentation will help - it only takes a minute to set up.
  3. Save the template and view in frontend. You will see that the strings change when you switch to a different language:

Why I like this approach:

  • All can be done within a single Oxygen template, and can be scaled to as many languages as required.
  • Strings translation provides a nice UI for site editors to modify static text in templates without needing Oxygen access.
  • If you have a lot of strings, you can download a .po file containing them, and then re-import it once the translations have been added.

Get in touch by emailing alex (at) smoothwebsites.net if you need assistance with setting up a Polylang website with Oxygen.

All features shown in this article are available in the free Polylang plugin found in the WordPress plugin repository. However I would still recommend Polylang Pro for the extra features it provides, such as content synchronization and sharing URL slugs between languages.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram