generating po mo translating files from scratch in a wordpress theme

The question:

Maybe I’m missing something here but I really can’t find any answer to my question around the web.

I am creating a new wordpress theme from scratch (literally from scratch) and would like to create languages folder with PO and MO file in it.

I would like to generate this translation MO file from scratch

I was looking for a good tutorial that will get me started and let me understand the whole procedure but all the good tutorials I found all talking about how to translate strings that are already exist.. meaning that a MO file exists already, the strings are already in it and I need to translate them.. that’s pretty easy but that’s not what I want.

I would like to generate something from scratch using my own custom strings..
+ I want to always be able to add more strings and compile it again to update the PO file.

can anyone send a good tutorial of how o do it from scratch?
am I missing something here?

Thanks a lot

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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');

Method 2

  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. Choose a unique identifier, I chose my-theme as an example, wrap your strings using the __() function:

__('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/

Method 3

I’m using eazypo to generate MO files for wordpress themes and plugins.

Nice and free software.
check their website, they have a guide.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment