Today I Learned - How to POT
Published on 2018-01-03 11:14
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:
* Text Domain: my-theme
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:
* Domain Path: /languages
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. :
__('Some text', 'my-domain') _e('Some text', 'my-domain')
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:
brew install gettext
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:
php path/to/makepot.php my-domain path/to/your-theme-directory
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.
- retrieves translated text🔼1
- displays translated text🔼2