Internationalization(i18n) and localization(l10n) are anchoring terminologies that have to be known while learning about translation. Internationalization is the process of adding the capability to the software to be translated to any locale.
Localization is the process of translating a software from its original language to the target language. Previously, we have seen about how to translate a translation-ready theme to a specified language. Now, let us see how to make a WordPress theme to be a translation-ready theme.
Making a WordPress theme translation ready is simple and easy. WordPress uses gettext functions to translate the specified string to the target language.
So, all the translatable strings of a WordPress theme in a text domain should be displayed by using the gettext functions. We need to have the list of translatable strings in a portable template (.POT) file (preferably created by using a tool or software to get good outcome).
The .POT file will be the base to create .po, .mo language mapping files for translating to the target locale.
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 text domain.
Note: The theme specification will be in 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 themes text domain by using using 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 below code in your theme’s functions.php to load the text domain.
load_theme_textdomain( 'yourthemename', language_file_path );
WordPress uses gettext framework function to translate the text string into the specified target language. It has 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 below code shows how to use gettext function to localize your theme.
Consider the following code showing text string without gettext functions.
// Previous/next page navigation. the_posts_pagination( array( 'prev_text' => "Previous page", 'next_text' => "Next page", 'before_page_number' => ' <span class="meta-nav screen-reader-text">Page</span> ', ) );
The text string in the above code will be enclosed by the localization functions as follows.
// Previous/next page navigation. the_posts_pagination( array( 'prev_text' => __( 'Previous page', 'yourthemename' ), 'next_text' => __( 'Next page', 'yourthemename' ), 'before_page_number' => ' <span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span> ', ) );
If there is PHP echo statement then it will be changed by using _e() function instead of __(). There are more gettext functions for various purposes to handle context sensual text, pluralization and many.
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 theme’s translatable strings.