How to translate WordPress theme using .po files (tutorial for humans)

How to translate WordPress theme using .po files (tutorial for humans)

Mar 4, 2021

Once upon a time I faced with seemed simple task – create theme translation files – .po and .mo extensions, but it turned into pain.

I’ve searched through forums few hours, and finally found free, and easy to use solution. Here is what I’ve done.

1. Automatically generate .pot file from theme files:

1. Download Eazy Po program

2. From file menu select “New from source code files..”.

3. In GNU xgettext Command Manager window; Press “Browse folder” to select base source folder.

4. In Build pane press “Execute Command” button to generate Pot file.

2. Create .po and .mo files for translations

Download PoEdit.

Open program > Choose “Create new translation”

Choose necessary language:

Make translation of each string:

Then click File -> Save as -> Save it as .po file – it will create .mo version automatically in the same folder – automatic files names will be assigned correctly regarding to the language slug. Repeat the same action for each language version.

3. Connect new translation to your theme

First, create /lang folder inside your theme and put those .po and .mo files there.
Open your theme’s functions.php and put this line somewhere in the code:

load_theme_textdomain( 'your-texdomain', get_template_directory() . '/lang' );

You can replace ‘your-texdomain’ by your theme folder name or anything you want. Necessary translation files will be connected automatically, just keep their names as 4-letters ISO language codes (see list here – ietf-language-tags – left column), just replace dash into lower dash (for example en-GB.po to en_GB.po).
That’s basically it – your theme is localized!!!

P.S.

You can use Loco Translate plugin to change translations of certain strings.
Just install plugin, go to Loco Translate -> Themes under your WordPress dashboard and choose the localization you need to edit.

Big thanks to ٍShagrouni for hints on wordpress.stackexchange.

Share

You might be interested