• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / Make Your Plugin Translatable
Squares with different countries' flags painted on them

Make Your Plugin Translatable

Last updated on June 26, 2018 by Sal Ferrarello

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.

Photo Credit

Pexels

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips, Programming Tagged With: Internationalization (I18n), WordPress Plugin

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in