Make WordPress Core

Ticket #26511: 26511.2.patch

File 26511.2.patch, 8.4 KB (added by ocean90, 9 years ago)
  • src/wp-includes/class-wp-locale-storage.php

     
     1<?php
     2/**
     3 * Locale API: WP_Locale_Storage class
     4 *
     5 * @package WordPress
     6 * @subpackage i18n
     7 * @since 4.6.0
     8 */
     9
     10/**
     11 * Core class for locale storage objects with a getter.
     12 *
     13 * @since 4.6.0
     14 */
     15class WP_Locale_Storage {
     16
     17        /**
     18         * The stored locale.
     19         *
     20         * @since 4.6.0
     21         * @var string
     22         */
     23        private $locale;
     24
     25        /**
     26         * Constructor. Stores the given locale.
     27         *
     28         * @since 4.6.0
     29         *
     30         * @param string $locale The locale.
     31         */
     32        public function __construct( $locale ) {
     33                $this->locale = (string) $locale;
     34        }
     35
     36        /**
     37         * Returns the stored locale.
     38         *
     39         * @since 4.6.0
     40         *
     41         * @return string The stored locale.
     42         */
     43        public function get() {
     44                return $this->locale;
     45        }
     46}
  • src/wp-includes/class-wp-locale-switcher.php

     
     1<?php
     2/**
     3 * Locale API: WP_Locale_Switcher class
     4 *
     5 * @package WordPress
     6 * @subpackage i18n
     7 * @since 4.6.0
     8 */
     9
     10/**
     11 * Core class used for switching locales.
     12 *
     13 * @since 4.6.0
     14 */
     15class WP_Locale_Switcher {
     16
     17        /**
     18         * Filter callbacks.
     19         *
     20         * @since 4.6.0
     21         * @var callback[]
     22         */
     23        private $filters = array();
     24
     25        /**
     26         * Locale stack.
     27         *
     28         * @since 4.6.0
     29         * @var string[]
     30         */
     31        private $locales = array();
     32
     33        /**
     34         * Original locale.
     35         *
     36         * @since 4.6.0
     37         * @var string
     38         */
     39        private $original_locale;
     40
     41        /**
     42         * Translation objects.
     43         *
     44         * @since 4.6.0
     45         * @var NOOP_Translations[][]
     46         */
     47        private $translations = array();
     48
     49        /**
     50         * Constructor. Stores the original locale.
     51         *
     52         * @since 4.6.0
     53         */
     54        public function __construct() {
     55                $this->original_locale = get_locale();
     56        }
     57
     58        /**
     59         * Switches the translations according to the given locale.
     60         *
     61         * @since 4.6.0
     62         *
     63         * @param string $locale The locale.
     64         * @return bool True on success, false on failure.
     65         */
     66        public function switch_to_locale( $locale ) {
     67                $current_locale = get_locale();
     68                if ( $current_locale === $locale ) {
     69                        return false;
     70                }
     71
     72                /**
     73                 * @global MO[] $l10n
     74                 */
     75                global $l10n;
     76
     77                $textdomains = array_keys( $l10n );
     78
     79                if ( ! $this->has_translations_for_locale( $current_locale ) ) {
     80                        foreach ( $textdomains as $textdomain ) {
     81                                $this->translations[ $current_locale ][ $textdomain ] = get_translations_for_domain( $textdomain );
     82                        }
     83                }
     84
     85                $this->remove_filters();
     86
     87                $this->add_filter_for_locale( $locale );
     88
     89                if ( $this->has_translations_for_locale( $locale ) ) {
     90                        foreach ( $textdomains as $textdomain ) {
     91                                if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) {
     92                                        $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ];
     93                                }
     94                        }
     95                } else {
     96                        foreach ( $l10n as $textdomain => $mo ) {
     97                                if ( 'default' === $textdomain ) {
     98                                        load_default_textdomain();
     99
     100                                        continue;
     101                                }
     102
     103                                unload_textdomain( $textdomain );
     104
     105                                if ( $mofile = $mo->get_filename() ) {
     106                                        load_textdomain( $textdomain, $mofile );
     107                                }
     108
     109                                $this->translations[ $locale ][ $textdomain ] = get_translations_for_domain( $textdomain );
     110                        }
     111                }
     112
     113                /**
     114                 * @global WP_Locale $wp_locale
     115                 */
     116                $GLOBALS['wp_locale'] = new WP_Locale();
     117
     118                $this->locales[] = $locale;
     119
     120                return true;
     121        }
     122
     123        /**
     124         * Restores the translations according to the previous locale.
     125         *
     126         * @since 4.6.0
     127         *
     128         * @return string|false Locale on success, false on error.
     129         */
     130        public function restore_locale() {
     131                if ( ! array_pop( $this->locales ) ) {
     132                        // The stack is empty, bail.
     133                        return false;
     134                }
     135
     136                $this->remove_filters();
     137
     138                if ( $locale = end( $this->locales ) ) {
     139                        if ( isset( $this->filters[ $locale ] ) ) {
     140                                add_filter( 'locale', $this->filters[ $locale ] );
     141                        }
     142                } else {
     143                        // There's nothing left in the stack: go back to the original locale.
     144                        $locale = $this->original_locale;
     145                }
     146
     147                /**
     148                 * @global MO[] $l10n
     149                 */
     150                global $l10n;
     151
     152                foreach ( array_keys( $l10n ) as $textdomain ) {
     153                        if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) {
     154                                $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ];
     155                        }
     156                }
     157
     158                /**
     159                 * @global WP_Locale $wp_locale
     160                 */
     161                $GLOBALS['wp_locale'] = new WP_Locale();
     162
     163                return $locale;
     164        }
     165
     166        /**
     167         * Checks if there are cached translations for the given locale.
     168         *
     169         * @since 4.6.0
     170         *
     171         * @param string $locale The locale.
     172         * @return bool True if there are cached translations for the given locale, false otherwise.
     173         */
     174        private function has_translations_for_locale( $locale ) {
     175                return ! empty( $this->translations[ $locale ] );
     176        }
     177
     178        /**
     179         * Removes all filter callbacks added before.
     180         *
     181         * @since 4.6.0
     182         */
     183        private function remove_filters() {
     184                foreach ( $this->filters as $filter ) {
     185                        remove_filter( 'locale', $filter );
     186                }
     187        }
     188
     189        /**
     190         * Adds a filter callback returning the given locale.
     191         *
     192         * @since 4.6.0
     193         *
     194         * @param string $locale The locale.
     195         */
     196        private function add_filter_for_locale( $locale ) {
     197                if ( ! isset( $this->filters[ $locale ] ) ) {
     198                        require_once ABSPATH . WPINC . '/class-wp-locale-storage.php';
     199
     200                        // This SHOULD be a closure.
     201                        $this->filters[ $locale ] = array( new WP_Locale_Storage( $locale ), 'get' );
     202                }
     203
     204                add_filter( 'locale', $this->filters[ $locale ] );
     205        }
     206}
  • src/wp-includes/locale.php

     
    1010/** WP_Locale class */
    1111require_once ABSPATH . WPINC . '/class-wp-locale.php';
    1212
     13/** WP_Locale_Switcher class */
     14require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';
     15
    1316/**
    1417 * Checks if current locale is RTL.
    1518 *
     
    2124 */
    2225function is_rtl() {
    2326        global $wp_locale;
     27
    2428        return $wp_locale->is_rtl();
    2529}
     30
     31/**
     32 * Switches the translations according to the given locale.
     33 *
     34 * @since 4.6.0
     35 *
     36 * @global WP_Locale_Switcher $wp_locale_switcher
     37 *
     38 * @param string $locale The locale.
     39 * @return bool True on success, false on failure.
     40 */
     41function switch_to_locale( $locale ) {
     42        global $wp_locale_switcher;
     43
     44        return $wp_locale_switcher->switch_to_locale( $locale );
     45}
     46
     47/**
     48 * Restores the translations according to the previous locale.
     49 *
     50 * @since 4.6.0
     51 *
     52 * @global WP_Locale_Switcher $wp_locale_switcher
     53 *
     54 * @return string|false Locale on success, false on error.
     55 */
     56function restore_locale() {
     57        global $wp_locale_switcher;
     58
     59        return $wp_locale_switcher->restore_locale();
     60}
  • src/wp-includes/pomo/mo.php

     
    1616        var $_nplurals = 2;
    1717
    1818        /**
     19         * Loaded MO file.
     20         *
     21         * @since 4.6.0
     22         * @var string
     23         */
     24        private $filename = '';
     25
     26        /**
     27         * Returns the loaded MO file.
     28         *
     29         * @since 4.6.0
     30         *
     31         * @return string The loaded MO file.
     32         */
     33        public function get_filename() {
     34                return $this->filename;
     35        }
     36
     37        /**
    1938         * Fills up with the entries from MO file $filename
    2039         *
    2140         * @param string $filename MO file to load
     
    2443                $reader = new POMO_FileReader($filename);
    2544                if (!$reader->is_resource())
    2645                        return false;
     46                $this->filename = (string) $filename;
    2747                return $this->import_from_reader($reader);
    2848        }
    2949
     
    299319                return $this->_nplurals;
    300320        }
    301321}
    302 endif;
    303  No newline at end of file
     322endif;
  • src/wp-settings.php

     
    379379 */
    380380$GLOBALS['wp_locale'] = new WP_Locale();
    381381
     382/**
     383 * WordPress Locale Switcher object for switching locales.
     384 * @global WP_Locale_Switcher $wp_locale_switcher
     385 * @since 4.6.0
     386 */
     387$GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher();
     388
    382389// Load the functions for the active theme, for both parent and child theme if applicable.
    383390if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) {
    384391        if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )