Preparing Strings for Localization in Themes and Plugins

Many plugin and theme developers fail to properly prepare their plugins and themes for localization. I suspect that rather than an unwillingness on their part to do this important task, it's instead a case of getting confused about what's needed. So let me give you some examples to show how easy it is to prepare strings for localization.

Many plugin and theme developers fail to properly prepare their plugins and themes for localization.

I suspect that rather than an unwillingness on their part to do this important task, it’s instead a case of getting confused about what’s needed.

So let me give you some examples to show how easy it is to prepare strings for localization.

Let’s say I am building a plugin and I need to output a simple string, to create a settings page header. Without localization it would look something like this:

[php]
echo ‘<h3>Thumbnail and Excerpt Settings</h3>’;
[/php]

If someone wants to translate the plugin to another language, they are stuck with that string, not good!

So lets add localization:

[php]
echo ‘<h3>’ . __( ‘Thumbnail and Excerpt Settings’ , ‘wprss’ ) . ‘</h3>’;
[/php]

What have we done here? We’ve used the __() function, which is used to translate a string and returns it as a string. We also add our domain (‘wprss’). This is needed and should be unique for every plugin or theme you develop.

Now the above is not the only way to localize a string, lets see some other examples, and you can decide which one you like best. All of theme work fine.

[php]
$str = __( ‘Thumbnail and Excerpt Settings’ , ‘wprss’ );
echo "<h3>$str</h3>";
[/php]

And another way using the printf() function:

[php]
printf( ‘<h3>%1$s</h3>’, __( ‘Thumbnail and Excerpt Settings’ , ‘wprss’ ) );
[/php]

In case you’re wondering, printf() is used to output a formatted string. You will probably also encounter sprintf(), a related function which writes a formatted string to a variable instead of outputting it.

For your reference, here are some common localization functions:

__ (double underscore) is the base translate function. It translates a string and returns it as a string.

_e does the same as __, but echo’s the result immediately.

_x is the contextual translate function. It has a second option to provide context to people doing the translation.

_ex is the same as _x, but echo’s the result.

Which style do you like best?

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

5 Responses

  1. Jean, this is a very good and simple tutorial to prepare a theme or plugin for localization. But you need to add these functions as well to your plugins or themes/child themes to make it complete:

    load_theme_textdomain
    load_child_theme_textdomain
    load_plugin_textdomain

    Please add it to your tutorial to make it more useful to those who just start preparing for localization.

Leave a Reply

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

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.
👋 Hey there! We're Gaby and Mark
Every week we share tutorials and genuine reviews of WordPress products and services in our newsletter.
Thousands of people read it!
We’d love for you to join.
We’d love for you to join. Here’s what you’ll be getting:

A single weekly email directly to your inbox.