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