Internationalizing your plugin is not difficult. All the information you need is out there and the WordPress Codex is very good resource, but I thought I’d sum it up for those who need the quick skinny on the matter.
Last weekend I learned how to internationalize my most recent plugin (Sidekick). All the information you need is out there and the WordPress Codex is very good resource, but I thought I’d sum it up for those who need the quick skinny on the matter.
Just a quick interesting sidebar in case you didn’t know, the abbreviations i18n and L10n are numeronyms for the terms Internationalization and Localization. There are 18 letters between the “i” and the “n” in the word internationalization (i18n) and 10 letters between the “L” and the “n” in the word Localization (L10n). Additionally, an uppercase “L” is used so that a lowercase “l” will not be mistaken for an “i”.
Here are the steps to quickly get your plugin internationalized:
1) Replace textual content in your plugin using the __()
and _e()
functions.
These resource should also get you started:
These are the more important things to consider during plugin/WordPress dev:
- Use a text-domain for your plugin
- Placeholders (using printf and argument swapping)
- Plurals
- Handling javascript files (inserting translations into js files)
2) Manually load the i18n text-domain in your plugin code using load_plugin_textdomain()
function (place this in the admin_init
hook)
load_plugin_textdomain('sidekick', false, basename(dirname( __FILE__ )) . '/languages');
3) Download and install Poedit. You will use it to create and edit your “.po” files. These are the files that contain the original and translated text.
4) Setup your first “.po” file (a PO file is essentially the same thing as a POT file).
Think about it this way, the PO will contain both the original and translated text (it will typically be given a standard language name: en_US, en_GB), the POT file will contain only the original text and will be used as template to hand out to translators (this file can be named something like: template.pot, default.pot).
- Open Poedit
- File > New Catalog…
- Enter in your details in the “Project info”, “Paths”, and “Keywords” tabs. This is how I have it setup:
- After you finish editing your catalog settings you will be prompted to save a “default.po” file, it is recommended that you create a [plugin-name]/languages/ folder to save all your language translations.
- Poedit will scan the paths that you set above, and search for the function keywords you’ve defined to find text that need translation.
Important when saving out the language translation files be sure to name them like so: [plugin-name]-[LANG].po (e.g. sidekick-en_US.po, sidekick-es_ES.po)
5) Adding a Text Domain: [plugin-name]
line to your plugin header, will allow WordPress to internationalize your plugin meta-data when it displays your plugin in the admin screen:
/* Plugin Name: Sidekick Plugin URI: http://farinspace.com/wordpress-sidekick-plugin/ Description: Used to enhance the WordPress admin interface. It is your <a href="http://en.wikipedia.org/wiki/Lightsaber" target="_blank">lightsaber</a> when battling administrative tasks. Author: Dimas Begunoff Version: 0.4.2 Author URI: http://www.farinspace.com/ Text Domain: sidekick */
In your PO/POT file you can then add, for example, your description like so:
#. Description of the plugin/theme msgid "Used to enhance the WordPress admin interface. It is your <a href=\"http://en.wikipedia.org/wiki/Lightsaber\" target=\"_blank\">lightsaber</a> when battling administrative tasks." msgstr ""
6) Testing it all out is pretty straight forward, edit your wp-config.php
file, change WPLANG
to a language file that you’ve created:
define ('WPLANG', 'es_ES');
Cool, should be useful for us. Would it be possible to integrate the plugin with http://crowdin.net instead of using poedit (or how hard it is to do it by oursevles)?
crowdin looks interesting, thanks for the link.
I would also consider https://poeditor.com/ as an alternative to poedit…