WordPress.org

Make WordPress Core

Ticket #26511: switch_locale.patch

File switch_locale.patch, 42.9 KB (added by pbearne, 2 years ago)

updated patch with Unit tests (start off)

  • src/wp-includes/class-wp-locale-storage.php

     
     1<?php
     2/**
     3 * Simple single (locale) value storage.
     4 *
     5 * @package    WordPress
     6 * @subpackage i18n
     7 */
     8
     9/**
     10 * Class for locale storage objects with a getter.
     11 *
     12 * @since 4.6.0
     13 */
     14class WP_Locale_Storage {
     15
     16        /**
     17         * The stored locale.
     18         *
     19         * @since 4.6.0
     20         * @var string
     21         */
     22        private $locale;
     23
     24        /**
     25         * Constructor. Stores the given locale.
     26         *
     27         * @since 4.6.0
     28         *
     29         * @param string $locale The locale.
     30         */
     31        public function __construct( $locale ) {
     32
     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
     45                return $this->locale;
     46        }
     47}
  • src/wp-includes/class-wp-locale-switcher.php

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

     
     1<?php
     2/**
     3 * Date and Time Locale object
     4 *
     5 * @package WordPress
     6 * @subpackage i18n
     7 */
     8
     9/**
     10 * Class that loads the calendar locale.
     11 *
     12 * @since 2.1.0
     13 */
     14class WP_Locale {
     15        /**
     16         * Stores the translated strings for the full weekday names.
     17         *
     18         * @since 2.1.0
     19         * @var array
     20         */
     21        public $weekday;
     22
     23        /**
     24         * Stores the translated strings for the one character weekday names.
     25         *
     26         * There is a hack to make sure that Tuesday and Thursday, as well
     27         * as Sunday and Saturday, don't conflict. See init() method for more.
     28         *
     29         * @see WP_Locale::init() for how to handle the hack.
     30         *
     31         * @since 2.1.0
     32         * @var array
     33         */
     34        public $weekday_initial;
     35
     36        /**
     37         * Stores the translated strings for the abbreviated weekday names.
     38         *
     39         * @since 2.1.0
     40         * @var array
     41         */
     42        public $weekday_abbrev;
     43
     44        /**
     45         * Stores the default start of the week.
     46         *
     47         * @since 4.4.0
     48         * @var string
     49         */
     50        public $start_of_week;
     51
     52        /**
     53         * Stores the translated strings for the full month names.
     54         *
     55         * @since 2.1.0
     56         * @var array
     57         */
     58        public $month;
     59
     60        /**
     61         * Stores the translated strings for the full month names.
     62         * array( 'siječnja', 'veljače', 'ožujka', 'travnja', 'svibnja', 'lipnja', 'srpnja', 'kolovoza', 'rujna', 'listopada', 'studenoga', 'prosinca' );
     63         *
     64         * @since 4.6.0
     65         * @var array
     66         */
     67        public $month_genitive;
     68
     69        /**
     70         * Stores the translated strings for the abbreviated month names.
     71         *
     72         * @since 2.1.0
     73         * @var array
     74         */
     75        public $month_abbrev;
     76
     77        /**
     78         * Stores the translated strings for 'am' and 'pm'.
     79         *
     80         * Also the capitalized versions.
     81         *
     82         * @since 2.1.0
     83         * @var array
     84         */
     85        public $meridiem;
     86
     87        /**
     88         * The text direction of the locale language.
     89         *
     90         * Default is left to right 'ltr'.
     91         *
     92         * @since 2.1.0
     93         * @var string
     94         */
     95        public $text_direction = 'ltr';
     96
     97        /**
     98         * The thousands separator and decimal point values used for localizing numbers.
     99         *
     100         * @since 2.3.0
     101         * @access public
     102         * @var array
     103         */
     104        public $number_format;
     105
     106        /**
     107         * Sets up the translated strings and object properties.
     108         *
     109         * The method creates the translatable strings for various
     110         * calendar elements. Which allows for specifying locale
     111         * specific calendar names and text direction.
     112         *
     113         * @since 2.1.0
     114         * @access private
     115         *
     116         * @global string $text_direction
     117         * @global string $wp_version
     118         */
     119        public function init() {
     120                // The Weekdays
     121                $this->weekday[0] = /* translators: weekday */ __('Sunday');
     122                $this->weekday[1] = /* translators: weekday */ __('Monday');
     123                $this->weekday[2] = /* translators: weekday */ __('Tuesday');
     124                $this->weekday[3] = /* translators: weekday */ __('Wednesday');
     125                $this->weekday[4] = /* translators: weekday */ __('Thursday');
     126                $this->weekday[5] = /* translators: weekday */ __('Friday');
     127                $this->weekday[6] = /* translators: weekday */ __('Saturday');
     128
     129                // The first letter of each day.
     130                $this->weekday_initial[ __( 'Sunday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Sunday initial' );
     131                $this->weekday_initial[ __( 'Monday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'M', 'Monday initial' );
     132                $this->weekday_initial[ __( 'Tuesday' ) ]   = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Tuesday initial' );
     133                $this->weekday_initial[ __( 'Wednesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'W', 'Wednesday initial' );
     134                $this->weekday_initial[ __( 'Thursday' ) ]  = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Thursday initial' );
     135                $this->weekday_initial[ __( 'Friday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'F', 'Friday initial' );
     136                $this->weekday_initial[ __( 'Saturday' ) ]  = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Saturday initial' );
     137
     138                // Abbreviations for each day.
     139                $this->weekday_abbrev[__('Sunday')]    = /* translators: three-letter abbreviation of the weekday */ __('Sun');
     140                $this->weekday_abbrev[__('Monday')]    = /* translators: three-letter abbreviation of the weekday */ __('Mon');
     141                $this->weekday_abbrev[__('Tuesday')]   = /* translators: three-letter abbreviation of the weekday */ __('Tue');
     142                $this->weekday_abbrev[__('Wednesday')] = /* translators: three-letter abbreviation of the weekday */ __('Wed');
     143                $this->weekday_abbrev[__('Thursday')]  = /* translators: three-letter abbreviation of the weekday */ __('Thu');
     144                $this->weekday_abbrev[__('Friday')]    = /* translators: three-letter abbreviation of the weekday */ __('Fri');
     145                $this->weekday_abbrev[__('Saturday')]  = /* translators: three-letter abbreviation of the weekday */ __('Sat');
     146
     147                // The Months
     148                $this->month['01'] = /* translators: month name */ __( 'January' );
     149                $this->month['02'] = /* translators: month name */ __( 'February' );
     150                $this->month['03'] = /* translators: month name */ __( 'March' );
     151                $this->month['04'] = /* translators: month name */ __( 'April' );
     152                $this->month['05'] = /* translators: month name */ __( 'May' );
     153                $this->month['06'] = /* translators: month name */ __( 'June' );
     154                $this->month['07'] = /* translators: month name */ __( 'July' );
     155                $this->month['08'] = /* translators: month name */ __( 'August' );
     156                $this->month['09'] = /* translators: month name */ __( 'September' );
     157                $this->month['10'] = /* translators: month name */ __( 'October' );
     158                $this->month['11'] = /* translators: month name */ __( 'November' );
     159                $this->month['12'] = /* translators: month name */ __( 'December' );
     160
     161                // The Months, genitive
     162                $this->month_genitive['01'] = /* translators: month name, genitive */ _x( 'January', 'genitive' );
     163                $this->month_genitive['02'] = /* translators: month name, genitive */ _x( 'February', 'genitive' );
     164                $this->month_genitive['03'] = /* translators: month name, genitive */ _x( 'March', 'genitive' );
     165                $this->month_genitive['04'] = /* translators: month name, genitive */ _x( 'April', 'genitive' );
     166                $this->month_genitive['05'] = /* translators: month name, genitive */ _x( 'May', 'genitive' );
     167                $this->month_genitive['06'] = /* translators: month name, genitive */ _x( 'June', 'genitive' );
     168                $this->month_genitive['07'] = /* translators: month name, genitive */ _x( 'July', 'genitive' );
     169                $this->month_genitive['08'] = /* translators: month name, genitive */ _x( 'August', 'genitive' );
     170                $this->month_genitive['09'] = /* translators: month name, genitive */ _x( 'September', 'genitive' );
     171                $this->month_genitive['10'] = /* translators: month name, genitive */ _x( 'October', 'genitive' );
     172                $this->month_genitive['11'] = /* translators: month name, genitive */ _x( 'November', 'genitive' );
     173                $this->month_genitive['12'] = /* translators: month name, genitive */ _x( 'December', 'genitive' );
     174
     175                // Abbreviations for each month.
     176                $this->month_abbrev[ __( 'January' ) ]   = /* translators: three-letter abbreviation of the month */ _x( 'Jan', 'January abbreviation' );
     177                $this->month_abbrev[ __( 'February' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Feb', 'February abbreviation' );
     178                $this->month_abbrev[ __( 'March' ) ]     = /* translators: three-letter abbreviation of the month */ _x( 'Mar', 'March abbreviation' );
     179                $this->month_abbrev[ __( 'April' ) ]     = /* translators: three-letter abbreviation of the month */ _x( 'Apr', 'April abbreviation' );
     180                $this->month_abbrev[ __( 'May' ) ]       = /* translators: three-letter abbreviation of the month */ _x( 'May', 'May abbreviation' );
     181                $this->month_abbrev[ __( 'June' ) ]      = /* translators: three-letter abbreviation of the month */ _x( 'Jun', 'June abbreviation' );
     182                $this->month_abbrev[ __( 'July' ) ]      = /* translators: three-letter abbreviation of the month */ _x( 'Jul', 'July abbreviation' );
     183                $this->month_abbrev[ __( 'August' ) ]    = /* translators: three-letter abbreviation of the month */ _x( 'Aug', 'August abbreviation' );
     184                $this->month_abbrev[ __( 'September' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Sep', 'September abbreviation' );
     185                $this->month_abbrev[ __( 'October' ) ]   = /* translators: three-letter abbreviation of the month */ _x( 'Oct', 'October abbreviation' );
     186                $this->month_abbrev[ __( 'November' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Nov', 'November abbreviation' );
     187                $this->month_abbrev[ __( 'December' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Dec', 'December abbreviation' );
     188
     189                // The Meridiems
     190                $this->meridiem['am'] = __('am');
     191                $this->meridiem['pm'] = __('pm');
     192                $this->meridiem['AM'] = __('AM');
     193                $this->meridiem['PM'] = __('PM');
     194
     195                // Numbers formatting
     196                // See http://php.net/number_format
     197
     198                /* translators: $thousands_sep argument for http://php.net/number_format, default is , */
     199                $thousands_sep = __( 'number_format_thousands_sep' );
     200
     201                if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
     202                        // Replace space with a non-breaking space to avoid wrapping.
     203                        $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
     204                } else {
     205                        // PHP < 5.4.0 does not support multiple bytes in thousands separator.
     206                        $thousands_sep = str_replace( array( '&nbsp;', '&#160;' ), ' ', $thousands_sep );
     207                }
     208
     209                $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
     210
     211                /* translators: $dec_point argument for http://php.net/number_format, default is . */
     212                $decimal_point = __( 'number_format_decimal_point' );
     213
     214                $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
     215
     216                // Set text direction.
     217                if ( isset( $GLOBALS['text_direction'] ) )
     218                        $this->text_direction = $GLOBALS['text_direction'];
     219                /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
     220                elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
     221                        $this->text_direction = 'rtl';
     222
     223                if ( 'rtl' === $this->text_direction && strpos( $GLOBALS['wp_version'], '-src' ) ) {
     224                        $this->text_direction = 'ltr';
     225                        add_action( 'all_admin_notices', array( $this, 'rtl_src_admin_notice' ) );
     226                }
     227        }
     228
     229        /**
     230         * Outputs an admin notice if the /build directory must be used for RTL.
     231         *
     232         * @since 3.8.0
     233         * @access public
     234         */
     235        public function rtl_src_admin_notice() {
     236                /* translators: %s: Name of the directory (build) */
     237                echo '<div class="error"><p>' . sprintf( __( 'The %s directory of the develop repository must be used for RTL.' ), '<code>build</code>' ) . '</p></div>';
     238        }
     239
     240        /**
     241         * Retrieve the full translated weekday word.
     242         *
     243         * Week starts on translated Sunday and can be fetched
     244         * by using 0 (zero). So the week starts with 0 (zero)
     245         * and ends on Saturday with is fetched by using 6 (six).
     246         *
     247         * @since 2.1.0
     248         * @access public
     249         *
     250         * @param int $weekday_number 0 for Sunday through 6 Saturday
     251         * @return string Full translated weekday
     252         */
     253        public function get_weekday($weekday_number) {
     254                return $this->weekday[$weekday_number];
     255        }
     256
     257        /**
     258         * Retrieve the translated weekday initial.
     259         *
     260         * The weekday initial is retrieved by the translated
     261         * full weekday word. When translating the weekday initial
     262         * pay attention to make sure that the starting letter does
     263         * not conflict.
     264         *
     265         * @since 2.1.0
     266         * @access public
     267         *
     268         * @param string $weekday_name
     269         * @return string
     270         */
     271        public function get_weekday_initial($weekday_name) {
     272                return $this->weekday_initial[$weekday_name];
     273        }
     274
     275        /**
     276         * Retrieve the translated weekday abbreviation.
     277         *
     278         * The weekday abbreviation is retrieved by the translated
     279         * full weekday word.
     280         *
     281         * @since 2.1.0
     282         * @access public
     283         *
     284         * @param string $weekday_name Full translated weekday word
     285         * @return string Translated weekday abbreviation
     286         */
     287        public function get_weekday_abbrev($weekday_name) {
     288                return $this->weekday_abbrev[$weekday_name];
     289        }
     290
     291        /**
     292         * Retrieve the full translated month by month number.
     293         *
     294         * The $month_number parameter has to be a string
     295         * because it must have the '0' in front of any number
     296         * that is less than 10. Starts from '01' and ends at
     297         * '12'.
     298         *
     299         * You can use an integer instead and it will add the
     300         * '0' before the numbers less than 10 for you.
     301         *
     302         * @since 2.1.0
     303         * @access public
     304         *
     305         * @param string|int $month_number '01' through '12'
     306         * @return string Translated full month name
     307         */
     308        public function get_month($month_number) {
     309                return $this->month[zeroise($month_number, 2)];
     310        }
     311
     312        /**
     313         * Retrieve translated version of month abbreviation string.
     314         *
     315         * The $month_name parameter is expected to be the translated or
     316         * translatable version of the month.
     317         *
     318         * @since 2.1.0
     319         * @access public
     320         *
     321         * @param string $month_name Translated month to get abbreviated version
     322         * @return string Translated abbreviated month
     323         */
     324        public function get_month_abbrev($month_name) {
     325                return $this->month_abbrev[$month_name];
     326        }
     327
     328        /**
     329         * Retrieve translated version of meridiem string.
     330         *
     331         * The $meridiem parameter is expected to not be translated.
     332         *
     333         * @since 2.1.0
     334         * @access public
     335         *
     336         * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
     337         * @return string Translated version
     338         */
     339        public function get_meridiem($meridiem) {
     340                return $this->meridiem[$meridiem];
     341        }
     342
     343        /**
     344         * Global variables are deprecated.
     345         *
     346         * For backward compatibility only.
     347         *
     348         * @deprecated For backward compatibility only.
     349         * @access private
     350         *
     351         * @global array $weekday
     352         * @global array $weekday_initial
     353         * @global array $weekday_abbrev
     354         * @global array $month
     355         * @global array $month_abbrev
     356         *
     357         * @since 2.1.0
     358         */
     359        public function register_globals() {
     360                $GLOBALS['weekday']         = $this->weekday;
     361                $GLOBALS['weekday_initial'] = $this->weekday_initial;
     362                $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
     363                $GLOBALS['month']           = $this->month;
     364                $GLOBALS['month_abbrev']    = $this->month_abbrev;
     365        }
     366
     367        /**
     368         * Constructor which calls helper methods to set up object variables
     369         *
     370         * @since 2.1.0
     371         */
     372        public function __construct() {
     373                $this->init();
     374                $this->register_globals();
     375        }
     376
     377        /**
     378         * Checks if current locale is RTL.
     379         *
     380         * @since 3.0.0
     381         * @return bool Whether locale is RTL.
     382         */
     383        public function is_rtl() {
     384                return 'rtl' == $this->text_direction;
     385        }
     386
     387        /**
     388         * Register date/time format strings for general POT.
     389         *
     390         * Private, unused method to add some date/time formats translated
     391         * on wp-admin/options-general.php to the general POT that would
     392         * otherwise be added to the admin POT.
     393         *
     394         * @since 3.6.0
     395         */
     396        public function _strings_for_pot() {
     397                /* translators: localized date format, see http://php.net/date */
     398                __( 'F j, Y' );
     399                /* translators: localized time format, see http://php.net/date */
     400                __( 'g:i a' );
     401                /* translators: localized date and time format, see http://php.net/date */
     402                __( 'F j, Y g:i a' );
     403        }
     404}
  • src/wp-includes/locale.php

     
    11<?php
    2 /**
    3  * Date and Time Locale object
    4  *
    5  * @package WordPress
    6  * @subpackage i18n
    7  */
    82
    9 /**
    10  * Class that loads the calendar locale.
    11  *
    12  * @since 2.1.0
    13  */
    14 class WP_Locale {
    15         /**
    16          * Stores the translated strings for the full weekday names.
    17          *
    18          * @since 2.1.0
    19          * @var array
    20          */
    21         public $weekday;
     3$dir = dirname( __FILE__ );
    224
    23         /**
    24          * Stores the translated strings for the one character weekday names.
    25          *
    26          * There is a hack to make sure that Tuesday and Thursday, as well
    27          * as Sunday and Saturday, don't conflict. See init() method for more.
    28          *
    29          * @see WP_Locale::init() for how to handle the hack.
    30          *
    31          * @since 2.1.0
    32          * @var array
    33          */
    34         public $weekday_initial;
     5require_once "$dir/class-wp-locale.php";
    356
    36         /**
    37          * Stores the translated strings for the abbreviated weekday names.
    38          *
    39          * @since 2.1.0
    40          * @var array
    41          */
    42         public $weekday_abbrev;
     7require_once "$dir/class-wp-locale-switcher.php";
    438
    44         /**
    45          * Stores the default start of the week.
    46          *
    47          * @since 4.4.0
    48          * @var string
    49          */
    50         public $start_of_week;
     9unset( $dir );
    5110
    52         /**
    53          * Stores the translated strings for the full month names.
    54          *
    55          * @since 2.1.0
    56          * @var array
    57          */
    58         public $month;
    59 
    60         /**
    61          * Stores the translated strings for the abbreviated month names.
    62          *
    63          * @since 2.1.0
    64          * @var array
    65          */
    66         public $month_abbrev;
    67 
    68         /**
    69          * Stores the translated strings for 'am' and 'pm'.
    70          *
    71          * Also the capitalized versions.
    72          *
    73          * @since 2.1.0
    74          * @var array
    75          */
    76         public $meridiem;
    77 
    78         /**
    79          * The text direction of the locale language.
    80          *
    81          * Default is left to right 'ltr'.
    82          *
    83          * @since 2.1.0
    84          * @var string
    85          */
    86         public $text_direction = 'ltr';
    87 
    88         /**
    89          * The thousands separator and decimal point values used for localizing numbers.
    90          *
    91          * @since 2.3.0
    92          * @access public
    93          * @var array
    94          */
    95         public $number_format;
    96 
    97         /**
    98          * Sets up the translated strings and object properties.
    99          *
    100          * The method creates the translatable strings for various
    101          * calendar elements. Which allows for specifying locale
    102          * specific calendar names and text direction.
    103          *
    104          * @since 2.1.0
    105          * @access private
    106          *
    107          * @global string $text_direction
    108          * @global string $wp_version
    109          */
    110         public function init() {
    111                 // The Weekdays
    112                 $this->weekday[0] = /* translators: weekday */ __('Sunday');
    113                 $this->weekday[1] = /* translators: weekday */ __('Monday');
    114                 $this->weekday[2] = /* translators: weekday */ __('Tuesday');
    115                 $this->weekday[3] = /* translators: weekday */ __('Wednesday');
    116                 $this->weekday[4] = /* translators: weekday */ __('Thursday');
    117                 $this->weekday[5] = /* translators: weekday */ __('Friday');
    118                 $this->weekday[6] = /* translators: weekday */ __('Saturday');
    119 
    120                 // The first letter of each day.
    121                 $this->weekday_initial[ __( 'Sunday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Sunday initial' );
    122                 $this->weekday_initial[ __( 'Monday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'M', 'Monday initial' );
    123                 $this->weekday_initial[ __( 'Tuesday' ) ]   = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Tuesday initial' );
    124                 $this->weekday_initial[ __( 'Wednesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'W', 'Wednesday initial' );
    125                 $this->weekday_initial[ __( 'Thursday' ) ]  = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Thursday initial' );
    126                 $this->weekday_initial[ __( 'Friday' ) ]    = /* translators: one-letter abbreviation of the weekday */ _x( 'F', 'Friday initial' );
    127                 $this->weekday_initial[ __( 'Saturday' ) ]  = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Saturday initial' );
    128 
    129                 // Abbreviations for each day.
    130                 $this->weekday_abbrev[__('Sunday')]    = /* translators: three-letter abbreviation of the weekday */ __('Sun');
    131                 $this->weekday_abbrev[__('Monday')]    = /* translators: three-letter abbreviation of the weekday */ __('Mon');
    132                 $this->weekday_abbrev[__('Tuesday')]   = /* translators: three-letter abbreviation of the weekday */ __('Tue');
    133                 $this->weekday_abbrev[__('Wednesday')] = /* translators: three-letter abbreviation of the weekday */ __('Wed');
    134                 $this->weekday_abbrev[__('Thursday')]  = /* translators: three-letter abbreviation of the weekday */ __('Thu');
    135                 $this->weekday_abbrev[__('Friday')]    = /* translators: three-letter abbreviation of the weekday */ __('Fri');
    136                 $this->weekday_abbrev[__('Saturday')]  = /* translators: three-letter abbreviation of the weekday */ __('Sat');
    137 
    138                 // The Months
    139                 $this->month['01'] = /* translators: month name */ __( 'January' );
    140                 $this->month['02'] = /* translators: month name */ __( 'February' );
    141                 $this->month['03'] = /* translators: month name */ __( 'March' );
    142                 $this->month['04'] = /* translators: month name */ __( 'April' );
    143                 $this->month['05'] = /* translators: month name */ __( 'May' );
    144                 $this->month['06'] = /* translators: month name */ __( 'June' );
    145                 $this->month['07'] = /* translators: month name */ __( 'July' );
    146                 $this->month['08'] = /* translators: month name */ __( 'August' );
    147                 $this->month['09'] = /* translators: month name */ __( 'September' );
    148                 $this->month['10'] = /* translators: month name */ __( 'October' );
    149                 $this->month['11'] = /* translators: month name */ __( 'November' );
    150                 $this->month['12'] = /* translators: month name */ __( 'December' );
    151 
    152                 // The Months, genitive
    153                 $this->month_genitive['01'] = /* translators: month name, genitive */ _x( 'January', 'genitive' );
    154                 $this->month_genitive['02'] = /* translators: month name, genitive */ _x( 'February', 'genitive' );
    155                 $this->month_genitive['03'] = /* translators: month name, genitive */ _x( 'March', 'genitive' );
    156                 $this->month_genitive['04'] = /* translators: month name, genitive */ _x( 'April', 'genitive' );
    157                 $this->month_genitive['05'] = /* translators: month name, genitive */ _x( 'May', 'genitive' );
    158                 $this->month_genitive['06'] = /* translators: month name, genitive */ _x( 'June', 'genitive' );
    159                 $this->month_genitive['07'] = /* translators: month name, genitive */ _x( 'July', 'genitive' );
    160                 $this->month_genitive['08'] = /* translators: month name, genitive */ _x( 'August', 'genitive' );
    161                 $this->month_genitive['09'] = /* translators: month name, genitive */ _x( 'September', 'genitive' );
    162                 $this->month_genitive['10'] = /* translators: month name, genitive */ _x( 'October', 'genitive' );
    163                 $this->month_genitive['11'] = /* translators: month name, genitive */ _x( 'November', 'genitive' );
    164                 $this->month_genitive['12'] = /* translators: month name, genitive */ _x( 'December', 'genitive' );
    165 
    166                 // Abbreviations for each month.
    167                 $this->month_abbrev[ __( 'January' ) ]   = /* translators: three-letter abbreviation of the month */ _x( 'Jan', 'January abbreviation' );
    168                 $this->month_abbrev[ __( 'February' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Feb', 'February abbreviation' );
    169                 $this->month_abbrev[ __( 'March' ) ]     = /* translators: three-letter abbreviation of the month */ _x( 'Mar', 'March abbreviation' );
    170                 $this->month_abbrev[ __( 'April' ) ]     = /* translators: three-letter abbreviation of the month */ _x( 'Apr', 'April abbreviation' );
    171                 $this->month_abbrev[ __( 'May' ) ]       = /* translators: three-letter abbreviation of the month */ _x( 'May', 'May abbreviation' );
    172                 $this->month_abbrev[ __( 'June' ) ]      = /* translators: three-letter abbreviation of the month */ _x( 'Jun', 'June abbreviation' );
    173                 $this->month_abbrev[ __( 'July' ) ]      = /* translators: three-letter abbreviation of the month */ _x( 'Jul', 'July abbreviation' );
    174                 $this->month_abbrev[ __( 'August' ) ]    = /* translators: three-letter abbreviation of the month */ _x( 'Aug', 'August abbreviation' );
    175                 $this->month_abbrev[ __( 'September' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Sep', 'September abbreviation' );
    176                 $this->month_abbrev[ __( 'October' ) ]   = /* translators: three-letter abbreviation of the month */ _x( 'Oct', 'October abbreviation' );
    177                 $this->month_abbrev[ __( 'November' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Nov', 'November abbreviation' );
    178                 $this->month_abbrev[ __( 'December' ) ]  = /* translators: three-letter abbreviation of the month */ _x( 'Dec', 'December abbreviation' );
    179 
    180                 // The Meridiems
    181                 $this->meridiem['am'] = __('am');
    182                 $this->meridiem['pm'] = __('pm');
    183                 $this->meridiem['AM'] = __('AM');
    184                 $this->meridiem['PM'] = __('PM');
    185 
    186                 // Numbers formatting
    187                 // See http://php.net/number_format
    188 
    189                 /* translators: $thousands_sep argument for http://php.net/number_format, default is , */
    190                 $thousands_sep = __( 'number_format_thousands_sep' );
    191 
    192                 if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
    193                         // Replace space with a non-breaking space to avoid wrapping.
    194                         $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
    195                 } else {
    196                         // PHP < 5.4.0 does not support multiple bytes in thousands separator.
    197                         $thousands_sep = str_replace( array( '&nbsp;', '&#160;' ), ' ', $thousands_sep );
    198                 }
    199 
    200                 $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
    201 
    202                 /* translators: $dec_point argument for http://php.net/number_format, default is . */
    203                 $decimal_point = __( 'number_format_decimal_point' );
    204 
    205                 $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
    206 
    207                 // Set text direction.
    208                 if ( isset( $GLOBALS['text_direction'] ) )
    209                         $this->text_direction = $GLOBALS['text_direction'];
    210                 /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
    211                 elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
    212                         $this->text_direction = 'rtl';
    213 
    214                 if ( 'rtl' === $this->text_direction && strpos( $GLOBALS['wp_version'], '-src' ) ) {
    215                         $this->text_direction = 'ltr';
    216                         add_action( 'all_admin_notices', array( $this, 'rtl_src_admin_notice' ) );
    217                 }
    218         }
    219 
    220         /**
    221          * Outputs an admin notice if the /build directory must be used for RTL.
    222          *
    223          * @since 3.8.0
    224          * @access public
    225          */
    226         public function rtl_src_admin_notice() {
    227                 /* translators: %s: Name of the directory (build) */
    228                 echo '<div class="error"><p>' . sprintf( __( 'The %s directory of the develop repository must be used for RTL.' ), '<code>build</code>' ) . '</p></div>';
    229         }
    230 
    231         /**
    232          * Retrieve the full translated weekday word.
    233          *
    234          * Week starts on translated Sunday and can be fetched
    235          * by using 0 (zero). So the week starts with 0 (zero)
    236          * and ends on Saturday with is fetched by using 6 (six).
    237          *
    238          * @since 2.1.0
    239          * @access public
    240          *
    241          * @param int $weekday_number 0 for Sunday through 6 Saturday
    242          * @return string Full translated weekday
    243          */
    244         public function get_weekday($weekday_number) {
    245                 return $this->weekday[$weekday_number];
    246         }
    247 
    248         /**
    249          * Retrieve the translated weekday initial.
    250          *
    251          * The weekday initial is retrieved by the translated
    252          * full weekday word. When translating the weekday initial
    253          * pay attention to make sure that the starting letter does
    254          * not conflict.
    255          *
    256          * @since 2.1.0
    257          * @access public
    258          *
    259          * @param string $weekday_name
    260          * @return string
    261          */
    262         public function get_weekday_initial($weekday_name) {
    263                 return $this->weekday_initial[$weekday_name];
    264         }
    265 
    266         /**
    267          * Retrieve the translated weekday abbreviation.
    268          *
    269          * The weekday abbreviation is retrieved by the translated
    270          * full weekday word.
    271          *
    272          * @since 2.1.0
    273          * @access public
    274          *
    275          * @param string $weekday_name Full translated weekday word
    276          * @return string Translated weekday abbreviation
    277          */
    278         public function get_weekday_abbrev($weekday_name) {
    279                 return $this->weekday_abbrev[$weekday_name];
    280         }
    281 
    282         /**
    283          * Retrieve the full translated month by month number.
    284          *
    285          * The $month_number parameter has to be a string
    286          * because it must have the '0' in front of any number
    287          * that is less than 10. Starts from '01' and ends at
    288          * '12'.
    289          *
    290          * You can use an integer instead and it will add the
    291          * '0' before the numbers less than 10 for you.
    292          *
    293          * @since 2.1.0
    294          * @access public
    295          *
    296          * @param string|int $month_number '01' through '12'
    297          * @return string Translated full month name
    298          */
    299         public function get_month($month_number) {
    300                 return $this->month[zeroise($month_number, 2)];
    301         }
    302 
    303         /**
    304          * Retrieve translated version of month abbreviation string.
    305          *
    306          * The $month_name parameter is expected to be the translated or
    307          * translatable version of the month.
    308          *
    309          * @since 2.1.0
    310          * @access public
    311          *
    312          * @param string $month_name Translated month to get abbreviated version
    313          * @return string Translated abbreviated month
    314          */
    315         public function get_month_abbrev($month_name) {
    316                 return $this->month_abbrev[$month_name];
    317         }
    318 
    319         /**
    320          * Retrieve translated version of meridiem string.
    321          *
    322          * The $meridiem parameter is expected to not be translated.
    323          *
    324          * @since 2.1.0
    325          * @access public
    326          *
    327          * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
    328          * @return string Translated version
    329          */
    330         public function get_meridiem($meridiem) {
    331                 return $this->meridiem[$meridiem];
    332         }
    333 
    334         /**
    335          * Global variables are deprecated.
    336          *
    337          * For backward compatibility only.
    338          *
    339          * @deprecated For backward compatibility only.
    340          * @access private
    341          *
    342          * @global array $weekday
    343          * @global array $weekday_initial
    344          * @global array $weekday_abbrev
    345          * @global array $month
    346          * @global array $month_abbrev
    347          *
    348          * @since 2.1.0
    349          */
    350         public function register_globals() {
    351                 $GLOBALS['weekday']         = $this->weekday;
    352                 $GLOBALS['weekday_initial'] = $this->weekday_initial;
    353                 $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
    354                 $GLOBALS['month']           = $this->month;
    355                 $GLOBALS['month_abbrev']    = $this->month_abbrev;
    356         }
    357 
    358         /**
    359          * Constructor which calls helper methods to set up object variables
    360          *
    361          * @since 2.1.0
    362          */
    363         public function __construct() {
    364                 $this->init();
    365                 $this->register_globals();
    366         }
    367 
    368         /**
    369          * Checks if current locale is RTL.
    370          *
    371          * @since 3.0.0
    372          * @return bool Whether locale is RTL.
    373          */
    374         public function is_rtl() {
    375                 return 'rtl' == $this->text_direction;
    376         }
    377 
    378         /**
    379          * Register date/time format strings for general POT.
    380          *
    381          * Private, unused method to add some date/time formats translated
    382          * on wp-admin/options-general.php to the general POT that would
    383          * otherwise be added to the admin POT.
    384          *
    385          * @since 3.6.0
    386          */
    387         public function _strings_for_pot() {
    388                 /* translators: localized date format, see http://php.net/date */
    389                 __( 'F j, Y' );
    390                 /* translators: localized time format, see http://php.net/date */
    391                 __( 'g:i a' );
    392                 /* translators: localized date and time format, see http://php.net/date */
    393                 __( 'F j, Y g:i a' );
    394         }
    395 }
    396 
    39711/**
    39812 * Checks if current locale is RTL.
    39913 *
     
    40721        global $wp_locale;
    40822        return $wp_locale->is_rtl();
    40923}
     24
     25/**
     26 * Switches the translations according to the given locale.
     27 *
     28 * @since 4.6.0
     29 *
     30 * @param string $locale The locale.
     31 */
     32function switch_to_locale( $locale ) {
     33
     34        /**
     35         * @global WP_Locale_Switcher $wp_locale_switcher
     36         */
     37        global $wp_locale_switcher;
     38
     39        return $wp_locale_switcher->switch_to_locale( $locale );
     40}
     41
     42/**
     43 * Restores the translations according to the previous locale.
     44 *
     45 * @since 4.6.0
     46 *
     47 * @return string|false Locale on success, false on error.
     48 */
     49function restore_locale( $reset = false ) {
     50
     51        /**
     52         * @global WP_Locale_Switcher $wp_locale_switcher
     53         */
     54        global $wp_locale_switcher;
     55
     56        return $wp_locale_switcher->restore_locale( $reset );
     57}
  • 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
     35                return $this->filename;
     36        }
     37
     38        /**
    1939         * Fills up with the entries from MO file $filename
    2040         *
    2141         * @param string $filename MO file to load
     
    2444                $reader = new POMO_FileReader($filename);
    2545                if (!$reader->is_resource())
    2646                        return false;
     47                $this->filename = (string) $filename;
    2748                return $this->import_from_reader($reader);
    2849        }
    2950
     
    299320                return $this->_nplurals;
    300321        }
    301322}
    302 endif;
    303  No newline at end of file
     323endif;
  • src/wp-settings.php

     
    367367 */
    368368$GLOBALS['wp_locale'] = new WP_Locale();
    369369
     370/**
     371 * WordPress Locale Switcher object for switching locales.
     372 * @global WP_Locale_Switcher $wp_locale_switcher
     373 * @since 4.6.0
     374 */
     375$GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher();
     376
    370377// Load the functions for the active theme, for both parent and child theme if applicable.
    371378if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) {
    372379        if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
  • tests/phpunit/tests/locale/class-wp-switch-locale.php

     
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_class_wp_switch_locale extends WP_UnitTestCase {
     8
     9        function test_switches_to_when_no_I18N() {
     10
     11                $this->assertFalse( switch_to_locale( 'es_ES' ) );
     12        }
     13
     14        function test_switches_to_es() {
     15                // We need to something in order to set the Global $l10n
     16                $file = DIR_TESTDATA . '/pomo/simple.mo';
     17                 load_textdomain( 'wp-tests-domain', $file );
     18
     19
     20                $this->assertEquals( 'es_ES', switch_to_locale( 'es_ES' ) );
     21
     22                $this->assertEquals( 'es_ES', get_locale() );
     23
     24                $this->assertEquals( 'en_GB', switch_to_locale( 'en_GB' ) );
     25
     26                $this->assertEquals( 'en_GB', get_locale() );
     27
     28                // return early if you try to switch to the same locale
     29                $this->assertEquals( 'en_GB', switch_to_locale( 'en_GB' ) );
     30
     31                $this->assertEquals( 'en_GB', get_locale() );
     32
     33                $this->assertEquals( 'es_ES', restore_locale() );
     34
     35                $this->assertEquals( 'es_ES', get_locale() );
     36
     37                $this->assertEquals( 'en_US', restore_locale( true ) );
     38
     39                $this->assertEquals( 'en_US', get_locale() );
     40
     41                $this->assertEquals( 'xx_XX', switch_to_locale( 'xx_XX' ) );
     42
     43                $this->assertEquals( 'xx_XX', get_locale() );
     44
     45                // tidy up
     46                unload_textdomain( 'wp-tests-domain' );
     47                global $l10n;
     48                $l10n = null;
     49        }
     50
     51
     52        function test_switches_to_when_no_I18N_after() {
     53
     54                $this->assertFalse( switch_to_locale( 'es_ES' ) );
     55                $this->assertFalse( switch_to_locale( 'en_GB' ) );
     56        }
     57}
  • tests/phpunit/tests/locale/test-locale.php

     
     1<?php
     2/**
     3 * wordpress-develop.
     4 * User: Paul
     5 * Date: 2016-05-16
     6 *
     7 */
     8
     9if ( !defined( 'WPINC' ) ) {
     10        die;
     11}
     12
     13class test_Locale extends WP_UnitTestCase {
     14
     15//      static $fr_lanf_files = array(
     16//              'admin-fr_FR.mo',
     17//              'admin-fr_FR.po',
     18//              'admin-network-fr_FR.mo',
     19//              'admin-network-fr_FR.po',
     20//              'continents-cities-fr_FR.mo',
     21//              'continents-cities-fr_FR.po',
     22//              'fr_FR.mo',
     23//              'fr_FR.po',
     24//      );
     25//
     26//
     27//      /**
     28//       * pass a new locale into WP_locale and reset current locale
     29//       */
     30//      function test_WP_locale(){
     31//
     32//              global $GLOBALS;
     33//
     34//              $GLOBALS['wp_locale'] = new WP_Locale( 'fr_FR' );
     35//
     36//              $this->assertEquals( 'fr_FR', get_locale() );
     37//
     38//              $this->assertEquals( 'dimanche', $GLOBALS['wp_locale']->weekday[0] );
     39//
     40//              foreach ( self::$fr_lanf_files as $file ) {
     41//                      $this->assertEquals( true, file_exists( WP_LANG_DIR . '/' . $file ) );
     42//              }
     43//
     44//              // remove any fr lang files
     45//              $this->remove_lang_files();
     46//      }
     47//
     48//      function remove_lang_files() {
     49//              foreach ( self::$fr_lanf_files as $file ) {
     50//                      $this->unlink( WP_LANG_DIR . '/' . $file );
     51//              }
     52//      }
     53}
  • tests/phpunit/tests/locale/class-wp-switch-locale.php

     
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_class_wp_switch_locale extends WP_UnitTestCase {
     8
     9        function test_switches_to_when_no_I18N() {
     10
     11                $this->assertFalse( switch_to_locale( 'es_ES' ) );
     12        }
     13
     14        function test_switches_to_es() {
     15                // We need to something in order to set the Global $l10n
     16                $file = DIR_TESTDATA . '/pomo/simple.mo';
     17                 load_textdomain( 'wp-tests-domain', $file );
     18
     19
     20                $this->assertEquals( 'es_ES', switch_to_locale( 'es_ES' ) );
     21
     22                $this->assertEquals( 'es_ES', get_locale() );
     23
     24                $this->assertEquals( 'en_GB', switch_to_locale( 'en_GB' ) );
     25
     26                $this->assertEquals( 'en_GB', get_locale() );
     27
     28                // return early if you try to switch to the same locale
     29                $this->assertEquals( 'en_GB', switch_to_locale( 'en_GB' ) );
     30
     31                $this->assertEquals( 'en_GB', get_locale() );
     32
     33                $this->assertEquals( 'es_ES', restore_locale() );
     34
     35                $this->assertEquals( 'es_ES', get_locale() );
     36
     37                $this->assertEquals( 'en_US', restore_locale( true ) );
     38
     39                $this->assertEquals( 'en_US', get_locale() );
     40
     41                $this->assertEquals( 'xx_XX', switch_to_locale( 'xx_XX' ) );
     42
     43                $this->assertEquals( 'xx_XX', get_locale() );
     44
     45                // tidy up
     46                unload_textdomain( 'wp-tests-domain' );
     47                global $l10n;
     48                $l10n = null;
     49        }
     50
     51
     52        function test_switches_to_when_no_I18N_after() {
     53
     54                $this->assertFalse( switch_to_locale( 'es_ES' ) );
     55                $this->assertFalse( switch_to_locale( 'en_GB' ) );
     56        }
     57}