Home » Tutorials » Translating Theme and Plugin Texts

Translating Theme and Plugin Texts

May 28th, 2012 by

WPML’s String Translation module lets you translate texts that other plugins and your theme create.

Before we go into the details of how it works in WPML, I want to spend 2 minutes explaining how WordPress handles string localization. This will make it much simpler to understand what WPML does and how you should use it.

WordPress, like other PHP programs, uses a localization framework called GetText. This framework let you wrap texts in calls.

For example, to display the label of a ‘Reply’ button, we use this call:

<?php _e('Reply','my-text-domain'); ?>

If you’re not familiar with GetText, skip the details for now. I want to talk about the principles first. If you’re eager to learn GetText basics, have a look at this FAQ about String Translation.

There are three phases:

  1. Collecting strings for translation – in this phase GetText, or another program will go through the code and look for these wrapping calls. When found, it will add the text inside the call to the list of texts for translation.
  2. Translation – we translate all the texts that were found in the previous step
  3. Applying the translation – when the program runs, as the GetText functions are called, they check if there is translation for each of the texts. If so, it gets output instead of the original. Hence, the site displays in the correct language.

As you can see, GetText handles just the ‘static’ texts in your site. These are all the little texts in the PHP. The content that you enter in the WordPress admin, in pages and posts is completely unrelated to GetText. Still, even though it’s a lot less volume than the ‘dynamic’ content, being able to translate the static texts is essential for a multilingual site.

How WPML translates GetText strings

WPML complements GetText by letting you do everything from within the WordPress admin. Let’s see what WPML does for the three steps that we listed above:

  1. Collecting strings for translation – WPML scans the PHP and looks for texts wrapped in GetText calls. It can scan plugins and your theme. This saves you the need of using an offline PHP parsing tool to generate the translation files.
  2. Translation – you’ll do all the translation work from within WPML’s String Translation module in the WordPress admin. No need to download and upload translation files.
  3. Applying the translation – WPML filters the standard GetText calls. If there’s translation in WPML’s String Translation, this translation applies. Otherwise, WPML let’s GetText enter the translation. This means that you can override any translation that you get in .mo files using WPML.

To enable all this, you’ll need to install WPML’s String Translation. It’s part of the Multilingual CMS package. If you haven’t done so already, go to your WPML account, click on Downloads and get the String Translation module.

Next, go to WPML->Theme and plugins localization and choose to Translate by WPML. This lets WPML connect to GetText and do all its magic.

Now, in the same Theme and plugins localization screen, you’ll see two sections. One for ‘Strings in the theme‘ and another for ‘Strings in the plugins‘. There’s a scan button for each section. When you scan the theme or plugins, WPML reports which files it’s processed. You don’t need to do much with this report, but it will help you make sure that you’re scanning the right file.

If WPML finds .mo files that include translations, it will use them. The existing translation will appear in WPML’s String Translation. Of course, you can later edit them and update the translation for these strings.

When you use that, WPML will go through all the PHP files (for what you selected) and look for strings for translation. It reports its findings and displays buttons to go to translation. Click on them and you’ll arrive at the String Translation screen.

Translating with .mo files

BTW, curious to know what ‘Translate using .mo files‘ does?

Remember, when you went to WPML->Theme and plugins localization, you had a choice between translating with WPML and translating with .mo files.

When you translate with WPML, you’re enabling WPML to connect to the GetText mechanism, scan the PHP source, extract texts and apply translations to the GetText calls.

When you choose ‘Translate using .mo files’, this entire thing is disabled. The theme and plugins (and WordPress core itself) still load their .mo files, but WPML doesn’t connect to any of it.

When you translate with WPML, all these .mo files are still loaded, but translation will come from WPML’s String translation, if translations exist there.

I hope that this makes it clearer. If you still have questions, head over to our forum and just ask. Mention that you’re asking about this tutorial, so that we have a good starting point.

More Reading on theme and plugin localization