Learn to implement i18n in your WordPress plugin. Master text domains, translation functions, and automated POT file generation for global distribution.
Previously in this course, we explored Versioning and Release Management to ensure our release cycle is stable and predictable. Now that our plugin is ready for production, we must ensure it can speak the language of its users worldwide. Internationalization (i18n) is the process of preparing your code so it can be easily localized into different languages without requiring changes to the core logic.
The foundation of WordPress i18n is the text domain. This is a unique string used to identify your plugin's strings to the WordPress translation system. Without a domain, WordPress cannot associate your strings with the correct language files.
You must declare your text domain in the plugin header:
PHP#6A9955">/** * Plugin Name: Knowledge Base * Text Domain: knowledge-base * Domain Path: /languages */
In your code, you should consistently use this domain in every translation function call. If you've been following our Modern PHP Standards, ensure your service providers load the translation files as early as possible, ideally during the plugins_loaded hook.
WordPress provides a robust set of translation functions. Never hardcode strings; wrap every user-facing string in one of these helpers.
__(): Translates a string and returns it._e(): Translates a string and echoes it._x(): Translates a string with context (crucial for disambiguation).esc_html__() / esc_attr__(): Combines translation with escaping for security.In our Knowledge Base plugin, let's update a repository method that returns status messages:
PHPnamespace KB\Repository; class ArticleRepository { public function getStatusMessage(string $status): string { #6A9955">// Use context to help translators distinguish between "Publish" (verb) and "Published" (adjective) return match($status) { 'draft' => _x('Draft', 'article status', 'knowledge-base'), 'published' => _x('Published', 'article status', 'knowledge-base'), default => esc_html__('Unknown Status', 'knowledge-base'), }; } }
By using _x with the 'article status' context, you allow translators to provide different translations based on whether "Draft" refers to a noun or a verb in their specific language.
A POT (Portable Object Template) file is a list of all translatable strings in your plugin. It acts as the source file for translators. Manually maintaining this is error-prone; instead, use the WP-CLI to automate the process.
Run this command from your plugin root:
Bashwp i18n make-pot . languages/knowledge-base.pot --domain=knowledge-base
This command scans all your PHP and JavaScript files, extracts strings wrapped in translation functions, and compiles them into the languages/ directory.
src/ directory.esc_html__('Your String', 'knowledge-base').languages folder to your root directory.wp i18n make-pot command and inspect the generated .pot file to ensure all your new strings are included.echo 'Hello ' . $name;. Use placeholders: printf(esc_html__('Hello %s', 'knowledge-base'), esc_html($name));.__("Hello $name", 'domain') will fail because the translator sees "Hello $name", not the actual value.@wordpress/i18n package. Learn more about Internationalization in React: WordPress Translation for Plugins to handle strings passed to your admin dashboards.Internationalization is not an afterthought; it is a requirement for any professional plugin. By implementing consistent text domains, utilizing the full suite of WordPress translation functions, and automating POT file generation, you ensure your plugin is ready for a global market.
Up next: We will secure our revenue model by building a Licensing Infrastructure to manage plugin activations and updates.
Learn how to implement i18n in your React admin screens. Master __() and _x() in JavaScript to make your Knowledge Base plugin truly translation-ready.
Read moreLearn to implement WordPress internationalization (i18n) to make your plugins translation-ready. Master text domains, translation functions, and POT file generation.
Internationalization (i18n)
Custom Hooks for React