Posted by José Lopes.
This post explains how to create for Wordpress plugins the language base file (.POT) and the specific files for each language version (.PO and .MO), in order to provide the plugins internationalization.
The Wordpress already explains how to create them but I feel that the explanation is not straight forward and its spread by to many links, so I hope have it all here.
Assuming that you wrote your plugin and you want to have it internationalized, you must have before anything:
if(function_exists('load_plugin_textdomain'))
load_plugin_textdomain('PLUGIN NAME','wp-content/plugins/PLUGIN DIRECTORY');
__("TEXT TO TRANSLATE","PLUGIN NAME")
These two details are crucial in order to generate the language files and to help Wordpress finding automatically the correct version in accordance with the user definitions.
To generate the .POT file, using a Linux system, just write on a shell:
xgettext PLUGIN NAME.pot
The .POT file created has the following look:
# translation of hideshowcomments.po to # Jose Lopes <jose.lopes@paxjula.com>, 2007. msgid "" msgstr "" "Project-Id-Version: hideshowcomments\n" "POT-Creation-Date: 2007-09-18 10:42+0100\n" "PO-Revision-Date: 2007-09-18 02:33+0200\n" "Last-Translator: Jose Lopes <jose.lopes@paxjula.com>\n" "Language-Team: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "X-Poedit-KeywordsList: __;_e\n" "X-Generator: KBabel 1.10.2\n" #: hideshowcomments.php:42 msgid "Click here to see or add comments" msgstr ""
Has you can see on the final lines, this file will have for each line where we set the text for translation, in this case for the hideshowcomments plugin, the default english version at msgid and the space for the new language version at msgstr.
To create a new language version you start from this file, edit it with KBabel for instance, and rename it like:
PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.po
We should use the standards for the language_country codes (as example: pt_PT for portuguese of Portugal and pt_BR for portuguese of Brasil).
For more information about the language_country codes check:
Once translated we can create the .MO file, writing on a shell:
msgfmt -o PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.mo PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.po
While distributing the plugin the .MO file of each language should be included as well as the .POT file, allowing others to translate into other languages.