Make WordPress Core

Changeset 63362


Ignore:
Timestamp:
08/27/2026 12:12:51 PM (3 months ago)
Author:
swissspidy
Message:

I18N: Simplify WP_Textdomain_Registry::has().

The $domains_with_translations cache was populated by scanning every translation file on each lookup, and matched text domains by prefix rather than in full. It was only ever read by has(), in a condition that can only be reached once a custom path has been registered, so checking for that path directly is equivalent, and takes the per-file work out of a loop that runs on every lookup. WP_Textdomain_Registry::$domains_with_translations is now deprecated.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/7735

Props kkmuffme, swissspidy.
Fixes #62348.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-textdomain-registry.php

    r63320 r63362  
    6464         *
    6565         * @since 6.2.0
     66         * @since 7.2.0 This property is no longer used.
    6667         *
    6768         * @var string[]
     69         *
     70         * @deprecated
    6871         */
    6972        protected $domains_with_translations = array();
    … …  
    109112         *
    110113         * This is the case if a path has been set for the current locale,
    111          * or if there is no information stored yet, in which case
    112          * {@see _load_textdomain_just_in_time()} will fetch the information first.
    113          *
    114          * @since 6.1.0
     114         * if there is no information stored yet, in which case
     115         * {@see _load_textdomain_just_in_time()} will fetch the information first,
     116         * or if a custom path has been registered via {@see load_plugin_textdomain()}
     117         * or {@see load_theme_textdomain()}, which is always worth looking at.
     118         *
     119         * @since 6.1.0
     120         * @since 7.2.0 Checks for a registered custom path instead of the
     121         *              `$domains_with_translations` property.
    115122         *
    116123         * @param string $domain Text domain.
    … …  
    121128                        isset( $this->current[ $domain ] ) ||
    122129                        empty( $this->all[ $domain ] ) ||
    123                         in_array( $domain, $this->domains_with_translations, true )
     130                        isset( $this->custom_paths[ $domain ] )
    124131                );
    125132        }
    … …  
    322329
    323330                        foreach ( $files as $file_path ) {
    324                                 if (
    325                                         ! in_array( $domain, $this->domains_with_translations, true ) &&
    326                                         str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )
    327                                 ) {
    328                                         $this->domains_with_translations[] = $domain;
    329                                 }
    330 
    331331                                if ( $file_path === $mo_path || $file_path === $php_path ) {
    332332                                        $found_location = rtrim( $location, '/' ) . '/';
  • trunk/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    r59430 r63362  
    373373                $this->assertTrue( $is_textdomain_loaded_after );
    374374        }
     375
     376        /**
     377         * Text domains that merely share a prefix with a translated one must not be
     378         * treated as translated themselves.
     379         *
     380         * "internationalized-plugin-de_DE.mo" exists, but there is no
     381         * "internationalized-de_DE.mo", so the "internationalized" text domain has no
     382         * translations at all.
     383         *
     384         * @ticket 62348
     385         *
     386         * @covers ::_load_textdomain_just_in_time
     387         * @covers ::is_textdomain_loaded
     388         */
     389        public function test_text_domain_sharing_a_prefix_with_a_translated_one_is_not_translated() {
     390                add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     391
     392                $actual = __( 'This is a dummy plugin', 'internationalized' );
     393
     394                remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     395
     396                $this->assertFalse(
     397                        is_textdomain_loaded( 'internationalized' ),
     398                        'The "internationalized" text domain should not be considered loaded'
     399                );
     400                $this->assertSame(
     401                        'This is a dummy plugin',
     402                        $actual,
     403                        'A text domain sharing a prefix with a translated one should not be translated'
     404                );
     405        }
     406
     407        /**
     408         * Calling load_plugin_textdomain() after translations have already been loaded
     409         * for one locale and missed for another must not stop just-in-time loading.
     410         *
     411         * Since 6.7.0 load_plugin_textdomain() only registers a custom path and leaves
     412         * the loading to _load_textdomain_just_in_time(), which bails out early when
     413         * WP_Textdomain_Registry::has() returns false.
     414         *
     415         * @ticket 62348
     416         *
     417         * @covers ::_load_textdomain_just_in_time
     418         * @covers ::load_plugin_textdomain
     419         */
     420        public function test_plugin_translations_survive_a_late_load_plugin_textdomain_call() {
     421                require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     422
     423                // de_DE translations are found in the WordPress languages directory.
     424                switch_to_locale( 'de_DE' );
     425                $before = i18n_plugin_test();
     426
     427                // A locale without any translations for this plugin is used in between.
     428                switch_to_locale( 'ja_JP' );
     429                $untranslated = i18n_plugin_test();
     430
     431                // Only now does the plugin register its own languages directory.
     432                load_plugin_textdomain( 'internationalized-plugin', false, 'internationalized-plugin/languages' );
     433
     434                restore_previous_locale();
     435                $after = i18n_plugin_test();
     436
     437                restore_current_locale();
     438
     439                $this->assertSame( 'Das ist ein Dummy Plugin', $before, 'de_DE translations should be loaded up front' );
     440                $this->assertSame( 'This is a dummy plugin', $untranslated, 'There should be no ja_JP translations' );
     441                $this->assertSame( 'Das ist ein Dummy Plugin', $after, 'de_DE translations should survive load_plugin_textdomain()' );
     442        }
    375443}
  • trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php

    r59430 r63362  
    2424                wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' );
    2525                wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' );
     26                wp_cache_delete( md5( WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages/' ), 'translation_files' );
    2627
    2728                parent::tear_down();
    … …  
    151152        }
    152153
     154        /**
     155         * The registry answers "yes" for a text domain it has never looked up,
     156         * so that _load_textdomain_just_in_time() gets a chance to resolve it.
     157         *
     158         * @ticket 62348
     159         *
     160         * @covers ::has
     161         */
     162        public function test_has_returns_true_for_unknown_text_domain() {
     163                $this->assertTrue( $this->instance->has( 'unknown-plugin' ) );
     164        }
     165
     166        /**
     167         * A lookup that found nothing still records the (negative) result for the
     168         * current locale, which keeps has() truthy.
     169         *
     170         * @ticket 62348
     171         *
     172         * @covers ::has
     173         * @covers ::get
     174         */
     175        public function test_has_returns_true_after_unsuccessful_lookup() {
     176                $this->assertFalse(
     177                        $this->instance->get( 'unknown-plugin', 'de_DE' ),
     178                        'A text domain without translations should not resolve to a path'
     179                );
     180                $this->assertTrue(
     181                        $this->instance->has( 'unknown-plugin' ),
     182                        'The negative result for the current locale should still be reported as available'
     183                );
     184        }
     185
     186        /**
     187         * Registering a custom path late must not make previously found translations
     188         * unreachable.
     189         *
     190         * load_plugin_textdomain() and load_theme_textdomain() only register a custom
     191         * path and hand the actual loading off to _load_textdomain_just_in_time(),
     192         * which bails early when has() returns false.
     193         *
     194         * @ticket 62348
     195         *
     196         * @covers ::has
     197         * @covers ::set_custom_path
     198         */
     199        public function test_has_after_custom_path_is_registered_following_a_successful_lookup() {
     200                // A first locale resolves to the WordPress languages directory.
     201                $this->assertSame(
     202                        WP_LANG_DIR . '/plugins/',
     203                        $this->instance->get( 'internationalized-plugin', 'de_DE' ),
     204                        'de_DE translations should be found in the WordPress languages directory'
     205                );
     206
     207                // A second locale has no translations at all, so 'current' becomes false.
     208                $this->assertFalse(
     209                        $this->instance->get( 'internationalized-plugin', 'fr_FR' ),
     210                        'There should be no fr_FR translations'
     211                );
     212
     213                // Only now does the plugin call load_plugin_textdomain().
     214                $this->instance->set_custom_path(
     215                        'internationalized-plugin',
     216                        WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages'
     217                );
     218
     219                $this->assertTrue(
     220                        $this->instance->has( 'internationalized-plugin' ),
     221                        'Registering a custom path should not hide the already known translations'
     222                );
     223                $this->assertSame(
     224                        WP_LANG_DIR . '/plugins/',
     225                        $this->instance->get( 'internationalized-plugin', 'de_DE' ),
     226                        'de_DE translations should still be found after registering a custom path'
     227                );
     228        }
     229
     230        /**
     231         * Same as above, except that no locale ever resolved, so there is nothing
     232         * left to remember once the negative results have been discarded.
     233         *
     234         * @ticket 62348
     235         *
     236         * @covers ::has
     237         * @covers ::set_custom_path
     238         */
     239        public function test_has_after_custom_path_is_registered_following_an_unsuccessful_lookup() {
     240                $this->assertFalse(
     241                        $this->instance->get( 'unknown-plugin', 'de_DE' ),
     242                        'There should be no de_DE translations'
     243                );
     244
     245                $this->instance->set_custom_path(
     246                        'unknown-plugin',
     247                        WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages'
     248                );
     249
     250                $this->assertTrue(
     251                        $this->instance->has( 'unknown-plugin' ),
     252                        'The newly registered custom path should be given a chance'
     253                );
     254        }
     255
     256        /**
     257         * Text domains are matched in full, not by prefix.
     258         *
     259         * "internationalized-plugin-de_DE.mo" must not count as a translation for the
     260         * "internationalized" text domain just because the file name starts with it.
     261         *
     262         * @ticket 62348
     263         *
     264         * @covers ::get
     265         * @covers ::has
     266         * @dataProvider data_text_domains_sharing_a_prefix
     267         *
     268         * @param string $domain Text domain that has no translations of its own.
     269         * @param string $locale Locale to look up.
     270         */
     271        public function test_translations_are_not_shared_between_text_domains_with_a_common_prefix( $domain, $locale ) {
     272                $this->assertFalse(
     273                        $this->instance->get( $domain, $locale ),
     274                        'A text domain sharing a prefix with a translated one should not resolve to a path'
     275                );
     276        }
     277
     278        /**
     279         * Data provider.
     280         *
     281         * @return array[]
     282         */
     283        public function data_text_domains_sharing_a_prefix() {
     284                return array(
     285                        // "internationalized-plugin-{de_DE,es_ES}.mo" exist, "internationalized-*" do not.
     286                        'prefix of a domain with .mo files'     => array( 'internationalized', 'de_DE' ),
     287                        'prefix of a domain with .mo files, es' => array( 'internationalized', 'es_ES' ),
     288                        // "internationalized-plugin-2-de_DE.l10n.php" exists, but is not a
     289                        // translation of "internationalized-plugin".
     290                        'domain whose sibling adds a suffix'    => array( 'internationalized-plugin', 'fr_FR' ),
     291                );
     292        }
     293
     294        /**
     295         * Once a custom path is registered, the domain must stay resolvable even when
     296         * an unrelated text domain shares its prefix.
     297         *
     298         * @ticket 62348
     299         *
     300         * @covers ::has
     301         * @covers ::set_custom_path
     302         */
     303        public function test_has_with_custom_path_is_unaffected_by_text_domains_sharing_a_prefix() {
     304                $this->assertFalse(
     305                        $this->instance->get( 'internationalized', 'de_DE' ),
     306                        'The "internationalized" text domain has no translations of its own'
     307                );
     308
     309                $this->instance->set_custom_path(
     310                        'internationalized',
     311                        WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages'
     312                );
     313
     314                $this->assertTrue(
     315                        $this->instance->has( 'internationalized' ),
     316                        'The newly registered custom path should be given a chance'
     317                );
     318        }
     319
    153320        public function data_domains_locales() {
    154321                return array(
Note: See TracChangeset for help on using the changeset viewer.