Translation is one of the basic features that every WordPress plugin or theme should have these days. As WordPress grows significantly all over the world, people from various countries speaking various languages start using WordPress. From my observation, many new eCommerce and company websites in Indonesia alone use WordPress. I expect the same in other countries.
When developing a WordPress plugin or theme, we can’t assume that our plugin or theme will be used only by English-speaking people. Even though English is widely used in the world, we still need to provide a way for non-English-speaking people to use our plugin or theme. Fortunately, WordPress provides the translation feature very well.
How to make a translatable string
When echoing a string, how do we usually do it? This is what I did when developing a WordPress plugin for the first time:
<?php echo 'Select a display mode that you want to use'; ?>
The string is not translatable, unless user change the source code which is a terrible idea. So, how do we make it translatable? Here’s how to do it in WordPress:
<?php _e( 'Select a display mode that you want to use', 'text-domain' ); ?>
We use _e()
function instead of echo
. The _e()
function is WordPress-defined gettext PHP function that is used for echoing translatable string. The first parameter of the _e()
function is a string that we want to make it translatable. The second parameter is a plugin or theme text domain. It should be unique and used consistently in the plugin or theme, it could be our plugin or theme name, or an acronym. In a nutshell, text domain is an identifier that will be used to decide which plugin or theme a certain translation belongs to.
Then how do we usually store a string into a variable? It’s usually like this:
<?php $string = 'This is a string.'; ?>
Again, the string is not translatable. So how do we make it translatable? Use this instead:
<?php $string = __( 'This is a string.', 'text-domain' ); ?>
A new problem arises. How do we echo a translatable formatted string? We can combine printf()
with __()
and placeholder to accomplish that. Here is an example:
<?php printf( __( 'You have a new %s shirt.', 'text-domain' ), $color ); ?>
The value of $color
will be inserted into %s
placeholder in the translated string like usual printf()
result.
For all translation-related WordPress functions, go to their specific codex page here. It has detailed description of each function that you can use to make translatable strings, including plural string, context-specific string, etc.
Developing a translatable WordPress plugin or theme is actually not difficult since WordPress has provided all necessary functions that we can use to make our plugins or themes translatable. It’s only a matter of choice. After knowing how easy it is to develop a translatable plugin, I started to cultivate a good habit by using translation functions whenever I wanted to make translatable strings. So right now whenever I want to echo out a translatable string, I will automatically use _e()
instead of plain echo
.
It’s easy, right?
Leave your comments below!