Skip to content

Commit bf38bd3

Browse files
committed
I18N: Improve _load_textdomain_just_in_time() logic when there are no translation files.
Fixes a performance issue where the JIT logic is invoked for every translation call if the there are no translations in the current locale. With this change, the information is cached by adding `Noop_Translations` instances to the global `$l10n` array. This way, `get_translations_for_domain()` returns earlier, thus avoiding subsequent `_load_textdomain_just_in_time()` calls. Props swissspidy, johnbillion, ocean90. Fixes #58321. git-svn-id: https://develop.svn.wordpress.org/trunk@55865 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7a89d0a commit bf38bd3

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/wp-includes/class-wp-textdomain-registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function get( $domain, $locale ) {
9797
*/
9898
public function has( $domain ) {
9999
return (
100-
! empty( $this->current[ $domain ] ) ||
100+
isset( $this->current[ $domain ] ) ||
101101
empty( $this->all[ $domain ] ) ||
102102
in_array( $domain, $this->domains_with_translations, true )
103103
);

src/wp-includes/l10n.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,12 @@ function unload_textdomain( $domain, $reloadable = false ) {
834834
do_action( 'unload_textdomain', $domain, $reloadable );
835835

836836
if ( isset( $l10n[ $domain ] ) ) {
837+
if ( $l10n[ $domain ] instanceof NOOP_Translations ) {
838+
unset( $l10n[ $domain ] );
839+
840+
return false;
841+
}
842+
837843
unset( $l10n[ $domain ] );
838844

839845
if ( ! $reloadable ) {
@@ -1307,6 +1313,8 @@ function get_translations_for_domain( $domain ) {
13071313
$noop_translations = new NOOP_Translations();
13081314
}
13091315

1316+
$l10n[ $domain ] = &$noop_translations;
1317+
13101318
return $noop_translations;
13111319
}
13121320

@@ -1322,7 +1330,7 @@ function get_translations_for_domain( $domain ) {
13221330
*/
13231331
function is_textdomain_loaded( $domain ) {
13241332
global $l10n;
1325-
return isset( $l10n[ $domain ] );
1333+
return isset( $l10n[ $domain ] ) && ! $l10n[ $domain ] instanceof NOOP_Translations;
13261334
}
13271335

13281336
/**

tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ public function test_get_translations_for_domain_does_not_return_null_if_overrid
121121
$this->assertInstanceOf( 'NOOP_Translations', $translations );
122122
}
123123

124+
/**
125+
* @ticket 58321
126+
*
127+
* @covers ::get_translations_for_domain
128+
*/
129+
public function test_get_translations_for_domain_get_locale_is_called_only_once() {
130+
$filter_locale = new MockAction();
131+
add_filter( 'locale', array( $filter_locale, 'filter' ) );
132+
133+
get_translations_for_domain( 'internationalized-plugin' );
134+
get_translations_for_domain( 'internationalized-plugin' );
135+
get_translations_for_domain( 'internationalized-plugin' );
136+
$translations = get_translations_for_domain( 'internationalized-plugin' );
137+
138+
remove_filter( 'locale', array( $filter_locale, 'filter' ) );
139+
140+
$this->assertSame( 1, $filter_locale->get_call_count() );
141+
$this->assertInstanceOf( 'NOOP_Translations', $translations );
142+
$this->assertFalse( is_textdomain_loaded( 'internationalized-plugin' ) );
143+
}
144+
124145
/**
125146
* @ticket 37113
126147
*

tests/phpunit/tests/l10n/wpLocaleSwitcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public function test_switch_to_site_locale_if_user_locale_is_set() {
384384
$locale_switched_user_locale = switch_to_locale( $user_locale ); // False.
385385
$locale_switched_site_locale = switch_to_locale( $site_locale ); // True.
386386
$site_locale_after_switch = get_locale();
387-
$language_header_after_switch = isset( $l10n['default'] ); // en_US
387+
$language_header_after_switch = is_textdomain_loaded( 'default' ); // en_US
388388

389389
restore_current_locale();
390390

0 commit comments

Comments
 (0)