Multilingual WordPress plugin development is super easy and increases your target audience. At flippercode, our developers strictly follow the multilingual approach in any type of WordPress development.

Instead of defining ‘Internationalization’ or ‘Localization’ words theoretically, I’d like to develop a multilingual plugin from scratch so you can understand this perfectly.

Create “Hello World!” Multilingual Plugin

Assuming you have little idea about wordpress plugin development, to start off, below is simple plugin code which create two navigation and display a dummy output.

/*
Plugin Name: Multilingual WordPress Plugin Tutorial
Description: Learn how to make a wordpress plugin multilingual.
Author: flippercode
Version: 1.0.0
Author URI: https://www.flippercode.com
*/

if(!class_exists(‘Multilingual_Tutorial’))
{
class Multilingual_Tutorial
{
/**
* Construct the plugin object
*/

public function __construct()
{
add_action(‘admin_menu’, array(&$this, ‘multilingual_menu’));
} // END public function __construct

public function multilingual_menu()
{
add_menu_page(
‘Multilingual Tutorial’,
‘Multilingual Tutorial’,
‘manage_options’,
‘multilingual-tutorial’,
array(&$this, ‘func_multilingual_tutorial’)
);

add_submenu_page(
‘multilingual-tutorial’,
‘Sub Menu’,
‘Sub Menu’,
‘manage_options’,
‘multilingual-tutorial-submenu’,
array(&$this, ‘func_multilingual_tutorial_submenu’)
);
}

public function func_multilingual_tutorial()
{

echo “Hello, World is here”;

}

public function func_multilingual_tutorial_submenu()
{

echo “Hello, World is here”;

}

}
}

// instantiate the plugin class
$multilingual = new Multilingual_Tutorial();

If you change your WordPress language using define(‘WPLANG’, ‘hi_IN’); in wp-config.php, this plugin doesn’t have any effect because it is not multilingual compatible WordPress plugin.

Below are simple changes we’ll do in this plugin to make it multilingual.

  • Define Strings using __() or _e() Functions: Whenever you define a string or label of input element or paragraph, always use __() or _e() functions.To output, a translated string, use _e($text,$domain) function, where $domain is the key which used to fetch translated string from languages files. This is known as ‘text domains’ which should be unique for a plugin.
    _e('Multilingual WordPress Plugin Tutorial', 'text-domain');

    here our unique key is ‘text-domain’ which will be used throughout the plugin.

If you don’t want to output a translated string, use __($text,$domain) function.

$string= __('Multilingual WordPress Plugin Tutorial', 'text-domain');

So by using these two functions, we’re going to modify our plugin.

To make navigation multilingual, replace:

      add_menu_page(
        	    'Multilingual Tutorial', 
        	    'Multilingual Tutorial', 
        	    'manage_options', 
        	    'multilingual-tutorial', 
        	    array(&$this, 'func_multilingual_tutorial')
        	);

        	add_submenu_page(
		    'multilingual-tutorial',
        	    'Sub Menu', 
        	    'Sub Menu', 
        	    'manage_options', 
        	    'multilingual-tutorial-submenu', 
        	    array(&$this, 'func_multilingual_tutorial_submenu')
        	);

With :

 	add_menu_page(
        	    __('Multilingual Tutorial','text-domain'), 
        	    __('Multilingual Tutorial','text-domain'), 
        	    'manage_options', 
        	    'multilingual-tutorial', 
        	    array(&$this, 'func_multilingual_tutorial')
        	);
        	add_submenu_page(
				'multilingual-tutorial',
        	    __('Sub Menu','text-domain'), 
        	    __('Sub Menu','text-domain'), 
        	    'manage_options', 
        	    'multilingual-tutorial-submenu', 
        	    array(&$this, 'func_multilingual_tutorial_submenu')
        	);

To make contents multilingual, replace:

echo "Hello, World is here";

with:

 _e("Hello, World is here",'text-domain');
  • Loading language files: When plugin loaded, we need to load the required language file according to selected languages in wp-config.php. WordPress handles this with help of load_plugin_textdomain() function.Use ‘plugins_loaded’ action and call load_plugin_textdomain() function when plugin loaded.
     
    
    add_action('plugins_loaded', array(&$this,'plugin_init')); 
    
    function plugin_init() {
    
    		load_plugin_textdomain( 'text-domain', false, dirname(plugin_basename(__FILE__)).'/languages/' );
    
    		}

    Here assume that all languages file located in languages folder within plugin folder. So below is the complete source to make above plugin a multilingual.

    
    

Create Languages Files in WordPress

To create languages files in WordPress, we use poedit software. Here are easy steps to create a .po file for your plugin on Creating POT Files.

Below is the screenshot of .po files for English and Hindi languages.

Languages Files for wordpress
Languages Files for wordpress

Did you notice file names here? It should have {domain}-{language_code}_{country_code}.po format. Here our {domain} is ‘text-domain’, {language_code} is ‘hi’ (Hindi) and {country_code} in ‘IN’ (India) so file name is ‘text-domain-hi_IN.po’. Here is complete list of Languages Codes and Country Codes.

How to Test a Multilingual Plugin

‘English’ is the default language for WordPress but you can change its language easily in following steps. Here we’re going to convert our default language to HINDI which locale code is ‘hi’ and country code is ‘IN’.

  • Step #1 : Open wp-config.php, change define(‘WPLANG’,”) to define(‘WPLANG’,’hi_IN’)
  • Step #2 : Make sure ‘hi_IN.mo’ and ‘hi_IN.po’ exists in ‘wp-content/languages’ folder. If not exists, here you can download languages files

Note that your plugin language file located at ‘your-plugin-path/languages’ folder and wordpress translation file located at ‘wp-content/languages’

  • Step #3 : Refresh your wordpress page and you should see wordpress options in hindi language.

Conclusion

By applying some small steps, you can make your plugin or themes multilingual. The first time, you create an English version .po file and distribute that to the translator. You can place any number of languages files in your languages folder. More languages you’ll provide, more download you get on codecanyon or official repository.