Internationalizing WordPress for Localization: A Guide

Are you tired of using plugins or themes that contain untranslated strings in your language? It can be frustrating and irritating, but the solution is simple: internationalization. WordPress has specific functions that make it easy to translate everything, but many developers don’t use them properly. In this article, we’ll go through the steps to properly internationalize your WordPress theme or plugin.

Step 1: Loading Translation Files

The first step is to load translation files. This can be done using the following code:

For a theme:

add_action(‘after_setup_theme’, ‘my_theme_setup’);

function my_theme_setup(){

load_theme_textdomain(‘my_theme’, get_template_directory() . ‘/languages’);


For a plugin:

function myplugin_init() {

load_plugin_textdomain( ‘my-plugin’, false, dirname( plugin_basename( __FILE__ ) ) );


add_action(‘plugins_loaded’, ‘myplugin_init’);

These files should be stored in a “languages” folder. You can then create a .pot or .po file using free software like POedit.

Step 2: Translating Strings

To make a string translatable, you need to include it in a function. The most commonly used functions are _e() and __(). Here’s an example of how to use __():

echo ‘

‘ . __( ‘This is the string content’, ‘textdomain’ ) . ‘


This function returns the string content, but doesn’t print it. To print it, you need to use echo. The _e() function prints the string content without using echo or print:

echo ‘


_e( ‘This is the string content’, ‘textdomain’ );

echo ‘


Step 3: Translating Strings with Variables

Sometimes a string may contain variables, which can’t be translated using _e() or __(). In this case, you need to use printf() and sprintf() functions. Here’s an example:

echo ‘


printf( __( ‘I bought %d books.’ ), $_books_count );

echo ‘


echo ‘


echo sprintf( __( ‘I bought %d books.’ ), $_books_count );

echo ‘


Step 4: Strings with More Than One Variable

If a string contains more than one variable, use the following code:

printf( __( ‘I bought %1$s books, and %2$s tomatoes.’ ), $books_count, $tomatoes_count );

Step 5: Dealing with Plurals

If a string contains a variable that could be singular or plural, use the _n() function. Here’s an example:

printf( _n( ‘I bought %d book.’, ‘I bought %d books.’, $books_count ), $books_count );

Step 6: Contexts

Sometimes a word can have different meanings depending on its context. In this case, use the _x() and _ex() functions. The second one prints the string content, while the first one only stores it. These functions have a second argument to explain the context. Here’s an example:

// In the content

echo _x( ‘apparent’, ‘in_content’, ‘my-plugin-domain’ );

// In the sidebar

echo _x( ‘apparent’, ‘in_sidebar’, ‘my-plugin-domain’ );

Step 7: JavaScript Internationalization

If you have a string that needs to be translated in a JavaScript file, use the wp_localize_script() method defined in the codex. Here’s an example:

// In your PHP file:

wp_enqueue_script( ‘script-handle’, … );

wp_localize_script( ‘script-handle’, ‘objectL10n’, array(

‘speed’ => $distance / $time,

‘submit’ => __( ‘Submit’, ‘my-plugin-domain’ ),

) );

// In the JavaScript file:


$(‘#speed’).val(‘{speed} km/h’.replace(‘{speed}’, objectL10n.speed));

By following these steps, you can easily internationalize your WordPress theme or plugin and make it accessible to users in different languages.

Stay in Touch


Related Articles