Translation of WordPress themes: _e function and .mo, .po files

December 5, 2016 in 12:56 pm | wiki

This article is dedicated to the translation of WordPress themes without having to connect plugins like qTranslate X.

Quick algorithm is as follows:

1) PHP files have to use this type of function (for example h1 tag):

<h1><?php _e('title', 'softsprint'); ?></h1>

‘title’ – this is the text that we aim to translate. It will appear in the tag h1 of our theme.
‘softsprint’ – the name of the theme folder which has to be translated. In our case, the folder is named ‘softsprint’.
More about _e function can be read here:

2) Pulling the values for the translation from the PHP files using the online services like http://www.icanlocalize.com/tools/php_scanner and then getting .po file

3) Open the created .po file in Poedit and define the translations for each value. There you have also to define the locale: for example, ru_RU:

4) Save our .po file and compile it into a .mo file. Compiled .mo file has to be renamed into ru_RU.mo (according to our language).
The list of languages and countries:

5) Put ru_RU.mo file to the languages, folder in our theme folder (in our case – softsprint). It is not necessary to place .po file in the folder, but save it for yourself – possibly you will need to edit / add new translations in the future;

6) Add the next code in the functions.php file (located in our theme folder):

<?php function my_theme_setup(){
	load_theme_textdomain('softsprint', get_template_directory() . '/languages');
add_action('after_setup_theme', 'my_theme_setup');?>

‘softsprint’ – the name of our theme folder;
‘/languages’ – the folder where the ru_RU.mo file was placed.

More details about the function load_theme_textdomain are described here: http://wp-kama.ru/function/load_theme_textdomain
The result is a concise code in the PHP files without extra plugins (eg, qTranslate X) and constructions of such kind:

<li><?php _e('Google Analytics и счётчики'); ?></li>

In addition, the translation editing is centralized (with a .po file) and does not need to open and edit every php file separately.

Let`s discuss

Our contacts

Lviv, Ukraine
моб: +380 97 551 44 55
skype: softsprint.net

Say hello!