Make WordPress Core


Ignore:
Timestamp:
08/11/2022 12:37:05 PM (22 months ago)
Author:
swissspidy
Message:

I18N: Introduce WP_Textdomain_Registry to store text domains and their language directory paths.

Previously, when using switch_to_locale() all current loaded text domains were unloaded and added to the $l10n_unloaded global. This prevented the just-in-time loading for text domains after a switch. The just-in-time loading was also only possible if the translations were stored in WP_LANG_DIR. Both issues have been fixed.

  • Adds WP_Textdomain_Registry to keep track of the language directory paths for all plugins and themes.
  • Updates all load_*_textdomain() functions to store the path in WP_Textdomain_Registry.
  • Adds $locale parameter to load_textdomain() to specify the locale the translation file is for.
  • Adds $reloadable parameter to unload_textdomain() to define whether a text domain can be loaded just-in-time again. This is used by WP_Locale_Switcher::load_translations().
  • Extends _load_textdomain_just_in_time() to also support text domains of plugins and themes with custom language directories.
  • Fixes the incorrect test_plugin_translation_after_switching_locale_twice() test which should have caught this issue earlier.
  • Adds a new test plugin and theme to test the loading of translations with a custom language directory.
  • Deprecates the now unused and private _get_path_to_translation() and _get_path_to_translation_from_lang_dir() functions.

Previously added in [49236] and reverted in [49236] to investigate concerns which are now addressed here.

Props yoavf, swissspidy, dd32, ocean90.
See #26511.
Fixes #39210.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r53715 r53874  
    43614361    return false;
    43624362}
     4363
     4364/**
     4365 * Gets the path to a translation file for loading a textdomain just in time.
     4366 *
     4367 * Caches the retrieved results internally.
     4368 *
     4369 * @since 4.7.0
     4370 * @deprecated 6.1.0
     4371 * @access private
     4372 *
     4373 * @see _load_textdomain_just_in_time()
     4374 *
     4375 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     4376 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     4377 * @return string|false The path to the translation file or false if no translation file was found.
     4378 */
     4379function _get_path_to_translation( $domain, $reset = false ) {
     4380    _deprecated_function( __FUNCTION__, '6.1.0', 'WP_Textdomain_Registry' );
     4381
     4382    static $available_translations = array();
     4383
     4384    if ( true === $reset ) {
     4385        $available_translations = array();
     4386    }
     4387
     4388    if ( ! isset( $available_translations[ $domain ] ) ) {
     4389        $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
     4390    }
     4391
     4392    return $available_translations[ $domain ];
     4393}
     4394
     4395/**
     4396 * Gets the path to a translation file in the languages directory for the current locale.
     4397 *
     4398 * Holds a cached list of available .mo files to improve performance.
     4399 *
     4400 * @since 4.7.0
     4401 * @deprecated 6.1.0
     4402 * @access private
     4403 *
     4404 * @see _get_path_to_translation()
     4405 *
     4406 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     4407 * @return string|false The path to the translation file or false if no translation file was found.
     4408 */
     4409function _get_path_to_translation_from_lang_dir( $domain ) {
     4410    _deprecated_function( __FUNCTION__, '6.1.0', 'WP_Textdomain_Registry' );
     4411
     4412    static $cached_mofiles = null;
     4413
     4414    if ( null === $cached_mofiles ) {
     4415        $cached_mofiles = array();
     4416
     4417        $locations = array(
     4418            WP_LANG_DIR . '/plugins',
     4419            WP_LANG_DIR . '/themes',
     4420        );
     4421
     4422        foreach ( $locations as $location ) {
     4423            $mofiles = glob( $location . '/*.mo' );
     4424            if ( $mofiles ) {
     4425                $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
     4426            }
     4427        }
     4428    }
     4429
     4430    $locale = determine_locale();
     4431    $mofile = "{$domain}-{$locale}.mo";
     4432
     4433    $path = WP_LANG_DIR . '/plugins/' . $mofile;
     4434    if ( in_array( $path, $cached_mofiles, true ) ) {
     4435        return $path;
     4436    }
     4437
     4438    $path = WP_LANG_DIR . '/themes/' . $mofile;
     4439    if ( in_array( $path, $cached_mofiles, true ) ) {
     4440        return $path;
     4441    }
     4442
     4443    return false;
     4444}
Note: See TracChangeset for help on using the changeset viewer.