Make WordPress Core

Changeset 55865


Ignore:
Timestamp:
05/29/2023 08:34:43 AM (11 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.

Location:
trunk
Files:
4 edited

Legend:

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

    r55349 r55865  
    9898    public function has( $domain ) {
    9999        return (
    100             ! empty( $this->current[ $domain ] ) ||
     100            isset( $this->current[ $domain ] ) ||
    101101            empty( $this->all[ $domain ] ) ||
    102102            in_array( $domain, $this->domains_with_translations, true )
  • trunk/src/wp-includes/l10n.php

    r55862 r55865  
    835835
    836836    if ( isset( $l10n[ $domain ] ) ) {
     837        if ( $l10n[ $domain ] instanceof NOOP_Translations ) {
     838            unset( $l10n[ $domain ] );
     839
     840            return false;
     841        }
     842
    837843        unset( $l10n[ $domain ] );
    838844
     
    13081314    }
    13091315
     1316    $l10n[ $domain ] = &$noop_translations;
     1317
    13101318    return $noop_translations;
    13111319}
     
    13231331function is_textdomain_loaded( $domain ) {
    13241332    global $l10n;
    1325     return isset( $l10n[ $domain ] );
     1333    return isset( $l10n[ $domain ] ) && ! $l10n[ $domain ] instanceof NOOP_Translations;
    13261334}
    13271335
  • 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     *
  • trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php

    r55224 r55865  
    385385        $locale_switched_site_locale  = switch_to_locale( $site_locale ); // True.
    386386        $site_locale_after_switch     = get_locale();
    387         $language_header_after_switch = isset( $l10n['default'] ); // en_US
     387        $language_header_after_switch = is_textdomain_loaded( 'default' ); // en_US
    388388
    389389        restore_current_locale();
Note: See TracChangeset for help on using the changeset viewer.