Make WordPress Core

Changeset 55928


Ignore:
Timestamp:
06/16/2023 12:56:03 PM (3 years ago)
Author:
swissspidy
Message:

I18N: Allow to short-circuit load_textdomain().

Introduces a new pre_load_textdomain filter, which is useful for plugins to develop and test alternative loading/caching strategies for translations. This brings consistency with the existing pre_load_script_translations filter for JavaScript translations.

Props ocean90, swissspidy.
Fixes #58035.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r55865 r55928  
    717717
    718718        $l10n_unloaded = (array) $l10n_unloaded;
     719
     720        /**
     721         * Filters whether to short-circuit loading .mo file.
     722         *
     723         * Returning a non-null value from the filter will effectively short-circuit
     724         * the loading, returning the passed value instead.
     725         *
     726         * @since 6.3.0
     727         *
     728         * @param bool|null   $loaded The result of loading a .mo file. Default null.
     729         * @param string      $domain Text domain. Unique identifier for retrieving translated strings.
     730         * @param string      $mofile Path to the MO file.
     731         * @param string|null $locale Locale.
     732         */
     733        $loaded = apply_filters( 'pre_load_textdomain', null, $domain, $mofile, $locale );
     734        if ( null !== $loaded ) {
     735                if ( true === $loaded ) {
     736                        unset( $l10n_unloaded[ $domain ] );
     737                }
     738
     739                return $loaded;
     740        }
    719741
    720742        /**
  • trunk/tests/phpunit/tests/l10n/loadTextdomain.php

    r54669 r55928  
    301301                $this->assertSame( get_user_locale(), $this->locale );
    302302        }
     303
     304        /**
     305         * @ticket 58035
     306         *
     307         * @covers ::load_theme_textdomain
     308         */
     309        public function test_pre_load_textdomain_filter() {
     310                $override_load_textdomain_callback = new MockAction();
     311                add_filter( 'override_load_textdomain', array( $override_load_textdomain_callback, 'action' ) );
     312
     313                add_filter( 'pre_load_textdomain', '__return_true' );
     314                load_plugin_textdomain( 'wp-tests-domain' );
     315                remove_filter( 'pre_load_textdomain', '__return_true' );
     316
     317                $this->assertSame( 0, $override_load_textdomain_callback->get_call_count(), 'Expected override_load_textdomain not to be called.' );
     318        }
    303319}
Note: See TracChangeset for help on using the changeset viewer.