load_muplugin_textdomain()WP 3.0.0

Load the translated strings for a plugin residing in the mu-plugins directory.

Hooks from the function

Return

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

Usage

load_muplugin_textdomain( $domain, $mu_plugin_rel_path );
$domain(string) (required)
Text domain. Unique identifier for retrieving translated strings.
$mu_plugin_rel_path(string)
Relative to WPMU_PLUGIN_DIR directory in which the .mo file resides.
Default: empty string

Examples

0

#1 Register a translation file for the MU plugin

The translation file must be in the plugin directory and must be named like: my-plugin-fr_FR.mo.

add_action( 'muplugins_loaded', 'myplugin_init' );

function myplugin_init(){
	load_muplugin_textdomain( 'my-plugin', dirname( plugin_basename( __FILE__ ) ) ); 
}
0

#2 If you want to place the .mo file in the subfolder lang:

We may change the translation file place. For example, let's place it into the lang subfolder of the plugin. The file must be named like: my-plugin-ru_RU.mo.

add_action( 'muplugins_loaded', 'myplugin_init' );

function myplugin_init() {
	 load_muplugin_textdomain( 'my-plugin', dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}

Notes

  • Global. WP_Textdomain_Registry. $wp_textdomain_registry WordPress Textdomain Registry.

Changelog

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

load_muplugin_textdomain() code WP 6.5.2

function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $wp_textdomain_registry;

	/** This filter is documented in wp-includes/l10n.php */
	$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;
	}

	$path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );

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

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