WordPress.org

Make WordPress Core

Ticket #37113: 37113.2.diff

File 37113.2.diff, 5.2 KB (added by swissspidy, 5 years ago)

Allow manual reloading of text domains

  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index fa63b19..7ce5839 100644
    function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) 
    509509 *
    510510 * @since 1.5.0
    511511 *
    512  * @global array $l10n
     512 * @global array $l10n          An array of all currently loaded text domains.
     513 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
    513514 *
    514515 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    515516 * @param string $mofile Path to the .mo file.
    516517 * @return bool True on success, false on failure.
    517518 */
    518519function load_textdomain( $domain, $mofile ) {
    519         global $l10n;
     520        global $l10n, $l10n_unloaded;
     521
     522        $l10n_unloaded = (array) $l10n_unloaded;
    520523
    521524        /**
    522525         * Filters whether to override the .mo file loading.
    function load_textdomain( $domain, $mofile ) { 
    530533        $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
    531534
    532535        if ( true == $plugin_override ) {
     536                unset( $l10n_unloaded[ $domain ] );
     537
    533538                return true;
    534539        }
    535540
    function load_textdomain( $domain, $mofile ) { 
    561566        if ( isset( $l10n[$domain] ) )
    562567                $mo->merge_with( $l10n[$domain] );
    563568
     569        unset( $l10n_unloaded[ $domain ] );
     570
    564571        $l10n[$domain] = &$mo;
    565572
    566573        return true;
    function load_textdomain( $domain, $mofile ) { 
    571578 *
    572579 * @since 3.0.0
    573580 *
    574  * @global array $l10n
     581 * @global array $l10n          An array of all currently loaded text domains.
     582 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
    575583 *
    576584 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    577585 * @return bool Whether textdomain was unloaded.
    578586 */
    579587function unload_textdomain( $domain ) {
    580         global $l10n;
     588        global $l10n, $l10n_unloaded;
     589
     590        $l10n_unloaded = (array) $l10n_unloaded;
    581591
    582592        /**
    583593         * Filters whether to override the text domain unloading.
    function unload_textdomain( $domain ) { 
    589599         */
    590600        $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
    591601
    592         if ( $plugin_override )
     602        if ( $plugin_override ) {
     603                $l10n_unloaded[ $domain ] = true;
     604
    593605                return true;
     606        }
    594607
    595608        /**
    596609         * Fires before the text domain is unloaded.
    function unload_textdomain( $domain ) { 
    603616
    604617        if ( isset( $l10n[$domain] ) ) {
    605618                unset( $l10n[$domain] );
     619
     620                $l10n_unloaded[ $domain ] = true;
     621
    606622                return true;
    607623        }
    608624
    function load_child_theme_textdomain( $domain, $path = false ) { 
    793809 * @access private
    794810 *
    795811 * @see get_translations_for_domain()
     812 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
    796813 *
    797814 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    798815 * @return bool True when the textdomain is successfully loaded, false otherwise.
    799816 */
    800817function _load_textdomain_just_in_time( $domain ) {
     818        global $l10n_unloaded;
     819
     820        $l10n_unloaded = (array) $l10n_unloaded;
     821
    801822        static $cached_mofiles = null;
    802823
    803824        // Short-circuit if domain is 'default' which is reserved for core.
    804         if ( 'default' === $domain ) {
     825        if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
    805826                return false;
    806827        }
    807828
  • tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    diff --git tests/phpunit/tests/l10n/loadTextdomainJustInTime.php tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
    index 14a2b0d..3f9515b 100644
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    100100
    101101                $this->assertTrue( $translations instanceof NOOP_Translations );
    102102        }
     103
     104        /**
     105         * @ticket 37113
     106         */
     107        public function test_should_allow_unloading_of_text_domain() {
     108                add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     109
     110                require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     111
     112                load_textdomain( 'internationalized-plugin', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' );
     113
     114                $expected_output_before      = i18n_plugin_test();
     115                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-plugin' );
     116
     117                unload_textdomain( 'internationalized-plugin' );
     118                remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     119
     120                $expected_output_after      = i18n_plugin_test();
     121                $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-plugin' );
     122
     123                add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     124                load_textdomain( 'internationalized-plugin', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' );
     125
     126                $expected_output_final      = i18n_plugin_test();
     127                $is_textdomain_loaded_final = is_textdomain_loaded( 'internationalized-plugin' );
     128
     129                // Text domain loaded just in time.
     130                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output_before );
     131                $this->assertTrue( $is_textdomain_loaded_before );
     132
     133                // Text domain unloaded.
     134                $this->assertSame( 'This is a dummy plugin', $expected_output_after );
     135                $this->assertFalse( $is_textdomain_loaded_after );
     136
     137                // Text domain loaded manually again.
     138                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output_final );
     139                $this->assertTrue( $is_textdomain_loaded_final );
     140        }
    103141}