How to Make a WordPress Theme Translation Ready

by Vincy. Last modified on July 13th, 2022.

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.


3 Simple Steps to Create Translation-Ready Theme

  1. Create and load text domain for your WordPress theme.
  2. Enclose Translatable Text String with WordPress gettext function.
  3. Create the POT file.

Create and Load 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 text domain.

Note: The theme specification will be in theme’s style.css

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 );

Enclose Translatable Text String with WordPress gettext Function

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.

Creating POT file with 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 theme’s translatable strings.

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page