WordPress.org

Make WordPress Core

Ticket #37997: 37997.4.diff

File 37997.4.diff, 7.7 KB (added by swissspidy, 3 years ago)
  • src/wp-includes/class-wp-locale-switcher.php

    diff --git src/wp-includes/class-wp-locale-switcher.php src/wp-includes/class-wp-locale-switcher.php
    index 3d288c8..2b0638d 100644
    class WP_Locale_Switcher { 
    202202                                continue;
    203203                        }
    204204
    205                         $mofile = $l10n[ $domain ]->get_filename();
    206 
    207205                        unload_textdomain( $domain );
    208 
    209                         if ( $mofile ) {
    210                                 load_textdomain( $domain, $mofile );
    211                         }
    212 
    213206                        get_translations_for_domain( $domain );
    214207                }
    215208        }
    class WP_Locale_Switcher { 
    228221         * @param string $locale The locale to change to.
    229222         */
    230223        private function change_locale( $locale ) {
     224                // Reset translation availability information.
     225                _get_path_to_translation( null, true );
     226
    231227                $this->load_translations( $locale );
    232228
    233229                $GLOBALS['wp_locale'] = new WP_Locale();
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index 35f3582..34ee34c 100644
    function load_child_theme_textdomain( $domain, $path = false ) { 
    827827 * the translation file from `wp-content/languages`, removing the need
    828828 * to call load_plugin_texdomain() or load_theme_texdomain().
    829829 *
    830  * Holds a cached list of available .mo files to improve performance.
    831  *
    832830 * @since 4.6.0
    833831 * @access private
    834832 *
    function load_child_theme_textdomain( $domain, $path = false ) { 
    841839function _load_textdomain_just_in_time( $domain ) {
    842840        global $l10n_unloaded;
    843841
    844         $l10n_unloaded = (array) $l10n_unloaded;
    845 
    846         static $cached_mofiles = null;
     842        $l10n_unloaded    = (array) $l10n_unloaded;
    847843
    848844        // Short-circuit if domain is 'default' which is reserved for core.
    849845        if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
    850846                return false;
    851847        }
    852848
     849        $translation_path = _get_path_to_translation( $domain );
     850        if ( false === $translation_path ) {
     851                return false;
     852        }
     853
     854        return load_textdomain( $domain, $translation_path );
     855}
     856
     857/**
     858 * Gets the path to a translation file in the `wp-content/languages` directory.
     859 *
     860 * Caches the retrieved results internally.
     861 *
     862 * @since 4.7.0
     863 * @access private
     864 *
     865 * @see _load_textdomain_just_in_time()
     866 *
     867 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     868 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     869 * @return string|false The path to the translation file or false if no translation file was found.
     870 */
     871function _get_path_to_translation( $domain, $reset = false ) {
     872        static $available_translations = array();
     873
     874        if ( true === $reset ) {
     875                $available_translations = array();
     876        }
     877
     878        if ( ! isset( $available_translations[ $domain ] ) ) {
     879                $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
     880        }
     881
     882        return $available_translations[ $domain ];
     883}
     884
     885/**
     886 * Gets the path to a translation file in the languages directory for loading
     887 * a textdomain just in time.
     888 *
     889 * Holds a cached list of available .mo files to improve performance.
     890 *
     891 * @since 4.7.0
     892 * @access private
     893 *
     894 * @see _get_path_to_translation()
     895 *
     896 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     897 * @return string|false The path to the translation file or false if no translation file was found.
     898 */
     899function _get_path_to_translation_from_lang_dir( $domain ) {
     900        static $cached_mofiles = null;
     901
    853902        if ( null === $cached_mofiles ) {
    854903                $cached_mofiles = array();
    855904
    function _load_textdomain_just_in_time( $domain ) { 
    869918        $locale = is_admin() ? get_user_locale() : get_locale();
    870919        $mofile = "{$domain}-{$locale}.mo";
    871920
    872         if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
    873                 return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
     921        $path = WP_LANG_DIR . '/plugins/' . $mofile;
     922        if ( in_array( $path, $cached_mofiles ) ) {
     923                return $path;
    874924        }
    875925
    876         if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
    877                 return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
     926        $path = WP_LANG_DIR . '/themes/' . $mofile;
     927        if ( in_array( $path, $cached_mofiles ) ) {
     928                return $path;
    878929        }
    879930
    880931        return false;
  • tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    diff --git tests/phpunit/tests/l10n/loadTextdomainJustInTime.php tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
    index f0d7b55..fc2b84a 100644
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    88        protected $orig_theme_dir;
    99        protected $theme_root;
    1010        protected static $user_id;
     11        private $locale_count;
    1112
    1213        public static function wpSetUpBeforeClass( $factory ) {
    1314                self::$user_id = $factory->user->create( array(
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    2122
    2223                $this->theme_root = DIR_TESTDATA . '/themedir1';
    2324                $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
     25                $this->locale_count = 0;
    2426
    2527                // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
    2628                $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    3133                unset( $GLOBALS['wp_themes'] );
    3234                unset( $GLOBALS['l10n'] );
    3335                unset( $GLOBALS['l10n_unloaded'] );
     36                _get_path_to_translation( null, true );
    3437        }
    3538
    3639        public function tearDown() {
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    4245                unset( $GLOBALS['wp_themes'] );
    4346                unset( $GLOBALS['l10n'] );
    4447                unset( $GLOBALS['l10n_unloaded'] );
     48                _get_path_to_translation( null, true );
    4549
    4650                parent::tearDown();
    4751        }
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    163167        }
    164168
    165169        /**
     170         * @ticket 37997
     171         */
     172        public function test_plugin_translation_after_switching_locale_twice() {
     173                require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     174
     175                switch_to_locale( 'de_DE' );
     176                $expected_de_DE = i18n_plugin_test();
     177
     178                switch_to_locale( 'es_ES' );
     179                $expected_es_ES = i18n_plugin_test();
     180
     181                restore_current_locale();
     182
     183                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_de_DE );
     184                $this->assertSame( 'This is a dummy plugin', $expected_es_ES );
     185        }
     186
     187        /**
    166188         * @ticket 26511
    167189         */
    168190        public function test_theme_translation_after_switching_locale() {
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    212234
    213235                $this->assertSame( 'Das ist ein Dummy Theme', $expected );
    214236        }
     237
     238        /**
     239         * @ticket 37997
     240         */
     241        public function test_get_locale_is_called_only_once_per_textdomain() {
     242                $textdomain = 'foo-bar-baz';
     243
     244                add_filter( 'locale', array( $this, '_filter_locale_count' ) );
     245
     246                __( 'Foo', $textdomain );
     247                __( 'Bar', $textdomain );
     248                __( 'Baz', $textdomain );
     249                __( 'Foo Bar', $textdomain );
     250                __( 'Foo Bar Baz', $textdomain );
     251
     252                remove_filter( 'locale', array( $this, '_filter_locale_count' ) );
     253
     254                $this->assertFalse( is_textdomain_loaded( $textdomain ) );
     255                $this->assertSame( 1, $this->locale_count );
     256        }
     257
     258        public function _filter_locale_count( $locale ) {
     259                ++$this->locale_count;
     260
     261                return $locale;
     262        }
    215263}
  • tests/phpunit/tests/l10n/localeSwitcher.php

    diff --git tests/phpunit/tests/l10n/localeSwitcher.php tests/phpunit/tests/l10n/localeSwitcher.php
    index 262f8b1..41492bd 100644
    class Tests_Locale_Switcher extends WP_UnitTestCase { 
    2424
    2525                unset( $GLOBALS['l10n'] );
    2626                unset( $GLOBALS['l10n_unloaded'] );
     27                _get_path_to_translation( null, true );
    2728        }
    2829
    2930        public function tearDown() {
    3031                unset( $GLOBALS['l10n'] );
    3132                unset( $GLOBALS['l10n_unloaded'] );
     33                _get_path_to_translation( null, true );
    3234
    3335                parent::tearDown();
    3436        }