How to translate a WordPress theme or plugin
WordPress has a built-in localisation system based on GNU gettext. The workflow: wrap translatable strings in translation functions, generate a .pot template, create a .po file for each language, compile it to a binary .mo file, and load the text domain in your theme or plugin.
Problem: How do you make a WordPress theme or plugin translatable, so that all user-facing strings can be translated without modifying the source code?
Step 1. Wrap strings in translation functions
Every function takes the text and your text domain, a unique ID of your theme or plugin:
// __() returns the translated string. $label = __( 'Read more', 'my-theme' ); // esc_html_e() echoes the translated string, escaped for HTML. esc_html_e( 'Posted on', 'my-theme' ); // esc_attr__() returns a translated string, escaped for an attribute. echo '<input type="submit" value="' . esc_attr__( 'Submit', 'my-theme' ) . '">'; // Placeholders: translators move %s, the code stays the same. printf( esc_html__( 'Posted by %s', 'my-theme' ), esc_html( get_the_author() ) );
Prefer the escaping variants (esc_html__(), esc_html_e(), esc_attr__()) over plain _e(): a translation is just text from a file, and escaping it on output keeps a broken or malicious translation from injecting HTML.
Step 2. Load the text domain
add_action( 'after_setup_theme', 'helloadmin_load_textdomain' );
function helloadmin_load_textdomain() {
load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
For a plugin, use load_plugin_textdomain() on the plugins_loaded hook.
Step 3. Generate the .pot template
With WP-CLI:
wp i18n make-pot . languages/my-theme.pot --domain=my-theme
If you don’t use WP-CLI, Poedit can extract the strings too: create a new catalogue and point it at your theme folder.
Step 4. Translate and compile
Open the .pot file in Poedit, translate the strings and save. Poedit creates both the .po and the .mo file. For a theme, name them by locale only and place them in the languages/ folder:
languages/ ├── my-theme.pot # template ├── zh_CN.po # Chinese translation source ├── zh_CN.mo # compiled file that WordPress reads └── ja.po
Plugins use the text domain as a prefix: my-plugin-zh_CN.mo.
Switch the language by URL
To serve a different locale on some URLs, for example under /zh-hans/, filter the locale before the text domain is loaded:
add_filter( 'locale', 'helloadmin_locale_by_url' );
function helloadmin_locale_by_url( $locale ) {
$uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
if ( ! is_admin() && 0 === strpos( $uri, '/zh-hans/' ) ) {
return 'zh_CN';
}
return $locale;
}
For a real multilingual site, a plugin like Polylang or WPML handles this, together with translated content and URLs.
NOTE: The .mo (machine object) file is the binary that WordPress reads at runtime; the .po (portable object) file is the human-editable source. Keep them in sync: edit the .po and recompile the .mo from it. Loco Translate lets editors update translations from the WordPress admin without touching the command line.