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.

git-svn-id: https://develop.svn.wordpress.org/trunk@39127 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2016-11-03 20:07:08 +00:00
parent 4757546045
commit 8a08cfc840
3 changed files with 98 additions and 7 deletions

View File

@ -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 ) ) {

View File

@ -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 );
}
}

View File

@ -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 );
}
}