Make WordPress Core


Ignore:
Timestamp:
11/12/2020 02:41:19 PM (6 years ago)
Author:
ocean90
Message:

I18N: Revert [49236] for now to investigate alternative implementations.

See #39210, #51678, #26511.

File:
1 edited

Legend:

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

    r49236 r49566  
    690690 * @since 1.5.0
    691691 *
    692  * @global MO[]                   $l10n                   An array of all currently loaded text domains.
    693  * @global MO[]                   $l10n_unloaded          An array of all text domains that have been unloaded again.
    694  * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
     692 * @global MO[] $l10n          An array of all currently loaded text domains.
     693 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
    695694 *
    696695 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     
    699698 */
    700699function load_textdomain( $domain, $mofile ) {
    701         global $l10n, $l10n_unloaded, $wp_textdomain_registry;
     700        global $l10n, $l10n_unloaded;
    702701
    703702        $l10n_unloaded = (array) $l10n_unloaded;
     
    757756        $l10n[ $domain ] = &$mo;
    758757
    759         /** @var WP_Textdomain_Registry $wp_textdomain_registry */
    760         $wp_textdomain_registry->set( $domain, dirname( $mofile ) );
    761 
    762758        return true;
    763759}
     
    767763 *
    768764 * @since 3.0.0
    769  * @since 5.6.0 Added the `$reloadable` parameter.
    770765 *
    771766 * @global MO[] $l10n          An array of all currently loaded text domains.
    772767 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
    773768 *
    774  * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
    775  * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
     769 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    776770 * @return bool Whether textdomain was unloaded.
    777771 */
    778 function unload_textdomain( $domain, $reloadable = false ) {
     772function unload_textdomain( $domain ) {
    779773        global $l10n, $l10n_unloaded;
    780774
     
    785779         *
    786780         * @since 3.0.0
    787          * @since 5.6.0 Added the `$reloadable` parameter.
    788          *
    789          * @param bool   $override   Whether to override the text domain unloading. Default false.
    790          * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
    791          * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
    792          */
    793         $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain, $reloadable );
     781         *
     782         * @param bool   $override Whether to override the text domain unloading. Default false.
     783         * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
     784         */
     785        $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
    794786
    795787        if ( $plugin_override ) {
    796                 if ( ! $reloadable ) {
    797                         $l10n_unloaded[ $domain ] = true;
    798                 }
     788                $l10n_unloaded[ $domain ] = true;
    799789
    800790                return true;
     
    805795         *
    806796         * @since 3.0.0
    807          * @since 5.6.0 Added the `$reloadable` parameter.
    808          *
    809          * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
    810          * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
    811          */
    812         do_action( 'unload_textdomain', $domain, $reloadable );
     797         *
     798         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     799         */
     800        do_action( 'unload_textdomain', $domain );
    813801
    814802        if ( isset( $l10n[ $domain ] ) ) {
    815803                unset( $l10n[ $domain ] );
    816804
    817                 if ( ! $reloadable ) {
    818                         $l10n_unloaded[ $domain ] = true;
    819                 }
     805                $l10n_unloaded[ $domain ] = true;
    820806
    821807                return true;
     
    882868 */
    883869function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
    884         global $wp_textdomain_registry;
    885 
    886870        /**
    887871         * Filters a plugin's locale.
     
    910894        }
    911895
    912         /* @var WP_Textdomain_Registry $wp_textdomain_registry */
    913         $wp_textdomain_registry->set( $domain, $path );
    914 
    915896        return load_textdomain( $domain, $path . '/' . $mofile );
    916897}
     
    921902 * @since 3.0.0
    922903 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
    923  *
    924  * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
    925904 *
    926905 * @param string $domain             Text domain. Unique identifier for retrieving translated strings.
     
    930909 */
    931910function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
    932         global $wp_textdomain_registry;
    933 
    934911        /** This filter is documented in wp-includes/l10n.php */
    935912        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
     
    944921        $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
    945922
    946         /* @var WP_Textdomain_Registry $wp_textdomain_registry */
    947         $wp_textdomain_registry->set( $domain, $path );
    948 
    949923        return load_textdomain( $domain, $path . '/' . $mofile );
    950924}
     
    960934 * @since 1.5.0
    961935 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
    962  *
    963  * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
    964936 *
    965937 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     
    969941 */
    970942function load_theme_textdomain( $domain, $path = false ) {
    971         global $wp_textdomain_registry;
    972 
    973943        /**
    974944         * Filters a theme's locale.
     
    991961                $path = get_template_directory();
    992962        }
    993 
    994         /* @var WP_Textdomain_Registry $wp_textdomain_registry */
    995         $wp_textdomain_registry->set( $domain, $path );
    996963
    997964        return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
     
    12241191 * @access private
    12251192 *
    1226  * @global MO[]                   $l10n_unloaded          An array of all text domains that have been unloaded again.
    1227  * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
     1193 * @see get_translations_for_domain()
     1194 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
    12281195 *
    12291196 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     
    12311198 */
    12321199function _load_textdomain_just_in_time( $domain ) {
    1233         global $l10n_unloaded, $wp_textdomain_registry;
     1200        global $l10n_unloaded;
    12341201
    12351202        $l10n_unloaded = (array) $l10n_unloaded;
     
    12401207        }
    12411208
    1242         /** @var WP_Textdomain_Registry $wp_textdomain_registry */
    1243         $path = $wp_textdomain_registry->get( $domain );
    1244         if ( ! $path ) {
     1209        $translation_path = _get_path_to_translation( $domain );
     1210        if ( false === $translation_path ) {
    12451211                return false;
    12461212        }
    12471213
     1214        return load_textdomain( $domain, $translation_path );
     1215}
     1216
     1217/**
     1218 * Gets the path to a translation file for loading a textdomain just in time.
     1219 *
     1220 * Caches the retrieved results internally.
     1221 *
     1222 * @since 4.7.0
     1223 * @access private
     1224 *
     1225 * @see _load_textdomain_just_in_time()
     1226 *
     1227 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     1228 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     1229 * @return string|false The path to the translation file or false if no translation file was found.
     1230 */
     1231function _get_path_to_translation( $domain, $reset = false ) {
     1232        static $available_translations = array();
     1233
     1234        if ( true === $reset ) {
     1235                $available_translations = array();
     1236        }
     1237
     1238        if ( ! isset( $available_translations[ $domain ] ) ) {
     1239                $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
     1240        }
     1241
     1242        return $available_translations[ $domain ];
     1243}
     1244
     1245/**
     1246 * Gets the path to a translation file in the languages directory for the current locale.
     1247 *
     1248 * Holds a cached list of available .mo files to improve performance.
     1249 *
     1250 * @since 4.7.0
     1251 * @access private
     1252 *
     1253 * @see _get_path_to_translation()
     1254 *
     1255 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     1256 * @return string|false The path to the translation file or false if no translation file was found.
     1257 */
     1258function _get_path_to_translation_from_lang_dir( $domain ) {
     1259        static $cached_mofiles = null;
     1260
     1261        if ( null === $cached_mofiles ) {
     1262                $cached_mofiles = array();
     1263
     1264                $locations = array(
     1265                        WP_LANG_DIR . '/plugins',
     1266                        WP_LANG_DIR . '/themes',
     1267                );
     1268
     1269                foreach ( $locations as $location ) {
     1270                        $mofiles = glob( $location . '/*.mo' );
     1271                        if ( $mofiles ) {
     1272                                $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
     1273                        }
     1274                }
     1275        }
     1276
    12481277        $locale = determine_locale();
    1249 
    1250         // Themes with their language directory outside of WP_LANG_DIR have a different file name.
    1251         $template_directory   = trailingslashit( get_template_directory() );
    1252         $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
    1253         if ( 0 === strpos( $path, $template_directory ) || 0 === strpos( $path, $stylesheet_directory ) ) {
    1254                 $mofile = "{$path}{$locale}.mo";
    1255         } else {
    1256                 $mofile = "{$path}{$domain}-{$locale}.mo";
    1257         }
    1258 
    1259         return load_textdomain( $domain, $mofile );
     1278        $mofile = "{$domain}-{$locale}.mo";
     1279
     1280        $path = WP_LANG_DIR . '/plugins/' . $mofile;
     1281        if ( in_array( $path, $cached_mofiles, true ) ) {
     1282                return $path;
     1283        }
     1284
     1285        $path = WP_LANG_DIR . '/themes/' . $mofile;
     1286        if ( in_array( $path, $cached_mofiles, true ) ) {
     1287                return $path;
     1288        }
     1289
     1290        return false;
    12601291}
    12611292
     
    12671298 * @since 2.8.0
    12681299 *
    1269  * @global MO[] $l10n An array of all currently loaded text domains.
     1300 * @global MO[] $l10n
    12701301 *
    12711302 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     
    12911322 * @since 3.0.0
    12921323 *
    1293  * @global MO[] $l10n An array of all currently loaded text domains.
     1324 * @global MO[] $l10n
    12941325 *
    12951326 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
Note: See TracChangeset for help on using the changeset viewer.