diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index ba856491af..0ad4e01ba7 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -88,7 +88,7 @@ function get_locale() { */ function get_user_locale( $user_id = 0 ) { $user = false; - if ( 0 === $user_id ) { + if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) { $user = wp_get_current_user(); } elseif ( $user_id instanceof WP_User ) { $user = $user_id; @@ -710,7 +710,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path * @param string $locale The plugin's current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); + $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -744,7 +744,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path */ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { /** This filter is documented in wp-includes/l10n.php */ - $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); + $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -783,7 +783,7 @@ function load_theme_textdomain( $domain, $path = false ) { * @param string $locale The theme's current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - $locale = apply_filters( 'theme_locale', get_locale(), $domain ); + $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; @@ -865,7 +865,7 @@ function _load_textdomain_just_in_time( $domain ) { } } - $locale = get_locale(); + $locale = is_admin() ? get_user_locale() : get_locale(); $mofile = "{$domain}-{$locale}.mo"; if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) { diff --git a/tests/phpunit/tests/l10n/loadTextdomain.php b/tests/phpunit/tests/l10n/loadTextdomain.php index ec26281491..a44f09a7fd 100644 --- a/tests/phpunit/tests/l10n/loadTextdomain.php +++ b/tests/phpunit/tests/l10n/loadTextdomain.php @@ -6,6 +6,14 @@ */ class Tests_L10n_loadTextdomain extends WP_UnitTestCase { protected $locale; + protected static $user_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$user_id = $factory->user->create( array( + 'role' => 'administrator', + 'locale' => 'de_DE', + ) ); + } public function setUp() { parent::setUp(); @@ -183,15 +191,57 @@ class Tests_L10n_loadTextdomain extends WP_UnitTestCase { $this->assertSame( get_locale(), $this->locale ); } + /** + * @ticket 38485 + */ + public function test_load_muplugin_textdomain_user_locale() { + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + load_muplugin_textdomain( 'wp-tests-domain' ); + + set_current_screen( 'front' ); + + $this->assertSame( get_user_locale(), $this->locale ); + } + public function test_load_plugin_textdomain_site_locale() { load_plugin_textdomain( 'wp-tests-domain' ); $this->assertSame( get_locale(), $this->locale ); } + /** + * @ticket 38485 + */ + public function test_load_plugin_textdomain_user_locale() { + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + load_plugin_textdomain( 'wp-tests-domain' ); + + set_current_screen( 'front' ); + + $this->assertSame( get_user_locale(), $this->locale ); + } + public function test_load_theme_textdomain_site_locale() { load_theme_textdomain( 'wp-tests-domain' ); $this->assertSame( get_locale(), $this->locale ); } + + /** + * @ticket 38485 + */ + public function test_load_theme_textdomain_user_locale() { + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + load_theme_textdomain( 'wp-tests-domain' ); + + set_current_screen( 'front' ); + + $this->assertSame( get_user_locale(), $this->locale ); + } } diff --git a/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php b/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php index 57aca144d3..f0d7b5528f 100644 --- a/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php +++ b/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php @@ -5,9 +5,16 @@ * @group i18n */ class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { + protected $orig_theme_dir; + protected $theme_root; + protected static $user_id; - private $orig_theme_dir; - private $theme_root; + public static function wpSetUpBeforeClass( $factory ) { + self::$user_id = $factory->user->create( array( + 'role' => 'administrator', + 'locale' => 'de_DE', + ) ); + } public function setUp() { parent::setUp(); @@ -171,4 +178,38 @@ class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { $this->assertSame( 'Das ist ein Dummy Theme', $expected ); } + + /** + * @ticket 38485 + */ + public function test_plugin_translation_with_user_locale() { + require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php'; + + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + $expected = i18n_plugin_test(); + + set_current_screen( 'front' ); + + $this->assertSame( 'Das ist ein Dummy Plugin', $expected ); + } + + /** + * @ticket 38485 + */ + public function test_theme_translation_with_user_locale() { + switch_theme( 'internationalized-theme' ); + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + require_once get_stylesheet_directory() . '/functions.php'; + + $expected = i18n_theme_test(); + + set_current_screen( 'front' ); + switch_theme( WP_DEFAULT_THEME ); + + $this->assertSame( 'Das ist ein Dummy Theme', $expected ); + } }