How to Build a Multilingual Plugin for WordPress

Posted on 18th June 2023

Introduction

WordPress is a content management system (CMS) that enables you to create a website or blog from scratch, or to improve an existing website. It is a free and open source software released under the GPL.

WordPress is used by millions of people around the world, in over 70 languages. While the software is available in so many languages, not all plugins and themes are translated into all these languages. This can be a problem for users who want to use WordPress in a language that is not fully supported.

In this article, we will show you how to build a multilingual plugin for WordPress. This plugin will enable you to translate your plugin or theme into any language, using the WordPress API.

What You Will Need

  • A text editor
  • A working installation of WordPress
  • Basic knowledge of PHP

Creating a Plugin

All plugins for WordPress are stored in the wp-content/plugins directory.

Create a new directory inside the plugins directory and name it my-multilingual-plugin.

Inside the my-multilingual-plugin directory, create a new file and name it my-multilingual-plugin.php.

Open the my-multilingual-plugin.php file in your text editor and add the following code:

<?php
/*
Plugin Name: My Multilingual Plugin
Plugin URI: https://example.com/
Description: This plugin enables you to translate your plugin or theme into any language.
Version: 1.0
Author: John Doe
Author URI: https://example.com/
License: GPLv2 or later
Text Domain: my-multilingual-plugin
*/

This is the basic structure of a WordPress plugin. The first part is the plugin header, which is used to provide information about the plugin. The second part is the actual code of the plugin.

In the plugin header, we have specified the following:

  • Plugin Name: This is the name of the plugin. It should be unique.
  • Plugin URI: This is the URL of the plugin website. It is optional but it is a good idea to include it.
  • Description: This is a short description of the plugin. It is also optional but it is a good idea to include it.
  • Version: This is the version number of the plugin. It is required.
  • Author: This is the name of the author of the plugin. It is required.
  • Author URI: This is the URL of the author’s website. It is optional but it is a good idea to include it.
  • License: This is the license under which the plugin is released. It is required.
  • Text Domain: This is the unique identifier for the plugin. It is required.

After the plugin header, we can start writing the actual code of the plugin. In this case, we will use the load_plugin_textdomain() function to load the plugin text domain.

This function takes two arguments: the text domain and the path to the directory where the translation files are stored.

The text domain should be the same as the one specified in the plugin header. The path to the directory can be either absolute or relative.

In our case, we will use a relative path and specify the directory where our plugin is stored:

load_plugin_textdomain( ‘my-multilingual-plugin’, false, dirname( plugin_basename( __FILE__ ) ) . ‘/languages/’ );

This code will load the plugin text domain and look for translation files in the languages directory inside our plugin directory.

Creating Translation Files

Translation files are stored in the languages directory and they use the .mo extension.

The .mo file is a binary file and it contains a table of all the strings that need to be translated, as well as the translated strings.

To create a .mo file, we first need to create a .po file. The .po file is a human-readable text file and it contains all the strings that need to be translated.

We can use a tool like Poedit to create and manage .po files. Poedit is a free and open source software available for Windows, macOS, and Linux.

Once you have installed Poedit, open it and click on the Create new translation button.

In the Project info tab, specify the following:

  • Project name: My Multilingual Plugin
  • Project ID: my-multilingual-plugin
  • Source code charset: UTF-8
  • Translations charset: UTF-8
  • Source code path: /path/to/wordpress/wp-content/plugins/my-multilingual-plugin
  • Language: Choose the language you want to translate the plugin into

In the Translation memory tab, you can specify a .po file that already contains translations for some of the strings. This can be useful if you are translating a plugin or theme that has already been translated into another language.

Click on the Create button to create the .po file.

The .po file will be opened in Poedit and it will contain all the strings that need to be translated.

Translate the strings into the chosen language and then click on the Save button. Poedit will automatically create the .mo file.

Repeat the process for each language you want to translate the plugin into.

Loading Translation Files

Once you have created the translation files, you need to load them.

First, you need to specify the location of the translation files. You can do this by setting the load_textdomain() function.

This function takes two arguments: the text domain and the path to the directory where the translation files are stored.

The text domain should be the same as the one specified in the plugin header. The path to the directory can be either absolute or relative.

In our case, we will use a relative path and specify the directory where our plugin is stored:

load_textdomain( ‘my-multilingual-plugin’, dirname( plugin_basename( __FILE__ ) ) . ‘/languages/’ );

Next, you need to specify the language in which WordPress should be loaded. You can do this by setting the WPLANG constant in the wp-config.php file.

The value of the WPLANG constant should be the language code of the language you want to use. For example, for French, the language code is fr_FR.

If you want to use the default language, you can set the WPLANG constant to an empty string.

define( ‘WPLANG’, ” );

Creating a Plugin Options Page

In order to create the plugin options page, you need to use the add_options_page() function.

This function takes four arguments: the page title, the menu title, the capability, and the function to be called.

The page title is the title of the page that will be displayed in the browser window. The menu title is the title of the menu item that will be added to the WordPress admin menu.

The capability is the user role that is required to access the page. The function is the name of the function that will be called when the page is loaded.

In our case, we will use the following code:

add_options_page( ‘My Multilingual Plugin’, ‘My Multilingual Plugin’, ‘