What is the best way create a plugin that is translation ready?
It doesn’t have to be translated from the beginning but it has to be easily translatable so fellow developers from different cultures can participate to the localization process of the plugin.
1. Write with localization in mind
Don’t use
echo
orprint()
to produce text output, instead use the WordPress functions__()
and_e()
:_e()
and__()
will provide the translation — in the current language — of the text provided as the first parameter._e()
will output the text whereas__()
will return it.The second parameter is the text domain, you will be using it to tell WordPress that the text provided as the first parameter belongs to this plugin, you can use any name you want but I prefer to use the same name as I used for the plugin file of directory, I find it more intuitive.
How to output dynamic text like: “Hello <username>”?
With
__()
andsprintf()
:2. Prepare the .pot/.po/.mo files
Definitions
Open Poedit and create a new catalog (File › New Catallog…) with these settings:
.
..
, (we will store language file in a plugin subdirectory called languages)__
and_e
Save the catalog as
/my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin.pot
and scan your plugin files for translatable text by pressing the update button. When the update is finished close that catalog, you won’t need to update that file unless you add new translatable strings (i.e enclosed in__()
or_e()
) to your plugin.Now let’s create the first translation (I will use fr_FR):
Using Podeit, create a catalog from a POT file
(File › New catalog from POT file…):
Save the catalog as
/my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin-fr_FR.po
. Translate some or all the of the strings, save the .po file again, upload both the .po and .mo files.Note that whenever you save a .po file a .mo file is generated with the same name, the filename of the .po file is crucial, it’s composed of the concatenation of the plugin text domain (my-plugin) and the language locale (fr_FR), always name your .po files for plugins like this: [textdomain]-[locale].po, here are some examples:
wpcf7-it_IT.po
wpcf7-pt_BR.po
wpcf7-ar.po
… Yes!Whenever the plugin is updated with new text, update the po file, translate new strings and reupload the .po and .mo files
3. Instruct the plugin to load translated text for the current language
Somewhere in your plugin, you must tell WordPress to use your .mo file, you can do it by using this code in the beginning of your plugin file:
Replace
my-plugin
with your plugin name in the 1st and 3rd parameter of theload_plugin_textdomain
function.4. Test and troubleshoot
Some reasons it may not work:
_e('my text')
with_e('my text', 'my-plugin')
)Nabilâs answer is fairly complete but there is an easy variation provided:
Your plugin is on the WordPress.org plugin repository
Youâre willing to require that your plugin only work with WordPress 4.6 or higher.
The steps are these:
In your pluginâs readme.txt file, add
Requires at least: 4.6
. See https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/if it isnât already, upload your plugin to the WordPress plugin repository. See https://wordpress.org/plugins/developers/add/.
Find your pluginâs slug/text domain. To do that, go to your pluginâs page on the WordPress plugin repository. The URL will be like https://wordpress.org/plugins/your-plugin-slug/. That last part of the URL, âyour-plugin-slugâ, is your pluginâs slug. Thatâs what you use for the translation functionsâ text domain.
Use WordPressâ translation functions in your plugin (like
__e(âhelloâ, âmy-plugin-domainâ);
). Just make sure to use the correct plugin text domain, acquired in the previous step. See https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/ for more info.If you do the above steps, WordPress will take care of:
(Answer from my blog post here: https://cmljnelson.blog/2019/01/01/the-really-lazy-way-to-translate-a-wordpress-plugin/)