softsprint menu Order

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):

1
<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:
https://codex.wordpress.org/Function_Reference/_e
http://wp-kama.ru/function/_e

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:
poedit-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:
http://www.gnu.org/software/gettext/manual/html_chapter/gettext_16.html#Language-Codes
http://www.gnu.org/software/gettext/manual/html_chapter/gettext_16.html#Country-Codes

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):

1
2
3
4
<?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:

1
<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

softsprint heart

Our contacts

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

Say hello!