wppaste
WordPress

load_textdomain( string $domain, string $mofile, string $locale = null ): bool

Since
1.5.0, 6.1.0
Source
wp-includes/l10n.php:726
Loads a .mo file into the text domain $domain.

Description

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.

Compatibility

WordPress
since 6.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$domainstring
Text domain. Unique identifier for retrieving translated strings.
$mofilestring
Path to the .mo file.
$localestringoptional
Locale. Default is the current locale.Default: null

Return value

bool
True on success, false on failure.

Performance profile

How much work a call to load_textdomain() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–117

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 126.

Plugin surface
6 hooks

Third-party callbacks on 'pre_load_textdomain', 'override_load_textdomain', 'load_textdomain' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost load_textdomain() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_string($domain)11none
is_string($domain) && $loaded !== null23–24apply_filters()
is_string($domain) && $loaded === null31apply_filters(), apply_filters()
is_string($domain) && $loaded === null && $preferred_format === "mo"63–65apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters()
is_string($domain) && $loaded === null && $preferred_format === "mo"66–68apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters()
is_string($domain) && $loaded === null && $preferred_format !== "mo"71–73apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace()
is_string($domain) && $loaded === null && $preferred_format !== "mo"74–76apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace()
is_string($domain) && $loaded === null && $preferred_format === "mo" && !$translation_files94–98apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), apply_filters(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format === "mo" && !$translation_files97–101apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), apply_filters(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format !== "mo" && !$translation_files102–106apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace(), apply_filters(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format === "mo" && !$translation_files && isset($l10n[$domain]) && $value instanceof105–106apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), apply_filters(), ->load_file(), ->get_filename(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format !== "mo" && !$translation_files105–109apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace(), apply_filters(), ->load_file(), ->set()
3 further outcomes, up to 117 instructions
is_string($domain) && $loaded === null && $preferred_format === "mo" && !$translation_files && isset($l10n[$domain]) && $value instanceof108–109apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), apply_filters(), ->load_file(), ->get_filename(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format !== "mo" && !$translation_files && isset($l10n[$domain]) && $value instanceof113–114apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace(), apply_filters(), ->load_file(), ->get_filename(), ->load_file(), ->set()
is_string($domain) && $loaded === null && $preferred_format !== "mo" && !$translation_files && isset($l10n[$domain]) && $value instanceof116–117apply_filters(), apply_filters(), do_action(), apply_filters(), determine_locale(), ::WP_Translation_Controller(), ->set_locale(), apply_filters(), substr_replace(), apply_filters(), ->load_file(), ->get_filename(), ->load_file(), ->set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12611–11712
8.512611–117122 fewer instructions than PHP 8.4
8.412811–119122 fewer instructions than PHP 8.3
8.313011–12112
8.213011–12112
8.113011–12112
7.413011–12112

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 6

6 hooks fire while load_textdomain() runs, in this order:

  1. apply_filters( pre_load_textdomain )filterline 749 (+23 into the body)

    Filters whether to short-circuit loading .mo file.

  2. apply_filters( override_load_textdomain )filterline 769 (+43 into the body)

    Filters whether to override the .mo file loading.

  3. do_action( load_textdomain )actionline 785 (+59 into the body)

    Fires before the MO translation file is loaded.

  4. apply_filters( load_textdomain_mofile )filterline 795 (+69 into the body)

    Filters MO file path for loading translations for a specific text domain.

  5. apply_filters( translation_file_format )filterline 816 (+90 into the body)

    Filters the preferred file format for translation files.

  6. apply_filters( load_translation_file )filterline 843 (+117 into the body)

    Filters the file path for loading translations for the given text domain.

Uses · 5

Used by · 5

Source code

function load_textdomain( $domain, $mofile, $locale = null ) {	/** @var WP_Textdomain_Registry $wp_textdomain_registry */	global $l10n, $l10n_unloaded, $wp_textdomain_registry; 	$l10n_unloaded = (array) $l10n_unloaded; 	if ( ! is_string( $domain ) ) {		return false;	} 	/**	 * Filters whether to short-circuit loading .mo file.	 *	 * Returning a non-null value from the filter will effectively short-circuit	 * the loading, returning the passed value instead.	 *	 * @since 6.3.0	 *	 * @param bool|null   $loaded The result of loading a .mo file. Default null.	 * @param string      $domain Text domain. Unique identifier for retrieving translated strings.	 * @param string      $mofile Path to the MO file.	 * @param string|null $locale Locale.	 */	$loaded = apply_filters( 'pre_load_textdomain', null, $domain, $mofile, $locale );	if ( null !== $loaded ) {		if ( true === $loaded ) {			unset( $l10n_unloaded[ $domain ] );		} 		return $loaded;	} 	/**	 * Filters whether to override the .mo file loading.	 *	 * @since 2.9.0	 * @since 6.2.0 Added the `$locale` parameter.	 *	 * @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.	 * @param string|null $locale   Locale.	 */	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile, $locale ); 	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 ( ! $locale ) {		$locale = determine_locale();	} 	$i18n_controller = WP_Translation_Controller::get_instance(); 	// Ensures the correct locale is set as the current one, in case it was filtered.	$i18n_controller->set_locale( $locale );

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.1.0
Added the $locale parameter.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/l10n.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.