Make WordPress Core


Ignore:
Timestamp:
05/29/2023 08:34:43 AM (14 months ago)
Author:
swissspidy
Message:

I18N: Improve _load_textdomain_just_in_time() logic when there are no translation files.

Fixes a performance issue where the JIT logic is invoked for every translation call if the there are no translations in the current locale. With this change, the information is cached by adding Noop_Translations instances to the global $l10n array. This way, get_translations_for_domain() returns earlier, thus avoiding subsequent _load_textdomain_just_in_time() calls.

Props swissspidy, johnbillion, ocean90.
Fixes #58321.

File:
1 edited

Legend:

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

    r54669 r55865  
    123123
    124124    /**
     125     * @ticket 58321
     126     *
     127     * @covers ::get_translations_for_domain
     128     */
     129    public function test_get_translations_for_domain_get_locale_is_called_only_once() {
     130        $filter_locale = new MockAction();
     131        add_filter( 'locale', array( $filter_locale, 'filter' ) );
     132
     133        get_translations_for_domain( 'internationalized-plugin' );
     134        get_translations_for_domain( 'internationalized-plugin' );
     135        get_translations_for_domain( 'internationalized-plugin' );
     136        $translations = get_translations_for_domain( 'internationalized-plugin' );
     137
     138        remove_filter( 'locale', array( $filter_locale, 'filter' ) );
     139
     140        $this->assertSame( 1, $filter_locale->get_call_count() );
     141        $this->assertInstanceOf( 'NOOP_Translations', $translations );
     142        $this->assertFalse( is_textdomain_loaded( 'internationalized-plugin' ) );
     143    }
     144
     145    /**
    125146     * @ticket 37113
    126147     *
Note: See TracChangeset for help on using the changeset viewer.