Theme Translation Guide for WordPress MultiLingual Projects

Make Do have been lucky enough to work with several international organisations over the years. This means that we have a lot of experience in making a WordPress website that works in multiple languages.

Simple Code, Multiple Languages

Supporting MultiLingual Projects as a WordPress Agency

When building a multilingual WordPress theme you need to ensure that both your content and your theme are able to be translated.

As an agency that supports multilingual websites, when you build a WordPress theme you need to ensure that any ‘static’ text can be translatable. IE any text that is hardcoded into the theme.

‘Theme’ Translation

You do this by using the built in WordPress internationalization functions (or i18n for short).

These are small functions that you can use to ensure that your text can be picked up by an internationalization parser. And they look like this:

  • __( 'String to Translate', 'my-theme' ); – String with no escaping
  • _e( 'String to Translate', 'my-theme' ); – Echoed string with no escaping
  • esc_html_e( 'String to Translate', 'my-theme' ); – Echoed string with HTML escaping (use esc_html__for no echo).
  • esc_attr_e( 'String to Translate', 'my-theme' ); – Echoed string with Attribute escaping (use esc_attr__for no echo).

You will note that each of the functions above have a second parameter (in this case my-theme, this is called the ‘Text Domain’. This allows you do set the translation defaults for your theme (or plugin, if you are using these functions inside a plugin).

To set a Text Domain you need to add the following line of meta to your theme (or plugin) definition file like so:

/*
 * Theme Name: Make Do Theme
 * Author: Make Do
 * Text Domain: my-theme
 */

Next you need to use the load_theme_textdomain() (there are also plugin and child theme variants of this function) to tell WordPress where your theme translation files are:

function my_theme_setup(){
    load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_theme_setup' );

In the ‘languages’ folder (which we pointed to in our last code example) is where we place our .mo, .po and .pot files.

These files essentially build a library of all the strings in your plugin (or theme) that can be translated, and the relevant translation files. You can do this by hand, but we prefer to let a plugin such as Loco Translate by Tim Whitlock, or the WordPress Multilingual (WPML) String Translation module generate these automatically for us.

Loco Translate in Action
Loco Translate in Action

You can read the WordPress guide to Internationalization for more information on i18n.

‘Content’ Translation

In the past Make Do have built completely custom translation systems for our clients that have very specific needs (for example, if multiple languages are needed within a single country, or even on the same page), along with all the locale manipulation that goes with it so that the pages can be indexed appropriately by search engines.

However, for most users, we would recommend using WordPress Multilingual (WPML), which is a great tool that lets you translate your content and your menus into alternative languages. It does have a price attached to it, but one that is well worth it in our opinion.

You can translate any piece of content within your WordPress Install:

Switch between languages in WPML
Switch between languages in WPML

When you click into a post, you can choose weather to start from scratch, or import the existing content:

Translate Content via the Sidebar
Translate Content via the Sidebar

Finally, you can configure WPML to output the URL with a language prefix, so you could have /es/articles/my-article/ for a Spanish version of /articles/my-article/.

There are of course other translation services available, but for our enterprise level clients, this is how we would provide the best theme translation service possible as a WordPress agency.

Let's talk about you

Prefer to talk on the phone?
You can speak to Kimb by calling us direct on (+44) 01143 606660.

Email the team…
Send your requirements to hello@makedo.net and we’ll reply within 24 hours.

Get Your WordPress Website Gutenberg Ready

In this post our WordPress Engineer Charlie describes WordPress development best practice on how you can quickly get your older WordPress websites ready for Gutenberg.

Read more blog posts

Sign up to our newsletter