SOFT
SPRINT

softsprint.net Shop

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

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


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

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

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

CONTACT US
Cookies | Privacy Policy | Terms and ConditionsSoftSprint ©