the_content ( ') more ' ) ; 19659026] The call of the function must be transformed to:
the_content ( __ ( ' Read more '' mitema ') ) ;
A Sometimes a text message includes dynamic data, such as numbers of a PHP variable. In these cases we will use the function PHP
printf to produce the string of the final message:
$ results_found = 12 ;  $ message = sprintf ( __ ( '% s results found ' ' mitema ') $ results_found ] ) ;
Singular and plural
WordPress offers a function for singular and plural translation of the same text message:
_n ( $ single $ plural $ , $ domain )
The first argument is the text that will be used for the sing ular, and the second is the text that will be used for the plural. The third argument is the number with which to compare to decide which to use.
Although the function
_n () is included in WordPress it is not recommended to use it since the translation software only uses the first parameter of a function, so these two text messages would not be processed. Instead we can use the PHP declaration
if ( $ results_found == ] 1 )
$ message = __ ( ' 1 result found ' ' my – text – domain ') ; ]
$ message = sprintf ( __ ( '% s results found ' ' my – text – domain ') $ results_found ) ;
Once we have processed each text message with the func ions mentioned above our subject is ready for translation. For these purposes there are three types of language files:
- The .POT file contains a list of all translatable messages in your theme
- The .po file is created when we translate a POT file into a specific language
- The .mo file is a binary file that is created automatically by Poedit. This is the one that WordPress reads to translate themes and plugins
We created the POT
The first thing is to create the POT file, which contains all the messages of the theme code and which will be the file that the translator use to translate messages from another language file. Although there are several applications to edit and create language files I recommend Poedit which is multiplatform and is the one I use.
- Open Poedit and create a new catalog
- Complete the catalog information in the "Project information" tab
- In the "Routes" tab, identify the folder where Poedit will search the files with the code that contains the translatable text messages. These folders are relative to the folder of our language file so we will save the file in a folder of your theme folder, so we will add
- In the "Keywords" tab we will define the WordPress functions used to translate messages (remember that Poedit is not only for WordPress). In our subject the two key words used have been
- Once you click "Accept" Poedit looks in the folders that we have told you in the "Routes" tab and will list the text messages found in the files of the theme.
- Saves the POT file in a folder called
languors inside the your subject folder.
This newly created POT file does not really need WordPress, it is simply the template that contains the translatable message strings, which you can provide to the translator for translate it and return a file
4. Offer a translation for each message and create a language file for a specific translation
As we already have the POT now we just have to start to translate text strings with Poedit the application, the first time save changes create the files alone
- Start choosing text strings from the list and translate one by one in the fields below
- Keep on translating until all the text strings are translated into your language
- Save and you will see the files
.mo automatically generated in the folder
5. Tell WordPress to activate the location and load the language file
Once we have created the files
.mo we can tell WordPress to activate the location and load the language files. Edit the file
wp-config.php which you'll find in the root folder of the WordPress installation.
What you need to do is modify the variable
WPLANG to the language you want use. For example:
It would be like this if we are going to use location (translation) in Spanish of Spain:
define ( ' WPLANG ' ' es_ES ') ;
If WordPress finds a translation file to the "Spanish of Spain" as we have defined in the catalog configuration , in the folder that we configured in the first step of this tutorial, then it will show the messages translated to this language in the subject and the visitors will see it in Spanish of Spain. If I did not find it then I would show the messages by default.
Although it was not about plugins, this guide does not hurt to explain how a plugin is prepared for translation especially when there are steps that are repeated, so let's see what changes at the time of internationalize plugins .
As we do with the theme, the first thing is to tell WordPress where you will find the language files, adding the following function, in this case in the main plugin file:
load_plugin_textdomain ( 'miplugin' , false basename ( dirname ( __ FILE __ ) ] ) . '/ languages' ) ;
The first parameter defines the domain of the text, if possible using the name of the plugin, since it must be unique. The second parameter defines the path of the folder where the file
.mo will be. As befits an outdated function, which was valid until WordPress 2.7, we will specify as value
false . The third parameter is the folder where the language files are, in this case we assume that there is a folder called
language inside the plugin folder.
Also, to load the language files we have to add a '
hook ' and link this function in the action
init from WordPress:
function traduccion_miplugin ()
load_plugin_textdomain ( 'miplugin' false basename ( dirname ( __ FILE __ ) ) . '/ languages' ) ]
add_action ( 'init' 'traduccion_miplugin' ]) ;
To finish we will have to process the text messages as we have done with the theme, following steps 2 to 5, using the functions that you know by now:
_e ($ text_message ) and
__ ($ text_message) . Once everything has been processed and the translation files created, our plugin will be ready for translation.