Further develop your plugin by internationalizing it for translations. Building plugins that support localization is considered best practice and allows your plugin to be used by many more users, from just about anywhere in the world.
- [Voiceover] Internationalizing your plugin means that it will be ready for users to translate into their own language. Translating, or localizing plugins is an excellent way to get more users and diversify your development skills. In this video, we continue building our plugin by making sure it's ready for localization into other languages. The same basic steps covered in this video can be used to internationalize any WordPress plugin. There are many ways to approach internationalization. The topic is deep and wide, with lots of nuances, variables, tools, and techniques.
Here are the general steps to internationalize any WordPress plugin. Let's start with phase one. While building our plugin in the previous videos, we've already done all steps for phase one. Here is step one, the languages folder. Here is step two, the matching slug name for the plugin folder ... And main file. And, for step three, our file header includes both text domain and domain path. They're included here in the main plugin file and in the readme.txt file.
Also, this is important: the text domain ... must match the plugin folder and main plugin file. So, this, this, and this must all gel together. Also important: the domain path must point to the languages folder. So, that takes care of everything for phase one. Next, we have phase two. For this phase, we need to add a function to load the text domain, and, then, go through the plugin and replace any text strings with a localization function.
Let's do each of these steps. First, we want to add the function that loads the plugin text domain. To save time, we can grab this function from the exercise files for this course. And, then, we add it to the plugin file, like so. This function is registered with the plugins loaded hook. And inside we are using the load plugin text domain function to do all the work.
As long as this function is included in the main plugin file, as we have done here, we don't have to change anything, except for the first parameter. This specifies the plugin's text domain, and it should match the text domain specified in the file header. These have to match up in order for this function to work properly. For the next step, we do the grunt work of adding localization functions to anything that can be translated. For example, here in the main plugin file, we find some text strings in the default options function.
Let's go ahead and prepare these strings for localization. Now, each of these three text strings are passed through the double underscore function. So, the first parameter is the string that we want to translate. And the second parameter ...
Specifies the plugin's text domain. Note that we only need to modify strings that may be read or translated by the user. So, we don't need to change any of the other strings. Because they won't be seen by the user. This file is now internationalized. To complete this step, we would continue going through the plugin, and replace any readable strings with the proper localization functions, as we have done here.
We've already seen the double underscore function. There's also an underscore 'E' function that echoes the string, instead of returning it. And the underscore 'X' function provides an extra parameter that can be used to specify context. These functions are commonly used, but there is a safer way to localize strings. These localization functions provide builtin sanitization for the translated string, which helps keep translations correct. And it also stops translators from running malicious code.
So, with that in mind, let's return to the plugin and replace the double underscore functions with the escaping functions. These text strings will be displayed in markup, so we use the escape html versions of the double underscore functions. Escape html, escape html. And escape html ... For each of our three strings. To save everyone time, I've went ahead and added localization functions to all readable text strings in our plugin.
So, let's remove this version of the plugin and grab the complete version from the exercise files for this video. And, then, install it in the plugins directory. In this version of the plugin, all text strings are properly prepared for translation. Especially for larger plugins, it can be tedious going through each string and adding the right localization function. Ideally, it's better to get in the habit of adding the localization functions sort of as you go, as you build the plugin.
So, at this point, we have completed everything in phase one and everything in phase two. So, that leaves us with the third and final phase: generating the default translation file, AKA the 'P'-'O'-'T,' or POT file. The POT file basically is a template that translators will use when translating your plugin. There are many tools for generating POT files, for example, Poedit, which is a standalone app. Or, if you prefer a WordPress plugin, try Loco Translate.
There also are many other plugins and apps available for you to check out. So, now, let's complete the internationalization of our plugin by generating the default POT file. I like to use Loco Translate. This plugin is super easy, and you can use it within the WordPress admin area. Here is our plugin on the translate plugins screen. Here, we can create a new POT file by clicking create template.
Notice the path where the POT file will be saved. And that's exactly correct according to our plugin's file header. So let's go ahead and click the button to make it so. Okay, the template file has been created. And here it is. Our plugin now has its own POT file located in the languages folder. Don't worry about the contents of this file. You'll never need to edit it directly.
Now that our plugin is fully internationalized, we can take a moment to debug. To do so, we return to Loco Translate and click the debug tab. And here we see that everything looks good. No errors or issues. You can explore further information under these tabs here. And, so, that's all there is to it. We are now done internationalizing our plugin. As explained, this video provides a basic overview of the process. To go more in-depth, check out the video course "WordPress: Internationalization" by Carrie Dils.
- WordPress APIs
- Action and filter hooks
- Activating and deactivating plugins
- Plugin security
- Creating the directory and files
- Adding menus and the settings page
- Inserting custom functionality
- Testing and debugging WordPress plugins
- Creating widgets
- Managing users and roles
- Adding custom post types and taxonomies
- Working with custom fields and database queries
- Using APIs: Transients, HTTP, and REST