Make WordPress Core


Ignore:
Timestamp:
10/20/2020 04:03:58 PM (6 years ago)
Author:
ocean90
Message:

I18N: Introduce WP_Textdomain_Registry to store text domains and their language directory paths.

Previously, when using switch_to_locale() all current loaded text domains were unloaded and added to the $l10n_unloaded global. This prevented the just-in-time loading for text domains after a switch. The just-in-time loading was also only possible if the translations were stored in WP_LANG_DIR. Both issues have been fixed.

  • Adds WP_Textdomain_Registry to keep track of the language directory paths for all plugins and themes.
  • Updates all load_*_textdomain() functions to store the path in WP_Textdomain_Registry.
  • Adds $reloadable parameter to unload_textdomain() to define whether a text domain can be loaded just-in-time again. This is used by WP_Locale_Switcher::load_translations().
  • Extends _load_textdomain_just_in_time() to also support text domains of plugins and themes with custom language directories.
  • Fixes the incorrect test_plugin_translation_after_switching_locale_twice() test which should have catch this issue earlier.
  • Adds a new test plugin/theme to test the loading of translations with a custom language directory.
  • Deprecates the now unused and private _get_path_to_translation() and _get_path_to_translation_from_lang_dir() functions.

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/l10n/localeSwitcher.php

    r48939 r49236  
    2323                $this->previous_locale = '';
    2424
    25                 unset( $GLOBALS['l10n'] );
    26                 unset( $GLOBALS['l10n_unloaded'] );
    27                 _get_path_to_translation( null, true );
     25                unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
     26
     27                /** @var WP_Textdomain_Registry $wp_textdomain_registry */
     28                global $wp_textdomain_registry;
     29
     30                $wp_textdomain_registry->reset();
    2831        }
    2932
    3033        public function tearDown() {
    31                 unset( $GLOBALS['l10n'] );
    32                 unset( $GLOBALS['l10n_unloaded'] );
    33                 _get_path_to_translation( null, true );
     34                unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
     35
     36                /** @var WP_Textdomain_Registry $wp_textdomain_registry */
     37                global $wp_textdomain_registry;
     38
     39                $wp_textdomain_registry->reset();
    3440
    3541                parent::tearDown();
    … …  
    389395        }
    390396
     397        /**
     398         * @ticket 39210
     399         */
     400        public function test_switch_reloads_plugin_translations_outside_wp_lang_dir() {
     401                global $wp_locale_switcher, $wp_textdomain_registry;
     402
     403                $locale_switcher = clone $wp_locale_switcher;
     404
     405                $wp_locale_switcher = new WP_Locale_Switcher();
     406                $wp_locale_switcher->init();
     407
     408                require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
     409
     410                $this->assertSame( WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages/', $wp_textdomain_registry->get( 'custom-internationalized-plugin' ) );
     411
     412                $expected = custom_i18n_plugin_test();
     413                $this->assertSame( 'This is a dummy plugin', $expected );
     414
     415                switch_to_locale( 'es_ES' );
     416                switch_to_locale( 'de_DE' );
     417
     418                $expected = custom_i18n_plugin_test();
     419                $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
     420
     421                restore_previous_locale();
     422
     423                $expected = custom_i18n_plugin_test();
     424                $this->assertSame( 'Este es un plugin dummy', $expected );
     425
     426                restore_current_locale();
     427
     428                $wp_locale_switcher = $locale_switcher;
     429        }
     430
     431        /**
     432         * @ticket 39210
     433         */
     434        public function test_switch_reloads_theme_translations_outside_wp_lang_dir() {
     435                global $wp_locale_switcher, $wp_textdomain_registry;
     436
     437                $locale_switcher = clone $wp_locale_switcher;
     438
     439                $wp_locale_switcher = new WP_Locale_Switcher();
     440                $wp_locale_switcher->init();
     441
     442                switch_theme( 'custom-internationalized-theme' );
     443
     444                require_once get_stylesheet_directory() . '/functions.php';
     445
     446                $this->assertSame( get_template_directory() . '/languages/', $wp_textdomain_registry->get( 'custom-internationalized-theme' ) );
     447
     448                $expected = custom_i18n_theme_test();
     449
     450                $this->assertSame( 'This is a dummy theme', $expected );
     451
     452                switch_to_locale( 'es_ES' );
     453                switch_to_locale( 'de_DE' );
     454
     455                $expected = custom_i18n_theme_test();
     456                $this->assertSame( 'Das ist ein Dummy Theme', $expected );
     457
     458                restore_previous_locale();
     459
     460                $expected = custom_i18n_theme_test();
     461                $this->assertSame( 'Este es un tema dummy', $expected );
     462
     463                restore_current_locale();
     464
     465                $wp_locale_switcher = $locale_switcher;
     466        }
     467
    391468        public function filter_locale() {
    392469                return 'es_ES';
Note: See TracChangeset for help on using the changeset viewer.