Make WordPress Core


Ignore:
Timestamp:
10/02/2024 01:42:11 PM (2 months ago)
Author:
swissspidy
Message:

I18N: Do not load translations directly in load_*_textdomain.

In [59127], _doing_it_wrong warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.

Because many plugins and themes still manually call load_plugin_textdomain(), load_theme_textdomain() or load_muplugin_textdomain(), even though they don't have to anymore, that caused a lot of warnings.

With this new approach, these functions merely register the translations path in the existing WP_Textdomain_Registry and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.

This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.

Props swissspidy, sergeybiryukov, mukesh27.
See #44937.

File:
1 edited

Legend:

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

    r57925 r59157  
    66 */
    77class Tests_L10n_LoadTextdomain extends WP_UnitTestCase {
    8     protected $locale;
    98    protected static $user_id;
    109
     
    2120        parent::set_up();
    2221
    23         $this->locale = '';
    24 
    25         add_filter( 'plugin_locale', array( $this, 'store_locale' ) );
    26         add_filter( 'theme_locale', array( $this, 'store_locale' ) );
    27 
    2822        /** @var WP_Textdomain_Registry $wp_textdomain_registry */
    2923        global $wp_textdomain_registry;
     
    4034
    4135        parent::tear_down();
    42     }
    43 
    44     public function store_locale( $locale ) {
    45         $this->locale = $locale;
    46 
    47         return $locale;
    4836    }
    4937
     
    234222
    235223    /**
    236      * @covers ::load_muplugin_textdomain
    237      */
    238     public function test_load_muplugin_textdomain_site_locale() {
    239         load_muplugin_textdomain( 'wp-tests-domain' );
    240 
    241         $this->assertSame( get_locale(), $this->locale );
    242     }
    243 
    244     /**
    245      * @ticket 38485
    246      *
    247      * @covers ::load_muplugin_textdomain
    248      */
    249     public function test_load_muplugin_textdomain_user_locale() {
    250         set_current_screen( 'dashboard' );
    251         wp_set_current_user( self::$user_id );
    252 
    253         load_muplugin_textdomain( 'wp-tests-domain' );
    254 
    255         $this->assertSame( get_user_locale(), $this->locale );
    256     }
    257 
    258     /**
    259      * @covers ::load_plugin_textdomain
    260      */
    261     public function test_load_plugin_textdomain_site_locale() {
    262         load_plugin_textdomain( 'wp-tests-domain' );
    263 
    264         $this->assertSame( get_locale(), $this->locale );
    265     }
    266 
    267     /**
    268      * @ticket 38485
    269      *
    270      * @covers ::load_plugin_textdomain
    271      */
    272     public function test_load_plugin_textdomain_user_locale() {
    273         set_current_screen( 'dashboard' );
    274         wp_set_current_user( self::$user_id );
    275 
    276         load_plugin_textdomain( 'wp-tests-domain' );
    277 
    278         $this->assertSame( get_user_locale(), $this->locale );
    279     }
    280 
    281     /**
    282      * @covers ::load_theme_textdomain
    283      */
    284     public function test_load_theme_textdomain_site_locale() {
    285         load_theme_textdomain( 'wp-tests-domain' );
    286 
    287         $this->assertSame( get_locale(), $this->locale );
    288     }
    289 
    290     /**
    291      * @ticket 38485
    292      *
    293      * @covers ::load_theme_textdomain
    294      */
    295     public function test_load_theme_textdomain_user_locale() {
    296         set_current_screen( 'dashboard' );
    297         wp_set_current_user( self::$user_id );
    298 
    299         load_theme_textdomain( 'wp-tests-domain' );
    300 
    301         $this->assertSame( get_user_locale(), $this->locale );
    302     }
    303 
    304     /**
    305224     * @ticket 58035
    306225     *
Note: See TracChangeset for help on using the changeset viewer.