load_plugin_textdomain()WP 1.5.0

Loads the .mo translation file from the specified folder. Does not work with MU plugins.

The .mo file must be named: TRANSLATION_DOMAIN-LOCALE.mowhere locale is the language code (see get_locale()). For example, if TRANSLATION_DOMAIN=my-plugin and the Russian language has been selected LOCALE = ru_RU, then the files should be named: my-plugin-ru_RU.mo and my-plugin-ru_RU.po.

It is recommended to call the function on plugins_loaded hook.

Since version 4.6. the function first tries to load the .mo file from the WP_LANG_DIR/plugins/ folder usually it's /wp-content/language/plugins.

To load the translation of the theme, use load_theme_textdomain().

Hooks from the function

Return

true|false. True when textdomain is successfully loaded, false otherwise.

Usage

load_plugin_textdomain( $domain, $deprecated, $plugin_rel_path );
$domain(строка) (required)
A unique identifier to get the translation string.
$deprecated(string)
Deprecated argument, works up to version 2.7. Path similar to ABSPATH, to .mo file.
Default: false
$plugin_rel_path(string)

The path to the .mo directory of the file is relative to WP_PLUGIN_DIR.

If the path is not specified, it will be the root directory of the plugins WP_PLUGIN_DIR. I.e. the path to the file will be as follows: WP_PLUGIN_DIR/domain-ru_RU.mo

Default: false

Examples

0

#1 Registering a translation file for the plugin

It is assumed that this code will be located in the main plugin file or in a file that is in the plugin's root directory. If this is not the case, __FILE__ must be replaced by the appropriate path.

Translation file must lie in the directory of the plugin and must be named: my-plugin-ru_RU.mo.

add_action( 'plugins_loaded', 'myplugin_init' );
function myplugin_init(){
	load_plugin_textdomain( 'my-plugin', false, dirname( plugin_basename( __FILE__ ) ) );
}
0

#2 If we want to put the translation file .mo in the languages subfolder:

add_action( 'plugins_loaded', 'myplugin_init' );

function myplugin_init() {
	 load_plugin_textdomain( 'my-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
0

#3 Register a translation file for the MU plugin

WP has a special function load_muplugin_textdomain().

Changelog

Since 1.5.0 Introduced.
Since 4.6.0 The function now tries to load the .mo file from the languages directory first.

load_plugin_textdomain() code WP 6.5.2

function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $wp_textdomain_registry;

	/**
	 * Filters a plugin's locale.
	 *
	 * @since 3.0.0
	 *
	 * @param string $locale The plugin's current locale.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$locale = apply_filters( 'plugin_locale', determine_locale(), $domain );

	$mofile = $domain . '-' . $locale . '.mo';

	// Try to load from the languages directory first.
	if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) {
		return true;
	}

	if ( false !== $plugin_rel_path ) {
		$path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
	} elseif ( false !== $deprecated ) {
		_deprecated_argument( __FUNCTION__, '2.7.0' );
		$path = ABSPATH . trim( $deprecated, '/' );
	} else {
		$path = WP_PLUGIN_DIR;
	}

	$wp_textdomain_registry->set_custom_path( $domain, $path );

	return load_textdomain( $domain, $path . '/' . $mofile, $locale );
}