How to Make a WordPress Multilingual Plugin
Making WordPress multilingual plugins may sometimes make our developers confused. But in reality, it is easier and most importantly it will increase your customers from the different locality of the word.
Honestly, I have searched multiple websites to find out how anyone can make a multilingual plugin. Sadly, didn’t find enough data to create this blog. After that, I talked with a senior in-house developer and he suggested me forum where I got the exact answers. Hopefully, it will help you from the scratch.
So devs, let’s learn the process together.
An Easy Guide to Make Your WordPress Plugin Multilingual
Here we will show you the setting process including some essential codes. Watch out-
1. Before Writing Your Code, Keep Localization in Mind
To produce text output, avoid using echo
or print()
. Rather use WordPress functions __()
and _e()
:
/** Not localization friendly */
echo "Welcome to my plugin";
// OR
print("Welcome to my plugin");
/** Localization friendly */
e('Welcome to my plugin', 'my-plugin'); // OR $my_text = _('Welcome to my plugin', 'my-plugin');
echo $my_text;
Here, you will get the translation in the current language from _e()
and __()
as the first parameter of the text provided. _e()
will output the text whereas __()
will return it.
text domain is the second parameter. With this parameter, you will inform WordPress that the text from the first parameter relates to this plugin.
You are independent to use any name but expert developers prefer to use the same name. It’s more spontaneous.
How to output dynamic text like: “Hello <username>”?
With __()
and sprintf()
:
/** Get the username */
$username = 'Magictrick';
/** Not localization friendly */
echo "Hello $username";
/** Localization friendly */
printf(__('Hello %s', 'my-plugin'), $username);
// OR
$my_text = sprintf(__('Hello %s', 'my-plugin'), $username);
echo $my_text;
2. Prepare the .pot/ .po/ .mo Files
Before talking about these files let’s know what are they-
The .pot File
Plugin developers use to put this file at their disposal. To create new translations, it uses as a starting point. WordPress doesn’t use this file.
A .po File
This is a translation file that a developer has started and may be completed. Like the .pot file, WordPress doesn’t utilize it.
A .mo file
When you save a .po file this file is generated by Poedit automatically. You can upload or re-upload these files when you update or create a .po file.
.mo files provide the translation to WordPress
So, now we have to create a new catalog by following some settings below. First of all, open Poedit. Now follow the settings-
Project Infomation
Here, you have to use your information as a developer (or your team). Don’t forget to match the plugin default language and your language & country.
Paths
- Base path:
.
- Paths: remove all & add
..
, (we will store language file in a plugin subdirectory called languages)
Keywords
remove all & add __
and _e
Now, keep the catalog as /my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin.pot
. Then press the update button to scan your plugin files for translatable text.
Close the catalog when the update is done. The update is needed when you are about to add new translatable strings (i.e enclosed in __()
or _e()
) to your plugin.
Create the First Translation (we will use fr_FR)
Firstly, from a POT file create a catalog by using Poedit.
Project Information
As discussed above use your (or your team) information. And, change the language & country. We will use French and France.
Paths
Don’t change
Keywords
Don’t change
Now, save the catalog as /my_wordpress_blog/wp-content/plugins/my-plugin/languages/my-plugin-fr_FR.po
. Make some or all of the strings
Make some or all of the strings translated and save the .po file again. Then, upload both the .po and .mo files.
Remark that a .mo file is generated with the same name when you save a .po file. The filename of the .po file is crucial. Because it’s composed of the concatenation of the plugin text domain (my-plugin) and the language locale (fr_FR).
Don’t forget to name the .po files for plugins like- [textdomain]-[locale].po
. Below we have given you some examples-
- Italian/Italy:
wpcf7-it_IT.po
- Portuguese/Brazil:
wpcf7-pt_BR.po
- Arabic:
wpcf7-ar.po
When your plugin will get updates with the new text you have to-
- Update the .po file
- Translate new strings
- Reupload the .po and .mo files
3. Instruct Your plugin to Load Translated Text for the Current Language
You should tell WordPress to utilize your .mo file and it must be somewhere in your plugin. Here, we have shared the code which you can use in the beginning of your plugin file:
function my_plugin_init()
{
load_plugin_textdomain( 'my-plugin', false, 'my-plugin/languages' );
}
add_action('init', 'my_plugin_init');
Replace my-plugin with your plugin name in the 1st and 3rd parameters of the load_plugin_textdomain function.
If you want to translate your plugin you can read this blog- Simplest Way to Translate any WordPress Plugin.
4. Test and Troubleshoot
There are some reasons for which the entire process may not work. Into the .pot or .po file, strings are not imported because of wrong catalog settings (path or keywords or both).
Text is not translated on the WordPress site
- .mo file for that language missing or has a wrong file name
- Text domain not used (replace _e(‘my text’) with _e(‘my text’, ‘my-plugin’))
- Text domain not loaded (use example above with the right parameters, WP will not warn you about mistakes)
FAQs
1. Which is the Best Multilingual Plugin for WordPress?
Answer: There are different types of Multilingual plugins. Here we will like to mention 5 all-time best Multilingual plugins-
WPML
Loco Translate
Polylang
Weglot
Translate WordPress with GTranslate
2. How can You Create a Multilanguage Plugin for WordPress?
Answer: Navigate to Appearance> Widgets. Now, add the language switcher widget to your sidebar. Here, you can use language names with flags. After doing all these things just click the save button and you will store your widget settings. After that, preview your site to check if the language switcher works or not.
3. Are Multilingual Sites Allowed in WordPress?
Answer: Bilingual or multilingual sites are not supported by WordPress. You will find different WordPress plugins in WordPress.org. These plugins will build a multilingual blog effortlessly.
4. How can You Add Multiple Languages to Your Website?
Answer: There is the easiest way to add multiple languages. You can take the Google Translate support which is free of cost. To enjoy this support, simply sign up for an account. Now, paste a small bit of code into the HTML.
5. What Languages are WordPress Plugins Written in?
Answer: PHP programming language is used in WordPress plugins. So, if you want to be a developer you must have good knowledge of PHP.
Final Words on WordPress Multilingual Plugin
Programming languages have not yet started to support localization. So, as a plugin developer, you have to convert the language to grasp your targeted audiences.
Making your WordPress multilingual plugin doesn’t only help to connect with your potential customers but also assist you in better Search Engine Optimization (SEO)
If you have tried the way to make your plugin multilingual don’t forget to share your experience with us.
Subscribe To Our Newsletter
Don’t miss any updates of our new templates and extensions
and all the astonishing offers we bring for you.