Loading WordPress translation files using load_textdomain

The load_textdomain function is a part of WordPress’s internationalization and localization system. It is used to load a .mo file into the text domain of the current site. This function is particularly useful when the site content needs to be displayed in different languages. It helps in translating the strings used in the plugin or theme into the desired language, as specified in the .mo file.

By loading the .mo file, the load_textdomain function enables the translation of all the text elements within the site, such as posts, pages, tags, categories, and custom post types, into the language set by the site’s administrator. This function is therefore a crucial part of making a WordPress site multilingual.

It’s important to note that the load_textdomain function only loads the translations but does not automatically apply them. The actual translation of the site’s content happens when the translated strings are retrieved using the appropriate WordPress function.

Parameters Accepted by the load_textdomain Function

The load_textdomain function in WordPress accepts three parameters. These parameters are:

  • $domain (string) – This is a required parameter. It serves as a unique identifier used in fetching translated strings, known as the text domain.
  • $mofile (string) – This is another required parameter. It specifies the path to the .mo file which contains the translations.
  • $locale (string) – This is an optional parameter. Its default value is null. It specifies the locale, with the default being the current locale.

Return Value of the load_textdomain Function

The load_textdomain function returns a boolean value. If the function executes successfully, it returns true. If it fails, it returns false.

Note: If the function does not accept any parameters, it would be explicitly stated.

Examples

How to Load a Text Domain for a Plugin

This is the most common usage of the load_textdomain function. It is used to load the .mo file for a specific text domain, in this case for a plugin.

function load_my_textdomain() {
 $domain = 'my-plugin';
 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
 $mofile = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo';
 load_textdomain( $domain, $mofile );
}
add_action( 'init', 'load_my_textdomain' );

How to Load a Text Domain for a Theme

This example shows how to use the load_textdomain function to load the .mo file for a specific text domain, in this case for a theme.

function load_my_textdomain() {
 $domain = 'my-theme';
 $locale = apply_filters( 'theme_locale', get_locale(), $domain );
 $mofile = WP_LANG_DIR . '/themes/' . $domain . '-' . $locale . '.mo';
 load_textdomain( $domain, $mofile );
}
add_action( 'after_setup_theme', 'load_my_textdomain' );

Conclusion

The load_textdomain function is a part of WordPress’s internationalization and localization system, serving to load a .mo file into the text domain. This function is utilized to translate text on a website, allowing developers to create multilingual sites that cater to a global audience. Its application in a WordPress theme or plugin can help in loading the translated strings for the text domain, hence making the website content accessible and understandable to users from different linguistic backgrounds.