WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 03:35:58 PM (5 years ago)
Author:
ocean90
Message:

I18N: Introduce a locale-switching function.

With the introduction of user-specific languages in [38705] it's necessary to be able to switch translations on the fly. For example emails should be sent in the language of the recipient and not the one of the current user.

This introduces a new WP_Locale_Switcher class which is used for switching locales and translations. It holds the stack of locales whenever switch_to_locale( $locale ) is called. With restore_previous_locale() you can restore the previous locale. restore_current_locale() empties the stack and sets the locale back to the initial value.

switch_to_locale() is added to most of core's email functions, either with the value of get_locale() (site language) or get_user_locale() (user language with fallback to site language).

Props yoavf, tfrommen, swissspidy, pbearne, ocean90.
See #29783.
Fixes #26511.

File:
1 edited

Legend:

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

    r37855 r38961  
    1010    private $theme_root;
    1111
    12     function setUp() {
     12    public function setUp() {
    1313        parent::setUp();
    1414
     
    2323        wp_clean_themes_cache();
    2424        unset( $GLOBALS['wp_themes'] );
    25 
     25        unset( $GLOBALS['l10n'] );
    2626        unset( $GLOBALS['l10n_unloaded'] );
    2727    }
    2828
    29     function tearDown() {
     29    public function tearDown() {
    3030        $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
    3131        remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
     
    3434        wp_clean_themes_cache();
    3535        unset( $GLOBALS['wp_themes'] );
     36        unset( $GLOBALS['l10n'] );
     37        unset( $GLOBALS['l10n_unloaded'] );
    3638
    3739        parent::tearDown();
     
    6163        $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-plugin' );
    6264
    63         unload_textdomain( 'internationalized-plugin' );
    6465        remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
    6566
     
    8384        $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-theme' );
    8485
    85         unload_textdomain( 'internationalized-theme' );
    8686        remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
    8787
     
    142142        $this->assertTrue( $is_textdomain_loaded_final );
    143143    }
     144
     145    /**
     146     * @ticket 26511
     147     */
     148    public function test_plugin_translation_after_switching_locale() {
     149        require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     150
     151        switch_to_locale( 'de_DE' );
     152        $expected = i18n_plugin_test();
     153        restore_previous_locale();
     154
     155        $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
     156    }
     157
     158    /**
     159     * @ticket 26511
     160     */
     161    public function test_theme_translation_after_switching_locale() {
     162        switch_theme( 'internationalized-theme' );
     163
     164        require_once get_stylesheet_directory() . '/functions.php';
     165
     166        switch_to_locale( 'de_DE' );
     167        $expected = i18n_theme_test();
     168        restore_previous_locale();
     169
     170        switch_theme( WP_DEFAULT_THEME );
     171
     172        $this->assertSame( 'Das ist ein Dummy Theme', $expected );
     173    }
    144174}
Note: See TracChangeset for help on using the changeset viewer.