load_textdomain( string $domain, string $mofile, string $locale = null ): bool
- Since
- 1.5.0, 6.1.0
- Source
wp-includes/l10n.php:726
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
- Scaling
- Scales with input
- Instructions
- 11–117
- Plugin surface
- 6 hooks
- Called by
- 5
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 126.
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.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($domain) | 11 | none |
is_string($domain) && $loaded !== null | 23–24 | apply_filters() |
is_string($domain) && $loaded === null | 31 | apply_filters(), apply_filters() |
is_string($domain) && $loaded === null && $preferred_format === "mo" | 63–65 | apply_filters(), apply_filters(), do_action(), apply_filters(), ::WP_Translation_Controller(), ->set_locale(), apply_filters() |
is_string($domain) && $loaded === null && $preferred_format === "mo" | 66–68 | apply_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–73 | apply_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–76 | apply_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_files | 94–98 | apply_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_files | 97–101 | apply_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_files | 102–106 | apply_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 instanceof | 105–106 | apply_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_files | 105–109 | apply_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 instanceof | 108–109 | apply_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 instanceof | 113–114 | apply_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 instanceof | 116–117 | apply_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 126 | 11–117 | 12 | |
| 8.5 | 126 | 11–117 | 12 | 2 fewer instructions than PHP 8.4 |
| 8.4 | 128 | 11–119 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 130 | 11–121 | 12 | |
| 8.2 | 130 | 11–121 | 12 | |
| 8.1 | 130 | 11–121 | 12 | |
| 7.4 | 130 | 11–121 | 12 |
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:
- apply_filters( pre_load_textdomain )filterline 749 (+23 into the body)
Filters whether to short-circuit loading .mo file.
- apply_filters( override_load_textdomain )filterline 769 (+43 into the body)
Filters whether to override the .mo file loading.
- do_action( load_textdomain )actionline 785 (+59 into the body)
Fires before the MO translation file is loaded.
- apply_filters( load_textdomain_mofile )filterline 795 (+69 into the body)
Filters MO file path for loading translations for a specific text domain.
- apply_filters( translation_file_format )filterline 816 (+90 into the body)
Filters the preferred file format for translation files.
- 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
- apply_filters()Calls the callback functions that have been added to a filter hook.
- do_action()Calls the callback functions that have been added to an action hook.
- determine_locale()Determines the current locale desired for the request.
- WP_Translation_Controller::get_instance()Utility method to retrieve the main instance of the class.
- WP_Translations::__construct()Constructor.
Used by · 5
- WP_REST_Site_Health_Controller::load_admin_textdomain()Loads the admin textdomain for Site Health tests.
- _load_textdomain_just_in_time()Loads plugin and theme text domains just-in-time.
- load_default_textdomain()Loads default translated strings based on locale.
- wp_load_translations_early()Attempts an early load of translations.
- wp_timezone_choice()Gives a nicely-formatted list of timezone strings.
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.
Signature, return type and hooks compared across 5 parsed releases.
$locale parameter.from the docblockAbout 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.