Today I Learned – How to POT

German flag in a circle turns into Czech flag in a circle as it travels from left to right and back

Since I am at the moment between jobs I can share what I am learning publicly. Today it’s basics in internationalization and localization.

For context I do plan to publish in two more languages and my platform is WordPress, so anything I’ll write is skewed that way.

First step – Internationalization

In WordPress there is a thing called text domain which decides what will be translated and why. This is important because you can have plugins and themes by different people and certain words and phrases can be translated a little bit differently. So when you want your strings to be translated in a certain way you have to specify a domain.

Text domain for themes is defined in the comments of styles.css: and in case of plugins in the comments of main PHP file:

The default folder where WordPress will look for language files is /languages folder. If you choose to use a different location you need to specify it at the same place as Text Domain as:

Now that we have set up text domain, we need to change all hard coded strings to functions with those strings and text domain as parameters. The easiest way is to use either  __()1 or  _e()2 :

It’s important to hard code the text because tools which help generate .pot file usually don’t execute PHP to learn whether the string should be included in translation or not.

Step two – Localization

When you start with localization you’ll first create a pot file. Easy way is to buy Poedit which will analyze your WordPress theme or plugin and will create a .pot file for you.

The hard, my, way was to use WordPress’ tool for creating .pot file. For that I had to install Gettext through homebrew first:

And download WordPress trunk from SVN. Downloading WordPress from wordpress.org isn’t unfortunately enough because it doesn’t have there development files.

After that I ran command:

Which creates a .pot file in your current directory. After that you can use free version of Poedit to create .po files and compile them to .mo files.

Happy translating!

  1. retrieves translated text
  2. displays translated text