load_textdomain()WP 1.5.0

Load a .mo file into the text domain $domain.

If the text domain already exists, the translations will be merged. If both sets have the same string, the translation from the original value will be taken.

On success, the .mo file will be placed in the $l10n global by $domain and will be a MO object.

Return

true|false. True on success, false on failure.

Usage

load_textdomain( $domain, $mofile );
$domain(string) (required)
Text domain. Unique identifier for retrieving translated strings.
$mofile(string) (required)
Path to the .mo file.

Examples

0

#1 Line translation in WordPress

In this example, translate the string __( 'book', 'mydomain' ). It is assumed that a .mo file has already been created and has the data to translate this string:

// here the .mo file must be in the `lang` folder, which is in 
// the folder that contains the file in which this (string - code) is called.
// Connect the existing .mo file (file name: ru_RU.mo or other, depending on the locale)
add_action( 'plugins_loaded', 'load_my_textdomain' );

function load_my_textdomain(){
	$mo_file_path = dirname(__FILE__) . '/lang/'. determine_locale() . '.mo';

	load_textdomain( 'mydomain', $mo_file_path );
}

For WP versions earlier than 5.0, use get_locale() instead of determine_locale().

Now in the theme we use:

<?php _e( 'book', 'mydomain' ); ?>

The "βιβλίο" will come out for Greek locale.

0

#2 Load translation which is different from the current user locale.

There are situations where one requires a plugin translation locale to be loaded which is different from the current user locale.

For example, in multilingual websites, creating a translation of a post/widget may require some translations to be loaded for a given plugin text domain. The user locale (dashboard locale) is loaded by default, so it is important to unload that if it has been loaded already and seek the translation file to load for the text domain for the requested locale,

if ( is_textdomain_loaded( $plugin ) ) {
	unload_textdomain( $plugin );
}

$mofile = sprintf( '%s-%s.mo', $plugin, $locale );

// check the installation language path first.
$domain_path = path_join( WP_LANG_DIR, 'plugins' );
$loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );

// else, check the plugin language folder.
if ( ! $loaded ) { 
	$domain_path = path_join( WP_PLUGIN_DIR, "{$plugin}/languages" );
	load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
}

Notes

  • Global. MO[]. $l10n An array of all currently loaded text domains.
  • Global. MO[]. $l10n_unloaded An array of all text domains that have been unloaded again.

Changelog

Since 1.5.0 Introduced.

Code of load_textdomain() WP 6.0.1

function load_textdomain( $domain, $mofile ) {
	global $l10n, $l10n_unloaded;

	$l10n_unloaded = (array) $l10n_unloaded;

	/**
	 * Filters whether to override the .mo file loading.
	 *
	 * @since 2.9.0
	 *
	 * @param bool   $override Whether to override the .mo file loading. Default false.
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile   Path to the MO file.
	 */
	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );

	if ( true === (bool) $plugin_override ) {
		unset( $l10n_unloaded[ $domain ] );

		return true;
	}

	/**
	 * Fires before the MO translation file is loaded.
	 *
	 * @since 2.9.0
	 *
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile Path to the .mo file.
	 */
	do_action( 'load_textdomain', $domain, $mofile );

	/**
	 * Filters MO file path for loading translations for a specific text domain.
	 *
	 * @since 2.9.0
	 *
	 * @param string $mofile Path to the MO file.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );

	if ( ! is_readable( $mofile ) ) {
		return false;
	}

	$mo = new MO();
	if ( ! $mo->import_from_file( $mofile ) ) {
		return false;
	}

	if ( isset( $l10n[ $domain ] ) ) {
		$mo->merge_with( $l10n[ $domain ] );
	}

	unset( $l10n_unloaded[ $domain ] );

	$l10n[ $domain ] = &$mo;

	return true;
}