Make WordPress Core


Ignore:
Timestamp:
11/21/2016 04:06:38 PM (8 years ago)
Author:
ocean90
Message:

I18N: Add an additional caching layer for _load_textdomain_just_in_time().

Previously, if no translation files exist for a text domain, _load_textdomain_just_in_time() went through the entire process each time it was called. This results in an increased call to get_locale() and its locale filter.
This change splits the logic into _get_path_to_translation() and _get_path_to_translation_from_lang_dir(). The former, which is used by _load_textdomain_just_in_time(), caches the result of the latter. It also removes some non-working code from WP_Locale_Switcher::load_translations().

Props jrf, swissspidy, sharkomatic, ocean90.
Fixes #37997.

File:
1 edited

Legend:

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

    r39127 r39330  
    99    protected $theme_root;
    1010    protected static $user_id;
     11    private $locale_count;
    1112
    1213    public static function wpSetUpBeforeClass( $factory ) {
     
    2223        $this->theme_root = DIR_TESTDATA . '/themedir1';
    2324        $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
     25        $this->locale_count = 0;
    2426
    2527        // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
     
    3234        unset( $GLOBALS['l10n'] );
    3335        unset( $GLOBALS['l10n_unloaded'] );
     36        _get_path_to_translation( null, true );
    3437    }
    3538
     
    4346        unset( $GLOBALS['l10n'] );
    4447        unset( $GLOBALS['l10n_unloaded'] );
     48        _get_path_to_translation( null, true );
    4549
    4650        parent::tearDown();
     
    161165
    162166        $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
     167    }
     168
     169    /**
     170     * @ticket 37997
     171     */
     172    public function test_plugin_translation_after_switching_locale_twice() {
     173        require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     174
     175        switch_to_locale( 'de_DE' );
     176        $expected_de_DE = i18n_plugin_test();
     177
     178        switch_to_locale( 'es_ES' );
     179        $expected_es_ES = i18n_plugin_test();
     180
     181        restore_current_locale();
     182
     183        $this->assertSame( 'Das ist ein Dummy Plugin', $expected_de_DE );
     184        $this->assertSame( 'This is a dummy plugin', $expected_es_ES );
    163185    }
    164186
     
    213235        $this->assertSame( 'Das ist ein Dummy Theme', $expected );
    214236    }
     237
     238    /**
     239     * @ticket 37997
     240     */
     241    public function test_get_locale_is_called_only_once_per_textdomain() {
     242        $textdomain = 'foo-bar-baz';
     243
     244        add_filter( 'locale', array( $this, '_filter_locale_count' ) );
     245
     246        __( 'Foo', $textdomain );
     247        __( 'Bar', $textdomain );
     248        __( 'Baz', $textdomain );
     249        __( 'Foo Bar', $textdomain );
     250        __( 'Foo Bar Baz', $textdomain );
     251
     252        remove_filter( 'locale', array( $this, '_filter_locale_count' ) );
     253
     254        $this->assertFalse( is_textdomain_loaded( $textdomain ) );
     255        $this->assertSame( 1, $this->locale_count );
     256    }
     257
     258    public function _filter_locale_count( $locale ) {
     259        ++$this->locale_count;
     260
     261        return $locale;
     262    }
    215263}
Note: See TracChangeset for help on using the changeset viewer.