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:
- Internationalization (i18n), the process of preparing your theme to be translated by the end users.
- 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.
- Wrap every string in a gettext function.
- Load your text domain.
- 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
- __(); 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
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
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
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
This is your theme’s style.css file header, please note the Text Domain line
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.
Creating a multilingual WordPress theme is not a tough task and it’s almost necessary for today’s market…
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.