up one level
How to Internationalize and Localize a WordPress Plugin
By w̶i̶e̶l̶d̶l̶i̶n̶u̶x̶.̶c̶o̶m̶ author Morgan Jassen
Applies to: WordPress v.4.7.2
This post is a step-by-step guide how to internationalize and localize (internationalize and translate) a WordPress Plugin.
I was inspired by this forum topic: "Language translation" https://wordpress.org/support...translation-26.
When troubleshooting the issue, at first it seemed that the plugin was already internationalized. But looking closer it was symptomatic -- wasn't working. I wished that I had a very simple, "Hello World"-style, internationalized and localized, WordPress Plugin to see what is the minimum that is needed to make it work. Resources that I used are linked at the bottom of this article -- I'm glad for them, as I needed them to figure this out, so thanks to the authors and publishers of those resources.
So here it is -- step-by-step how to add internationalization and localization to a WordPress Plugin.
As a starting point, we'll be using the "mzz-helloworld" WordPress plugin.
Download, install, and activate the "mzz-helloworld" plugin. (https://github.com/mjassen/mzz-helloworld).
Create a folder called "languages" in the plugin's root directory, like so:
In ...wp-content/plugins/mzz-helloworld/mzz-helloworld.php, immediately below the Plugin Header, (so on line 8 in our case, add a "...textdomain..." line, like this:
load_plugin_textdomain('mzz-helloworld', false, plugin_basename(dirname(__FILE__)). '/languages');
In ...wp-content/plugins/mzz-helloworld/mzz-helloworld.php, on line 12 in our case, add a call to the "__()" function, with the string as the fist parameter and the textdomain ("mzz-helloworld" in our case) as the second parameter. So, before, it was like this:
echo "Hello, Mzz World!";
, but after, it looks like this:
echo __("Hello, Mzz World!", 'mzz-helloworld');
Great! We're done internationalizing the plugin. In other words, the code is ready. Now it's time to move ahead and localize (translate) the plugin strings. We're deciding to localize this plugin into Brazilian Portugese language. So next, we'll create new .po and .mo files.
(Step 5 is a long one, with multiple screenshots)
We're creating new .po and .mo files that will hold the localizations (hold the translations).
- We can create the .po file from a blank .pot file. Get a blank new .pot file. For example, download a copy of the "Blank-WordPress.pot" file from https://raw.githubusercontent.com/fxbenard...pot. Save the "Blank-WordPress.pot" file somewhere convenient like on your desktop.
- Then Launch Poedit program and choose "File" -> "New catalog from POT file...", browse to that "Blank-WordPress.pot" file. Opening the .pot file will have caused the "Settings" screen to appear, populated with default settings. Click OK.
- In the "Save as..." window that comes up next, name the new file "mzz-helloworld-pt_BR.po", and save it into the /languages folder, like so:
(we use the "pt_BR" code because we've decided we want to translate the plugin to Brazilian Portugese.)
- When we save/create the .po file, Poedit program automatically creates the corresponding .mo file for us.
- Next, still in Poedit program, from the menu choose: "Catalog" -> "Update from sources". The program goes and finds all the strings that we had marked with "__()" in the previous step.
Next, we'll translate the text strings. In Poedit program, type the corresponding translation for each line. In our case, we only have one line, "Hello, Mzz World!". So we'll type the translation in there as "Olá, Mzz World!". Click Save, and close Poedit.
That's It! Congratulations, we're done localizing the plugin, which means we're done internationalizing and localizing it.
A final tip, to see the the plugin localized it in our language, of course, we'll need to have told WordPress to use Brazilian Portugese as the Site Language.
- In the WordPress Admin Dashboard, set "Settings" -> "General" -> "Site Language" to be "Português do Brasil", and click "Save Changes".
In conclusion, this post has been a step-by-step guide how to internationalize and localize (internationalize and translate) a WordPress Plugin.
2017-01-30 Update: Here is a link to the final (internationalized and localized) version of the mzz-helloworld plugin:
* "Import users from CSV with meta" (WordPress Plugin) (This plugin is internationalized and localized in a way that works, so I was able to learn from it) https://wordpress.org/plugins/import-users-from-csv-with-meta/
* Professional WordPress Design and Development (book) (Chapter 8 - Plugin Development) , by Damstra, Stern, and Williams. (Wrox; 3 edition (January 12, 2015)) ISBN-13: 978-1118987247 (https://www.amazon.com/Professional-Word...7241)
[2019-03-11 edit: Moved to: https://investorworker.com/2017/... .html.]