WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/20/2020 04:03:58 PM (9 months ago)
Author:
ocean90
Message:

I18N: Introduce WP_Textdomain_Registry to store text domains and their language directory paths.

Previously, when using switch_to_locale() all current loaded text domains were unloaded and added to the $l10n_unloaded global. This prevented the just-in-time loading for text domains after a switch. The just-in-time loading was also only possible if the translations were stored in WP_LANG_DIR. Both issues have been fixed.

  • Adds WP_Textdomain_Registry to keep track of the language directory paths for all plugins and themes.
  • Updates all load_*_textdomain() functions to store the path in WP_Textdomain_Registry.
  • Adds $reloadable parameter to unload_textdomain() to define whether a text domain can be loaded just-in-time again. This is used by WP_Locale_Switcher::load_translations().
  • Extends _load_textdomain_just_in_time() to also support text domains of plugins and themes with custom language directories.
  • Fixes the incorrect test_plugin_translation_after_switching_locale_twice() test which should have catch this issue earlier.
  • Adds a new test plugin/theme to test the loading of translations with a custom language directory.
  • Deprecates the now unused and private _get_path_to_translation() and _get_path_to_translation_from_lang_dir() functions.

Props yoavf, swissspidy, dd32, ocean90.
See #26511.
Fixes #39210.

File:
1 edited

Legend:

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

    r46586 r49236  
    2525        add_filter( 'plugin_locale', array( $this, 'store_locale' ) );
    2626        add_filter( 'theme_locale', array( $this, 'store_locale' ) );
     27
     28        /** @var WP_Textdomain_Registry $wp_textdomain_registry */
     29        global $wp_textdomain_registry;
     30
     31        $wp_textdomain_registry->reset();
    2732    }
    2833
     
    3136        remove_filter( 'theme_locale', array( $this, 'store_locale' ) );
    3237
     38        /** @var WP_Textdomain_Registry $wp_textdomain_registry */
     39        global $wp_textdomain_registry;
     40
     41        $wp_textdomain_registry->reset();
     42
    3343        parent::tearDown();
    3444    }
     
    117127     * @ticket 21319
    118128     */
    119     function test_is_textdomain_is_not_loaded_after_gettext_call_with_no_translations() {
     129    public function test_is_textdomain_is_not_loaded_after_gettext_call_with_no_translations() {
    120130        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
    121131        __( 'just some string', 'wp-tests-domain' );
     
    123133    }
    124134
    125     function test_override_load_textdomain_noop() {
     135    public function test_override_load_textdomain_noop() {
    126136        add_filter( 'override_load_textdomain', '__return_true' );
    127137        $load_textdomain = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/non-existent-file' );
     
    132142    }
    133143
    134     function test_override_load_textdomain_non_existent_mofile() {
     144    public function test_override_load_textdomain_non_existent_mofile() {
    135145        add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
    136146        $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/non-existent-file.mo' );
     
    146156    }
    147157
    148     function test_override_load_textdomain_custom_mofile() {
     158    public function test_override_load_textdomain_custom_mofile() {
    149159        add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
    150160        $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' );
     
    166176     * @return bool
    167177     */
    168     function _override_load_textdomain_filter( $override, $domain, $file ) {
     178    public function _override_load_textdomain_filter( $override, $domain, $file ) {
    169179        global $l10n;
    170180
Note: See TracChangeset for help on using the changeset viewer.