Make WordPress Core

Changeset 39127


Ignore:
Timestamp:
11/03/2016 08:07:08 PM (8 years ago)
Author:
swissspidy
Message:

I18N: Use the user's locale when loading text domains in the admin.

Leverages get_user_locale() in load_*_textdomain() and _load_textdomain_just_in_time() to always load translations in the user's language when in the admin.

This re-introduces [39069], but now with additional tests and a function_exists( 'wp_get_current_user' ) check in get_user_locale() in case it gets used early.

Props swissspidy, ocean90.
Fixes #38485.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r39070 r39127  
    8989function get_user_locale( $user_id = 0 ) {
    9090    $user = false;
    91     if ( 0 === $user_id ) {
     91    if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
    9292        $user = wp_get_current_user();
    9393    } elseif ( $user_id instanceof WP_User ) {
     
    711711     * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    712712     */
    713     $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
     713    $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
    714714
    715715    $mofile = $domain . '-' . $locale . '.mo';
     
    745745function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
    746746    /** This filter is documented in wp-includes/l10n.php */
    747     $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
     747    $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
    748748
    749749    $mofile = $domain . '-' . $locale . '.mo';
     
    784784     * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    785785     */
    786     $locale = apply_filters( 'theme_locale', get_locale(), $domain );
     786    $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
    787787
    788788    $mofile = $domain . '-' . $locale . '.mo';
     
    866866    }
    867867
    868     $locale = get_locale();
     868    $locale = is_admin() ? get_user_locale() : get_locale();
    869869    $mofile = "{$domain}-{$locale}.mo";
    870870
  • trunk/tests/phpunit/tests/l10n/loadTextdomain.php

    r39125 r39127  
    77class Tests_L10n_loadTextdomain extends WP_UnitTestCase {
    88    protected $locale;
     9    protected static $user_id;
     10
     11    public static function wpSetUpBeforeClass( $factory ) {
     12        self::$user_id = $factory->user->create( array(
     13            'role'   => 'administrator',
     14            'locale' => 'de_DE',
     15        ) );
     16    }
    917
    1018    public function setUp() {
     
    184192    }
    185193
     194    /**
     195     * @ticket 38485
     196     */
     197    public function test_load_muplugin_textdomain_user_locale() {
     198        set_current_screen( 'dashboard' );
     199        wp_set_current_user( self::$user_id );
     200
     201        load_muplugin_textdomain( 'wp-tests-domain' );
     202
     203        set_current_screen( 'front' );
     204
     205        $this->assertSame( get_user_locale(), $this->locale );
     206    }
     207
    186208    public function test_load_plugin_textdomain_site_locale() {
    187209        load_plugin_textdomain( 'wp-tests-domain' );
     
    190212    }
    191213
     214    /**
     215     * @ticket 38485
     216     */
     217    public function test_load_plugin_textdomain_user_locale() {
     218        set_current_screen( 'dashboard' );
     219        wp_set_current_user( self::$user_id );
     220
     221        load_plugin_textdomain( 'wp-tests-domain' );
     222
     223        set_current_screen( 'front' );
     224
     225        $this->assertSame( get_user_locale(), $this->locale );
     226    }
     227
    192228    public function test_load_theme_textdomain_site_locale() {
    193229        load_theme_textdomain( 'wp-tests-domain' );
     
    195231        $this->assertSame( get_locale(), $this->locale );
    196232    }
     233
     234    /**
     235     * @ticket 38485
     236     */
     237    public function test_load_theme_textdomain_user_locale() {
     238        set_current_screen( 'dashboard' );
     239        wp_set_current_user( self::$user_id );
     240
     241        load_theme_textdomain( 'wp-tests-domain' );
     242
     243        set_current_screen( 'front' );
     244
     245        $this->assertSame( get_user_locale(), $this->locale );
     246    }
    197247}
  • trunk/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    r38961 r39127  
    66 */
    77class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase {
    8 
    9     private $orig_theme_dir;
    10     private $theme_root;
     8    protected $orig_theme_dir;
     9    protected $theme_root;
     10    protected static $user_id;
     11
     12    public static function wpSetUpBeforeClass( $factory ) {
     13        self::$user_id = $factory->user->create( array(
     14            'role'   => 'administrator',
     15            'locale' => 'de_DE',
     16        ) );
     17    }
    1118
    1219    public function setUp() {
     
    172179        $this->assertSame( 'Das ist ein Dummy Theme', $expected );
    173180    }
     181
     182    /**
     183     * @ticket 38485
     184     */
     185    public function test_plugin_translation_with_user_locale() {
     186        require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     187
     188        set_current_screen( 'dashboard' );
     189        wp_set_current_user( self::$user_id );
     190
     191        $expected = i18n_plugin_test();
     192
     193        set_current_screen( 'front' );
     194
     195        $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
     196    }
     197
     198    /**
     199     * @ticket 38485
     200     */
     201    public function test_theme_translation_with_user_locale() {
     202        switch_theme( 'internationalized-theme' );
     203        set_current_screen( 'dashboard' );
     204        wp_set_current_user( self::$user_id );
     205
     206        require_once get_stylesheet_directory() . '/functions.php';
     207
     208        $expected = i18n_theme_test();
     209
     210        set_current_screen( 'front' );
     211        switch_theme( WP_DEFAULT_THEME );
     212
     213        $this->assertSame( 'Das ist ein Dummy Theme', $expected );
     214    }
    174215}
Note: See TracChangeset for help on using the changeset viewer.