Wordpress make your own plugin multilingual

Categories: 

Tags: 

Summary

  • Step 1: make some string in your plugin translatable
  • Step 2: load the function with add_action 'init' in your plugin
  • Step 3: create folder and file for translations inside your plugin
  • Step 4: update your language files with poedit

In the example, we are going to translate the plugin to Spanish.

  • Plugin name: my_plugin
  • Path: wordpress/wp-content/plugins/my_plugin
  • Language file: wordpress/wp-content/plugins/my_plugin/languages/my_plugin-es_ES.po
  • Note: the language file should be called like the plugin name plus '-xx_XX', the code for each language. In this case, '-es_ES' for Spanish.

Step 1: make some strings in your plugin translatable

  • We can use Wordpress functions like '__' to make something translatable. Example for strings 'Father' and 'Teacher':
function change_role_name() {
    global $wp_roles;
 
    if (!isset($wp_roles)) {
        $wp_roles = new WP_Roles();
    }
 
    //You can list all currently available roles like this...
    $roles = $wp_roles->get_names();  
 
    //You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
    $wp_roles->roles['parent']['name'] = __( 'Father', 'my_plugin');
    $wp_roles->role_names['parent'] = __( 'Father', 'my_plugin');           
    $wp_roles->roles['teacher']['name'] = __( 'Teacher', 'my_plugin');
    $wp_roles->role_names['teacher'] = __( 'Teacher', 'my_plugin');           
}
add_action('init', 'change_role_name');

Step 2: load the function 'load_plugin_textdomain' with add_action 'init' in your plugin

/**
 * Action: init
 */
function my_plugin_action_init()
{
    // this will search language files in 'wordpress/wp-content/my_plugin/languages'
    load_plugin_textdomain('my_plugin', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
add_action('init', 'my_plugin_action_init');

Step 3: create folder and file for translations inside your plugin

<my_plugin-es_ES.po> file example without any strings translated

# Translation of Development in Spanish (Spain)
# This file is distributed under the same license as the Development package.
msgid ""
msgstr ""
"Project-Id-Version: My Plugin \n"
"POT-Creation-Date: 2014-09-20 11:54+0100\n"
"PO-Revision-Date: 2014-09-20 11:55+0100\n"
"Last-Translator: Guillermo Cerezo Somera \n"
"Language-Team:  \n"
"Language: es_ES\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 1.5.4\n"
"X-Poedit-Basepath: ../\n"
"X-Poedit-KeywordsList: __;_;_e;_ex;_x;_n\n"
"X-Poedit-SearchPath-0: .\n"

Step 4: update your language files with poedit

We don't really need to create from scratch the '.po' language file, we should use http://poedit.net/ to do it. It's really easy update your language files with it, only need to open the file 'my_plugin-es_ES.po' with poedit, click in update strings from source code and automatically the news strings will appear to be translated. Just save the file when finished and Wordpress will do the rest.

After that, the file 'my_plugin-es-ES.po' will appear with the new translated strings:

#: my_plugin.php:47
msgid "Father"
msgstr "Padre"
 
#: my_plugin.php 48
msgid "Teacher"
msgstr "Profesor"