mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
I18N: Change how WP_Textdomain_Registry caches translation information.
`WP_Textdomain_Registry` was introduced in [53874] and later adjusted in [54682] to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and `load_*_textdomain()` functions. This change improves how the class stores information about all existing MO files on the site, addressing an issue where translations are not loaded after calling `switch_to_locale()`. Props johnbillion, ocean90, SergeyBiryukov. Fixes #57116. git-svn-id: https://develop.svn.wordpress.org/trunk@55010 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -24,20 +24,26 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
|
||||
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
global $wp_textdomain_registry, $wp_locale_switcher;
|
||||
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
|
||||
remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
|
||||
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
global $wp_textdomain_registry, $wp_locale_switcher;
|
||||
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
|
||||
remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
@@ -336,8 +342,8 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
wp_set_current_user( $user_id );
|
||||
set_current_screen( 'dashboard' );
|
||||
|
||||
$locale_switcher = clone $wp_locale_switcher;
|
||||
|
||||
// Reset $wp_locale_switcher so it thinks es_ES is the original locale.
|
||||
remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
|
||||
@@ -357,8 +363,6 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
$language_header_after_restore = $l10n['default']->headers['Language']; // de_DE
|
||||
|
||||
$wp_locale_switcher = $locale_switcher;
|
||||
|
||||
$this->assertFalse( $locale_switched_user_locale );
|
||||
$this->assertTrue( $locale_switched_site_locale );
|
||||
$this->assertSame( $site_locale, $site_locale_after_switch );
|
||||
@@ -388,8 +392,8 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
wp_set_current_user( $user_id );
|
||||
set_current_screen( 'dashboard' );
|
||||
|
||||
$locale_switcher = clone $wp_locale_switcher;
|
||||
|
||||
// Reset $wp_locale_switcher so it thinks es_ES is the original locale.
|
||||
remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
|
||||
@@ -409,8 +413,6 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
$language_header_after_restore = $l10n['default']->headers['Language']; // de_DE
|
||||
|
||||
$wp_locale_switcher = $locale_switcher;
|
||||
|
||||
remove_filter( 'locale', array( $this, 'filter_locale' ) );
|
||||
|
||||
$this->assertFalse( $locale_switched_user_locale );
|
||||
@@ -426,8 +428,6 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
* @covers ::load_default_textdomain
|
||||
*/
|
||||
public function test_multiple_switches_to_site_locale_and_user_locale() {
|
||||
global $wp_locale_switcher;
|
||||
|
||||
$site_locale = get_locale();
|
||||
|
||||
$user_id = self::factory()->user->create(
|
||||
@@ -440,11 +440,6 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
wp_set_current_user( $user_id );
|
||||
set_current_screen( 'dashboard' );
|
||||
|
||||
$locale_switcher = clone $wp_locale_switcher;
|
||||
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
|
||||
$user_locale = get_user_locale();
|
||||
|
||||
load_default_textdomain( $user_locale );
|
||||
@@ -458,8 +453,6 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
restore_current_locale();
|
||||
|
||||
$wp_locale_switcher = $locale_switcher;
|
||||
|
||||
$this->assertSame( 'en_US', get_locale() );
|
||||
$this->assertSame( 'This is a dummy plugin', $actual );
|
||||
}
|
||||
@@ -469,12 +462,7 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_switch_reloads_plugin_translations_outside_wp_lang_dir() {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_locale_switcher, $wp_textdomain_registry;
|
||||
|
||||
$locale_switcher = clone $wp_locale_switcher;
|
||||
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
|
||||
|
||||
@@ -494,25 +482,58 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
restore_current_locale();
|
||||
|
||||
$wp_locale_switcher = $locale_switcher;
|
||||
|
||||
$this->assertSame( 'This is a dummy plugin', $actual );
|
||||
$this->assertSame( WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages/', $registry_value );
|
||||
$this->assertSame( 'Das ist ein Dummy Plugin', $actual_de_de );
|
||||
$this->assertSame( 'Este es un plugin dummy', $actual_es_es );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57116
|
||||
*/
|
||||
public function test_switch_reloads_plugin_translations() {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$has_translations_1 = $wp_textdomain_registry->has( 'internationalized-plugin' );
|
||||
|
||||
require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
|
||||
|
||||
$actual = i18n_plugin_test();
|
||||
|
||||
switch_to_locale( 'es_ES' );
|
||||
|
||||
$lang_path_es_es = $wp_textdomain_registry->get( 'internationalized-plugin', determine_locale() );
|
||||
|
||||
switch_to_locale( 'de_DE' );
|
||||
|
||||
$actual_de_de = i18n_plugin_test();
|
||||
|
||||
$has_translations_3 = $wp_textdomain_registry->has( 'internationalized-plugin' );
|
||||
|
||||
restore_previous_locale();
|
||||
|
||||
$actual_es_es = i18n_plugin_test();
|
||||
|
||||
restore_current_locale();
|
||||
|
||||
$lang_path_en_us = $wp_textdomain_registry->get( 'internationalized-plugin', determine_locale() );
|
||||
|
||||
$this->assertSame( 'This is a dummy plugin', $actual );
|
||||
$this->assertSame( 'Das ist ein Dummy Plugin', $actual_de_de );
|
||||
$this->assertSame( 'Este es un plugin dummy', $actual_es_es );
|
||||
$this->assertTrue( $has_translations_1 );
|
||||
$this->assertTrue( $has_translations_3 );
|
||||
$this->assertSame( WP_LANG_DIR . '/plugins/', $lang_path_es_es );
|
||||
$this->assertFalse( $lang_path_en_us );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 39210
|
||||
*/
|
||||
public function test_switch_reloads_theme_translations_outside_wp_lang_dir() {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_locale_switcher, $wp_textdomain_registry;
|
||||
|
||||
$locale_switcher = clone $wp_locale_switcher;
|
||||
|
||||
$wp_locale_switcher = new WP_Locale_Switcher();
|
||||
$wp_locale_switcher->init();
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
switch_theme( 'custom-internationalized-theme' );
|
||||
|
||||
@@ -534,14 +555,34 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
restore_current_locale();
|
||||
|
||||
$wp_locale_switcher = $locale_switcher;
|
||||
|
||||
$this->assertSame( get_template_directory() . '/languages/', $registry_value );
|
||||
$this->assertSame( 'This is a dummy theme', $actual );
|
||||
$this->assertSame( 'Das ist ein Dummy Theme', $actual_de_de );
|
||||
$this->assertSame( 'Este es un tema dummy', $actual_es_es );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57116
|
||||
*/
|
||||
public function test_switch_to_locale_should_work() {
|
||||
global $wp_textdomain_registry;
|
||||
require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
|
||||
|
||||
$has_translations = $wp_textdomain_registry->has( 'internationalized-plugin' );
|
||||
$path = $wp_textdomain_registry->get( 'internationalized-plugin', 'es_ES' );
|
||||
|
||||
$actual = i18n_plugin_test();
|
||||
|
||||
switch_to_locale( 'es_ES' );
|
||||
|
||||
$actual_es_es = i18n_plugin_test();
|
||||
|
||||
$this->assertTrue( $has_translations );
|
||||
$this->assertNotEmpty( $path );
|
||||
$this->assertSame( 'This is a dummy plugin', $actual );
|
||||
$this->assertSame( 'Este es un plugin dummy', $actual_es_es );
|
||||
}
|
||||
|
||||
public function filter_locale() {
|
||||
return 'es_ES';
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase {
|
||||
$reflection_property = $reflection->getProperty( 'cached_mo_files' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
$this->assertNull(
|
||||
$this->assertEmpty(
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Cache not empty by default'
|
||||
);
|
||||
@@ -78,28 +78,6 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get_path_from_lang_dir
|
||||
*/
|
||||
public function test_get_does_not_check_themes_directory_for_plugin() {
|
||||
$reflection = new ReflectionClass( $this->instance );
|
||||
$reflection_property = $reflection->getProperty( 'cached_mo_files' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
$this->instance->get( 'internationalized-plugin', 'de_DE' );
|
||||
|
||||
$this->assertArrayHasKey(
|
||||
WP_LANG_DIR . '/plugins',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Default plugins path missing from cache'
|
||||
);
|
||||
$this->assertArrayNotHasKey(
|
||||
WP_LANG_DIR . '/themes',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Default themes path should not be in cache'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::set
|
||||
* @covers ::get
|
||||
|
||||
Reference in New Issue
Block a user