Internationalization and Localization of WordPress Theme : Part I

If you are into WordPress theme development and want your theme to have a more users around it, Internationalization and Localization of your WordPress theme can add a plus point in doing so. In this article, I will be talking about how to make the WordPress Theme translation ready.

So, What is Internationalization and Localization?

Internationalization and Localization
Internationalization and Localization


Commonly abbreviated as i18n as there are 18 letters between the letter i and n, Internationalization is the process of making WordPress theme translation ready so that it can be adapted to various languages and regions without technical changes.


Commonly abbreviated as l10n as there are 10 letters between the letter l and n, Localization is the process of adapting internationalized WordPress theme for a specific language by translating text.

Importance of Internationalization and Localization

If you are a developer and if you make your theme translation ready. Then people who want to use your theme will use it( nothing special here ). Now, there is also chance of people who know English but want their theme in other language will also use it. And, there are people who even don’t read and write but still use WordPress theme, these people will also use it. So, your theme will eventually attract more users. Now, again if you are user you will have the benefit of using the WordPress theme in the language of your choice.

So, the developers and users have their own benefits.

If we make our theme translation ready though we don’t need it for now, we may need it in future. So, it is always good to have our theme translation ready as it is one of the best practices.

How to make the theme translation ready?

You will need to follow these two following steps:

a. Load a text domain

Copy this following code to functions.php file of your theme.

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain('your_theme_name', get_template_directory() . '/languages');

The first parameter of load_theme_textdomain function is a unique identifier and it’s a good practice to have the theme’s name and the second parameter is the path where you have your translated laguage files. It is a good pracitice to keep all the files inside the languages folder. You can keep these files anywhere you like but it looks kind of messy if there are many languages file.
Write this function inside a function as shown above and hook it to after_setup_theme action hook.

b. Processing Text Messages with WordPress functions

Here, I will discuss about functions used for internationalization and localization one bye one.

1. The __() Function

The __() function works by making your text ready for translation and returning it for use in PHP. In this example, you will assign the return value of __() to a PHP variable.

$text = __( 'WordPress is a wonderful publishing platform.', 'your_theme_name' );

Note that this function uses a double underscore, not a single underscore.

2. The _e() Function

_e() makes your text ready for localization. It works similarly to echo in PHP by displaying text on the screen.

_e( 'WordPress is a wonderful publishing platform.', 'your_theme_name' );

3. The esc_attr__() Function

esc_attr__() is the internationalization equivalent of the esc_attr() WordPress function. It escapes HTML attributes, so anything passed to it won’t break HTML validation standards or open a site up to potential security vulnerabilities. esc_attr__() returns the translation for use in PHP.

return '<a href="” title=”' .esc_attr__( 'Visit the Terms', 'your_theme_name' ) . '">' .__( 'Terms', 'your_theme_name' ) . '</a>';

4. The esc_attr_e() Function

esc_attr_e() works the same as the esc_attr__() function except that displays the translation on the screen.

<a href="<?php echo admin_url( 'dashboard.php' ); ?>" title="<?php esc_attr_e( 'Visit the WordPress dashboard', 'your_theme_name' ); ?>"> <?php _e( 'Dashboard', 'your_theme_name' ); ?></a> 

5. The esc_html__() Function

esc_html__() is the internationalization equivalent of the WordPress function esc_html(). You need to use this function in situations in which HTML would be inappropriate for use. This function returns its output for use in PHP.

Suppose a form was submitted with the content of a <textarea> in which a default text message is provided. You’d escape the input the user submitted or escape the default message that the translator provides.

/* If the user input any text, escape it. */
if ( !empty( $_POST['text'] ) )
	$message = esc_html( $_POST['text'] ); 
/* If no text was input, use a default, translated message. */
	$message = esc_html__( 'No message input by the user.', 'your_theme_name' ); 
return $message;

6. The esc_html_e() Function

esc_html_e() behaves the same as the esc_html__() function except that it displays the translated text on the screen instead of returning it.

<textarea name="your-text" id="your-id">
	esc_html_e( 'Please input a description.', 'your_theme_name' ); 

7. The _x() Function

Sometimes, you need to provide context for translations. The _x() function enables plugin developers to use the same text string multiple times within a theme. This function’s purpose is to provide a context in which a specific text string is used.

/* Displaying "Post" as a noun. */
$text = _x( 'Post', 'noun', 'your_theme_name' ); 
/* Displaying “Post” as a verb. */
$text = _x( 'Post', 'verb', 'your_theme_name' ); 

8. The _ex() Function

_ex() is a function to use when you need to note a specific context for a string of text. It works the same as the _x() function except that it echoes its output instead of returning it.

/* Displaying "Post" as a noun. */
_ex( 'Post', 'noun', 'your_theme_name' ); 
/* Displaying “Post” as a verb. */
_ex( 'Post', 'verb', 'your_theme_name' ); 

9. The esc_attr_x() Function

esc_attr_x() is a combination of two of the earlier translation functions:
esc_attr__() and _x(). It enables you to translate text, provide a context for translation, and escape it for use in HTML attributes. This function returns translated text for use in PHP, but it does not have a similar function for printing text to the screen.

/* The text for the "title" attribute of the link. */
$text = esc_attr_x( 'Admin', 'admin link', 'your_theme_name' ); 
/* Display the link on the screen. */
echo '<a href="' . admin_url( 'dashboard.php' ) . '"title="' . $text . '">' . $text . '</a>';

10. The esc_html_x() Function

esc_html_x() merges the esc_html__() and _x() functions into a single function that allows for text translation, escapes unwanted HTML, and provides a context to translators.

$text = esc_html_x( 'None','favorite item', 'your_theme_name');

11. The _n() Function

It is used to differentiate between singular and plural forms of text.

/* Count the number of posts. */
$count_posts = wp_count_posts(); 
/* Get the count for the number of posts with a post_status of 'publish'. */
$count = $count_posts->publish; 
/* Display a sentence, letting the user know how many posts are published. */
printf( _n( 'You have published %s post.', 'You have published %s posts.', $count, 'your_theme_name' ), $count );

12. The _nx() Function

The _nx() function is a combination of the _n() and _x() translation functions. It allows for the differentiation of singular and plural forms of text and a context for the text.

printf( _nx('%s post', '%s posts', $count, 'post count', 'your_theme_name'), $count);


After you process all the text messages in your theme with appropriate internationalization and localization WordPress function you will have your theme translation ready.

On the next part of the this article, I will be talking about how the extract these translation ready messages with appropriate software, provide a translation for each message, create a language file for a different language and instruct WordPress to enable localization and to load the language file.

Leave a Reply