is_textdomain_loaded()WP 3.0.0

Whether there are translations for the text domain.

1 time — 0.000027 sec (very fast) | 50000 times — 0.01 sec (speed of light) | PHP 7.1.5, WP 4.8.2

No Hooks.

Return

true|false. Whether there are translations.

Usage

is_textdomain_loaded( $domain );
$domain(string) (required)
Text domain. Unique identifier for retrieving translated strings.

Examples

0

#1 Connect the plugin translation file, if this is not already done.

add_action( 'plugins_loaded', 'myplugin_init' );

function myplugin_init(){

	$domain = 'my-plugin';

	if( is_textdomain_loaded( $domain ) ){
		load_plugin_textdomain( $domain, false, dirname( plugin_basename( __FILE__ ) ) ); 
	}

}
0

#2 Include Plugin Translation 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 requires 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' );
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" );
	$loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
}

Notes

  • Global. MO[]. $l10n An array of all currently loaded text domains.

Changelog

Since 3.0.0 Introduced.

is_textdomain_loaded() code WP 6.1.1

function is_textdomain_loaded( $domain ) {
	global $l10n;
	return isset( $l10n[ $domain ] );
}