It’s no secret that WordPress is used around the world. So it’s a good idea to ensure that themes and plugins can be easily translated into other languages. Adding internationalization and localization support by having a translation ready WordPress theme can greatly increase its market share.
Difference Between Internationalization and Localization
Internationalization is the process of developing a plugin/theme so it can easily be translated into other languages.
Localization describes the subsequent process of translating an internationalized plugin into a new language.
Side Note: It’s worth noting that internationalization is often abbreviated as i18n (because there are 18 letters between the ‘i’ and the ‘n’), and localization is shortened to l10n (because there are 10 letters between the ‘l’ and the ‘n’.)
The localization process can be done using a software called Poedit with three types of language files: the POT file (that contains a list of all translatable messages in the WordPress theme), the .PO file (created when you translate a POT file to a particular language) and the .MO file (a binary file that is created automatically by Poedit and is not human-readable).
Translation Ready Themes
There is some confusion about what a translation-ready theme actually means. So let’s clear up any possible confusion and explain what this entails:
- Translation ready themes that only come with a .pot file
You can find translation ready themes that only come with a .pot file (and sometimes with an English version of .mo and .po files). These files can be used to translate the theme into other languages. These themes don’t provide translations to other languages, but you have the tools needed to localize the WordPress theme like the Poedit software or the Loco Translate plugin.
For example, in the Jupiter X theme, we provide the jupiterx.pot file and a user can translate the theme to their language.
- Translation ready themes with translations into other languages
You can find translation ready themes that come with .pot file that also come with translations into other languages. These translations usually have been done by translators, volunteers or any other users.
For example, in the Jupiter 6 theme we provide .po and .mo files for some languages, meaning a customer doesn’t have to translate the theme themselves.
Steps to Create a Translation Ready Theme
- Create and load the text domain for your WordPress theme.
- Enclose the Translatable Text String with the WordPress gettext function.
- Create the POT file.
Create and Load the Text Domain
The Text Domain will be a part of the WordPress theme or plugin specification. The following code snippets show the theme and plugin specification with the text domain.
Note: The theme specification will be in the theme’s style.css
/*
Theme Name: YOUR THEMENAME
Theme URI: yourdomain/themes/yourthemename/
…
…
Text Domain: yourthemename
*/
/*
Plugin Name: Plugin Name
Plugin URI: yourdomain
…
…
Text Domain: yourthemename
*/
Load your theme’s text domain by using the load_theme_textdomain() function. This function has two parameters as the text domain name and the directory path where we will store the .pot, .po, .mo language files. Paste the code seen below in your theme’s functions.php to load the text domain.
load_theme_textdomain( ‘yourthemename’, language_file_path );
Enclose the Translatable Text String with the WordPress gettext Function
WordPress uses the gettext framework function to translate the text string into the specified target language. It has a set of functions in a core translation API file l10n.php located in the wp-includes directory. We have to enclose the text string with the appropriate function. The code below shows how to use the gettext function to localize your theme.
__()
This is one of the most basic translation functions. Like many of its siblings, it takes two parameters: the string to be translated and the text domain.
$name = __( ‘My Stats’, ‘textdomain’ );
It’s used when you want to mark a simple string for translation and return the value to use somewhere else. This is frequently the case within functions – think of registering post types.
_e()
This is almost the same as the function above, except it echoes the value. It can be used when you’re translating in the HTML content directory:
_n()
This function is used when translating strings with a conditional plural in them. This means that you don’t know in advance if the string will use the plural or singular form because it depends on the momentary value of some parameter. A good example would be a comment count.
For example, if a comment count is one, you would need to use a singular form: “One comment.” If a comment count is 0 or more than one, you would use the plural: “Many comments”. This can be done in one go by using the _n() function.
This takes four parameters: the singular form, the plural form, the number to check and the text domain:
$comment_count = get_comments_number();
$comment_count_text = _n( ‘One Comment’, ‘Many Comments’, $comment_count, ‘textdomain’ );
You can read more about the gettext function in the following articles:
https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
https://codex.wordpress.org/I18n_for_WordPress_Developers
Creating the POT file with the Translatable Text String
After applying appropriate gettext functions for all the translatable text strings of your theme files, create a .POT template file. This template will be the base consisting of all the translatable strings. This file will be referred to create language mapping file .po, .mo pair for each language translation. You can use any popular tool to create this file for getting the theme’s translatable strings.
Read more about making translation ready plugins in the WordPress Codex page.
Wrapping up:
One of the goals of WordPress is to make it easy for users across the world to publish content.
In this article, we took a look at the basics of translations, what a text domain is, the functions we can use and how to create translation files.
We hope this was helpful to you. Please share your experiences in the comments below!😉
No comment yet, add your voice below!