How to create a Multilingual WordPress Theme

JavaScript for WordPress

What does Multilingual WordPress Theme mean?

The multilingual theme is the theme that can be translated into multiple languages by using language files (.po/.mo) or by using a translation plugin, like WPML.
As a theme developer, you may need to know that WordPress downloads in non-English languages have exceeded the English downloads. So WordPress is going global and your theme has to be ready for localization.

Making a multilingual WordPress theme process:

  1. Internationalization (i18n), the process of preparing your theme to be translated by the end users.
  2. Localization (l10n), the process of translating the internationalized theme.

I’ll be concerned with internationalization, later I’ll write about the different options to localize an internationalized theme.

Internationalization process:

  1. Wrap every string in a gettext function.
  2. Load your text domain.
  3. Language files.

Step 1 : Wrap every string in a gettext function

Every string in your theme needs to be wrapped in a gettext function. The most commonly used functions are

Please note:

  • __(); is a double underscores function.
  • _e(); is the same as __(); but it just echoes the result.
  • Strings and text domains are in single quotes.
  • Text domain has to be a literal string, not a constant nor a function!
  • There are many functions based on __(); and _e(); such as esc_html_e();. It first runs the string through _e(); then it does esc_html();

Example on __();

The function in that example looks for the translation of ‘Hello World’ and returns it. If it does not exist, it returns ‘Hello World’.

Example on _e();

Here, the function is looking for the translation of ‘Hello World’ and prints it out. If it does not exist, it prints out the string which is ‘Hello World’.

Now, if you have a message that contains a variable, for example:

“This page contains 20 posts.” and ‘20’ is coming from this variable

$num_posts=20;

Do you think this is right ?

__(‘This page contains $num_posts posts.’ , 'theme_text_domain');

NO! it’s not that way, it should be done by

HTML Elements

Do you think that the translator needs to translate the HTML markup in your string ? Of course not, markup needs to be eliminated from your code

Pluralization

From our example above, when $num_posts = 1; the message will be “This page contains 1 posts.” which is wrong, it should be “This page contains 1 post.”
In this case, we should use this function

The _n() function will return the first message if $num_posts = 1 or the second message if $num_posts is more than 1.
Now, every string and message in your theme are translatable, the next step is to create the languages files that needed for your clients to be able to translate your theme. However, without the language files, your theme will be translatable using translation plugins like WPML.

Step 2: Load your text domain

For best results, you’ll need to use your text domain in 2 files, style.css, and functions.php

In style.css

This is your theme’s style.css file header, please note the Text Domain line

In functions.php

Step 3: Language files

Those files will contain every translatable string or text in your theme files, the strings you have wrapped in gettext functions. And also, the translations you may provide your clients with.
Those files are:
POT: It’s a file contains every translatable string or text.
.po: It’s the file created when you translate your POT file to a particular language. For example, fr_FR.po
.mo: It’s the file that the machine can read, it’s a binary file. You’ll have to convert your .po file to .mo file. For example, fr_FR.mo
The process of creating POT file and translating it to .po file then converting it to .mo file can be done using a software like POEdit.
You don’t have to create .po and .mo files for every language on the planet. You can just create the main POT file for your clients.
In this post, I explain in details with screenshots, how to use POEdit to create language files for your theme.

Conclusion:

Creating a multilingual WordPress theme is not a tough task and it’s almost necessary for today’s market…

Need help?

If you want to be sure that your theme is properly internationalized, or if you need a hand in creating your multilingual WordPress theme, please feel free to contact us and we can help.

GET MORE STUFF 
IN YOUR INBOX
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
Close
GET MORE STUFF
IN YOUR INBOX
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
We have created the 1st online directory for the Multilingual Themes
CLICK HERE

WE'RE SOCIAL!

Follow us on..