Wordpress - generating po mo translating files from scratch in a wordpress theme

You can use the tool POEdit to translate your theme from scratch or update/add new strings into the .po/.mo files. Here is the tool usage tutorial: Translating_With_Poedit

There is a plugin that can do the job for you: codestyling-localization

STEPS:

1. Load a text domain for the theme.

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain('mytheme', get_template_directory() . '/languages');
}

2. Find the messages that need to be translated and process them with the appropriate WordPress function.

_e(‘Hello user’,’mytheme’);
the_content( __(‘Read more’,’mytheme’) );

3. Create language files. 3.1 The POT file contains a list of all translatable messages in our theme.

3.2 The .po file is created when we translate a POT file to a particular locale.

3.3 The .mo is a binary file that is created automatically by translation software and is not human-readable.

4. Create the POT file with help of a tool called PoEdit

4.1 Open Poedit, and create a new catalog. Fill in the project’s information in the “Project info” tab.

4.2 Provide path to the theme and it will parse all the text those needs to be translated. You need to provide the keywords we used in the theme __( and _e so it can parse and create the POT for you.

4.3 Now add the translated text for each string inside POT file and a .po file is created by setting the language code and country code as the file name.

4.4 Generate .mo file out of it. (When saving a .po file, Poedit will automatically create an .mo file, which is a binary file and is not human-readable.)

4.5 Instruct WordPress to enable the localization and to load the language files. Edit the wp-config.php file and edit this define('WPLANG', 'en_GB');


Here's how to do it in 2018:

  1. Install wp-cli on your development machine.
  2. Run wp i18n make-pot . languages/my-theme.pot in your plugin/theme folder (wp-cli i18n)
  3. Wrap your strings with gettext:

// Requires WordPress >= 4.7
__('Something');

// Compatible with WordPress < 4.7 (Roughly 15%~ of all WordPress Websites)
__('Something', 'my-theme');

  1. Run step 2 every time you add a new translatable string
  2. (Optional) Translate it locally with Loco Translate or similar to test everything is working as expected
  3. Publish your plugin/theme and translate it with https://translate.wordpress.org/

Tags:

Translation