Make WordPress Core


Ignore:
Timestamp:
10/20/2020 04:03:58 PM (6 years ago)
Author:
ocean90
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 $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 catch this issue earlier.
  • Adds a new test plugin/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.

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

File:
1 edited

Legend:

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

    r49197 r49236  
    41354135        return is_string( $value ) ? addslashes( $value ) : $value;
    41364136}
     4137
     4138/**
     4139 * Gets the path to a translation file for loading a textdomain just in time.
     4140 *
     4141 * Caches the retrieved results internally.
     4142 *
     4143 * @since 4.7.0
     4144 * @deprecated 5.6.0
     4145 * @access private
     4146 *
     4147 * @see _load_textdomain_just_in_time()
     4148 *
     4149 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     4150 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     4151 * @return string|false The path to the translation file or false if no translation file was found.
     4152 */
     4153function _get_path_to_translation( $domain, $reset = false ) {
     4154        _deprecated_function( __FUNCTION__, '5.6.0', 'WP_Textdomain_Registry' );
     4155
     4156        static $available_translations = array();
     4157
     4158        if ( true === $reset ) {
     4159                $available_translations = array();
     4160        }
     4161
     4162        if ( ! isset( $available_translations[ $domain ] ) ) {
     4163                $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
     4164        }
     4165
     4166        return $available_translations[ $domain ];
     4167}
     4168
     4169/**
     4170 * Gets the path to a translation file in the languages directory for the current locale.
     4171 *
     4172 * Holds a cached list of available .mo files to improve performance.
     4173 *
     4174 * @since 4.7.0
     4175 * @deprecated 5.6.0
     4176 * @access private
     4177 *
     4178 * @see _get_path_to_translation()
     4179 *
     4180 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     4181 * @return string|false The path to the translation file or false if no translation file was found.
     4182 */
     4183function _get_path_to_translation_from_lang_dir( $domain ) {
     4184        _deprecated_function( __FUNCTION__, '5.6.0', 'WP_Textdomain_Registry' );
     4185
     4186        static $cached_mofiles = null;
     4187
     4188        if ( null === $cached_mofiles ) {
     4189                $cached_mofiles = array();
     4190
     4191                $locations = array(
     4192                        WP_LANG_DIR . '/plugins',
     4193                        WP_LANG_DIR . '/themes',
     4194                );
     4195
     4196                foreach ( $locations as $location ) {
     4197                        $mofiles = glob( $location . '/*.mo' );
     4198                        if ( $mofiles ) {
     4199                                $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
     4200                        }
     4201                }
     4202        }
     4203
     4204        $locale = determine_locale();
     4205        $mofile = "{$domain}-{$locale}.mo";
     4206
     4207        $path = WP_LANG_DIR . '/plugins/' . $mofile;
     4208        if ( in_array( $path, $cached_mofiles, true ) ) {
     4209                return $path;
     4210        }
     4211
     4212        $path = WP_LANG_DIR . '/themes/' . $mofile;
     4213        if ( in_array( $path, $cached_mofiles, true ) ) {
     4214                return $path;
     4215        }
     4216
     4217        return false;
     4218}
Note: See TracChangeset for help on using the changeset viewer.