As we watch the number of non-English WordPress installations grow faster than English installations, the value of making your plugin “translation ready” grows as well. In order to make your plugin translatable (this is also know as Internationalization or I18n
), there are three things you need to do.
1. Use Translation Functions
Whenever you use a string in your plugin run it through a translation function, the easiest to use being __()
.
Instead of
# Do NOT do this.
$btn_txt = 'Get Raptor';
run the text through the __()
function first.
$btn_txt = __(
'Get Raptor', // Text to translate.
'fe-raptor-button' // Plugin slug typed out. Not a variable.
);
Note: Your second parameter, the plugin slug, must be a string typed out. It can not be a variable. This is because the programs that make translations scan through your files without parsing variables.
Note: If you’re concerned about ambiguity in the text causing problems for the translator, add a comment directly before the line being translated that begins with /* translators:
.
e.g.
/* translators: this text appears on the button that triggers displaying the raptor */
$btn_txt = __(
'Get Raptor', // Text to translate.
'fe-raptor-button' // Plugin slug typed out. Not a variable.
);
2. Modify your plugin header comments.
Add Text Domain and Domain Path to Your Plugin Header
* Author: Sal Ferrarello
* Author URI: http://salferrarello.com/
* License: Apache-2.0
* License URI: https://spdx.org/licenses/Apache-2.0.html
* Text Domain: fe-raptor-button
* Domain Path: /languages
Text Domain
The Text Domain
should be the same as your plugin slug (this is the name of your plugin’s directory).
Domain Path
The Domain Path
should always be /languages
.
3. Load Your Plugin Text Domain
Add the following code to the root file of your plugin (in my case this would be fe-raptor-button.php
). The first parameter for load_plugin_textdomain() is your plugin slug.
NOTE: This code uses an anonymous function and the PHP special value __DIR__
both of these are not available in PHP versions less than 5.3
. I do not work with any PHP version lower than this but if you need to, the following code will need to be modified.
add_action('plugins_loaded', function() {
load_plugin_textdomain( 'fe-raptor-button', false, __DIR__ );
});
Other Notes
For more information on internationalizing your code, see the WordPress International Information for Developers.
Leave a Reply