A few days ago, I wrote how to add the translation files to your child theme. If you use the theme which doesn’t come with the translation files for your language, you should check this post. Today I gonna talk about how to add the translation files to your plugin. There is one different thing from the theme translation files which is the translation file name. Okay, let do this.
Where the translation files will be stored
We will create the translation file in /languages in our plugin. The languages folder is the default language path that WP will look for the translation files.
For example, If you have the plugin stores in the my-plugin folder. The plugin structure will be:
my-plugin
|-my-plugin.php
|-languages
Create the POT file
In my situation, I build my own plugin in order to add the new feature to the site. The site has two languages which are English and Thai. So I need to add the translation files for both sites and the translation files must work on both WP backend and frontend. To do that, we will create the POT(Portable Object Template) file. We will use POEdit tool. It is free. You can use any tools you like, if you don’t have any, let’s download and install POEdit on your computer.
Now you should have the POEdit ready to use. Next, follow the steps below.
- open POEdit
- click File then choose New
- choose the language you want to translate from the dialog box. In my case, Thai(Thailand) which is th_TH code.
- on the screen, choose Extract from sources then the Catalog properties dialog box will pop up.
- at the Translation properties, add the project name and version, and language if you don’t choose from the step above. Click at the Sources paths tab, you will see the red message said “Please save the file first. This section cannot be edited until then”. Meaning we need to save the file before continue. Let press OK button and press Save at the toolbar. Then chooses the language folder you want to save the translation file to. Name the file as my-plugin-th_TH.po
- after that, you will see the PO file created in your languages folder
- next, choose Extract from sources then the Catalog properties dialog box will pop up again
- now, at Sources paths, you will see the base path is /languages. click at the Plus icon under the Paths textarea to add our plugin folder path
- next, at Sources keywords, add the __ then add another _e which both are the WP translation function so the POEdit can find your source with these functions for translation
- finally, press OK button, POEdit will collect all source text for the new translation.
- then you will see the screen that you can add the translation for your language.
- press Save at the toolbar then you will see the MO file created in your languages folder
- remember, I mention above that the translation file name for the plugin is different from the theme translation file name. We need to add the plugin name as the prefix for the PO file. In my case, I have th_TH.po and th_TH.mo in my languages. My plugin folder is my-plugin. So finally my PO file will name my-plugin-th_TH.po. And I need to compile the PO file to MO file for the new name. So I open PO file in POEdit and just save it. Then I will have my-plugin-th_TH.mo.
- now I just delete the th_TH.mo. so my plugin folder is clean.
Why do I need to compile the MO file again? Because the MO file is the computer reader. The PO file is for the human reader. If the PO file changes, you need to compile to the machine language so the machine can read.
Load your translation file when the plugin loads
Now, we will add the code that loads the translation file into your plugin. Add the code below into your main plugin file. For example, my-plugin.php follows the plugin structure above.
<?php
// for the plugin
add_action( 'init', 'myplugin_load_textdomain' );
function myplugin_load_textdomain() {
load_plugin_textdomain(
'my-plugin',
false,
basename( dirname( __FILE__ ) ) . '/languages' );
}
?>
For the theme, the code will store in the functions.php of the current theme. And the code will be below.
<?php
// for the theme
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup(){
load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
?>
The Result
Now if you have the command below. The translation text will work. notice that my-plugin is your plugin folder name.
<?php
echo __( 'translate text', 'my-plugin' );
_e( 'translate text', 'my-plugin' );
?>
useful link: Display translated text
You can use the text domain for the plugin as same as the theme uses. Instead of using my-plugin, you can replace it with your text domain of the plugin. It can be found at the main plugin file in the top comment section.
NOTE
Using Free or Paid Plugins
It is too much work for you, you can use the plugins below.
- Loco Translate (free and paid version)
- WPML (paid version)
- Polylang (free and paid version)
- TranslatePress (free and paid version)
That’s all for today.