_load_textdomain_just_in_time

The timeline below displays how wordpress function _load_textdomain_just_in_time has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Loads plugin and theme text domains just-in-time.
 *
 * When a textdomain is encountered for the first time, we try to load
 * the translation file from `wp-content/languages`, removing the need
 * to call load_plugin_textdomain() or load_theme_textdomain().
 *
 * @since 4.6.0
 * @access private
 *
 * @global MO[]                   $l10n_unloaded          An array of all text domains that have been unloaded again.
 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
 *
 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 * @return bool True when the textdomain is successfully loaded, false otherwise.
 */
function _load_textdomain_just_in_time($domain)
{
    /** @var WP_Textdomain_Registry $wp_textdomain_registry */
    global $l10n_unloaded, $wp_textdomain_registry;
    $l10n_unloaded = (array) $l10n_unloaded;
    // Short-circuit if domain is 'default' which is reserved for core.
    if ('default' === $domain || isset($l10n_unloaded[$domain])) {
        return false;
    }
    if (!$wp_textdomain_registry->has($domain)) {
        return false;
    }
    $locale = determine_locale();
    $path = $wp_textdomain_registry->get($domain, $locale);
    if (!$path) {
        return false;
    }
    // Themes with their language directory outside of WP_LANG_DIR have a different file name.
    $template_directory = trailingslashit(get_template_directory());
    $stylesheet_directory = trailingslashit(get_stylesheet_directory());
    if (str_starts_with($path, $template_directory) || str_starts_with($path, $stylesheet_directory)) {
        $mofile = "{$path}{$locale}.mo";
    } else {
        $mofile = "{$path}{$domain}-{$locale}.mo";
    }
    return load_textdomain($domain, $mofile, $locale);
}

WordPress Version: 5.1

/**
 * Loads plugin and theme textdomains just-in-time.
 *
 * When a textdomain is encountered for the first time, we try to load
 * the translation file from `wp-content/languages`, removing the need
 * to call load_plugin_texdomain() or load_theme_texdomain().
 *
 * @since 4.6.0
 * @access private
 *
 * @see get_translations_for_domain()
 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
 *
 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 * @return bool True when the textdomain is successfully loaded, false otherwise.
 */
function _load_textdomain_just_in_time($domain)
{
    global $l10n_unloaded;
    $l10n_unloaded = (array) $l10n_unloaded;
    // Short-circuit if domain is 'default' which is reserved for core.
    if ('default' === $domain || isset($l10n_unloaded[$domain])) {
        return false;
    }
    $translation_path = _get_path_to_translation($domain);
    if (false === $translation_path) {
        return false;
    }
    return load_textdomain($domain, $translation_path);
}

WordPress Version: 4.7

/**
 * Loads plugin and theme textdomains just-in-time.
 *
 * When a textdomain is encountered for the first time, we try to load
 * the translation file from `wp-content/languages`, removing the need
 * to call load_plugin_texdomain() or load_theme_texdomain().
 *
 * @since 4.6.0
 * @access private
 *
 * @see get_translations_for_domain()
 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
 *
 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 * @return bool True when the textdomain is successfully loaded, false otherwise.
 */
function _load_textdomain_just_in_time($domain)
{
    global $l10n_unloaded;
    $l10n_unloaded = (array) $l10n_unloaded;
    // Short-circuit if domain is 'default' which is reserved for core.
    if ('default' === $domain || isset($l10n_unloaded[$domain])) {
        return false;
    }
    $translation_path = _get_path_to_translation($domain);
    if (false === $translation_path) {
        return false;
    }
    return load_textdomain($domain, $translation_path);
}

WordPress Version: 4.6

/**
 * Loads plugin and theme textdomains just-in-time.
 *
 * When a textdomain is encountered for the first time, we try to load
 * the translation file from `wp-content/languages`, removing the need
 * to call load_plugin_texdomain() or load_theme_texdomain().
 *
 * Holds a cached list of available .mo files to improve performance.
 *
 * @since 4.6.0
 * @access private
 *
 * @see get_translations_for_domain()
 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
 *
 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 * @return bool True when the textdomain is successfully loaded, false otherwise.
 */
function _load_textdomain_just_in_time($domain)
{
    global $l10n_unloaded;
    $l10n_unloaded = (array) $l10n_unloaded;
    static $cached_mofiles = null;
    // Short-circuit if domain is 'default' which is reserved for core.
    if ('default' === $domain || isset($l10n_unloaded[$domain])) {
        return false;
    }
    if (null === $cached_mofiles) {
        $cached_mofiles = array();
        $locations = array(WP_LANG_DIR . '/plugins', WP_LANG_DIR . '/themes');
        foreach ($locations as $location) {
            foreach (get_available_languages($location) as $file) {
                $cached_mofiles[] = "{$location}/{$file}.mo";
            }
        }
    }
    $locale = get_locale();
    $mofile = "{$domain}-{$locale}.mo";
    if (in_array(WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles)) {
        return load_textdomain($domain, WP_LANG_DIR . '/plugins/' . $mofile);
    }
    if (in_array(WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles)) {
        return load_textdomain($domain, WP_LANG_DIR . '/themes/' . $mofile);
    }
    return false;
}