Skip to content

Commit d47a104

Browse files
I18N: Correct strict comparison in WP_Translations::translate_plural().
This ensures that the `$count` value is compared as an integer to account for plugins or themes passing a numeric string instead. Follow-up to [57337]. Props dd32, mukesh27, swissspidy, pmbaldha, SergeyBiryukov. Fixes #63642. git-svn-id: https://develop.svn.wordpress.org/trunk@60484 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d71b1d8 commit d47a104

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/wp-includes/l10n/class-wp-translations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function translate_plural( $singular, $plural, $count = 1, $context = ''
124124
}
125125

126126
// Fall back to the original with English grammar rules.
127-
return ( 1 === $count ? $singular : $plural );
127+
return ( 1 === (int) $count ? $singular : $plural );
128128
}
129129

130130
/**

tests/phpunit/tests/l10n/wpTranslations.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,16 @@ public function test_translate_plural() {
226226
public function test_translate_plural_complex() {
227227
load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/l10n/plural-complex.mo' );
228228

229+
$this->assertSame( '%s razpoložljiva posodobitev', _n( '%s update available', '%s updates available', '1', 'wp-tests-domain' ) ); // 1, 101, 201
229230
$this->assertSame( '%s razpoložljiva posodobitev', _n( '%s update available', '%s updates available', 101, 'wp-tests-domain' ) ); // 1, 101, 201
230231
$this->assertSame( '%s razpoložljivi posodobitvi', _n( '%s update available', '%s updates available', 102, 'wp-tests-domain' ) ); // 2, 102, 202
231232
$this->assertSame( '%s razpoložljive posodobitve', _n( '%s update available', '%s updates available', 103, 'wp-tests-domain' ) ); // 3, 4, 103
232233
$this->assertSame( '%s razpoložljivih posodobitev', _n( '%s update available', '%s updates available', 5, 'wp-tests-domain' ) ); // 0, 5, 6
234+
235+
// Test with strings that are not in the translation files.
236+
$this->assertSame( 'Singular', _n( 'Singular', 'Plural', 1, 'wp-tests-domain' ) );
237+
$this->assertSame( 'Singular', _n( 'Singular', 'Plural', '1', 'wp-tests-domain' ) );
238+
$this->assertSame( 'Plural', _n( 'Singular', 'Plural', 2, 'wp-tests-domain' ) );
233239
}
234240

235241
/**
@@ -239,10 +245,16 @@ public function test_translate_plural_complex() {
239245
public function test_translate_plural_complex_php() {
240246
load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/l10n/plural-complex.php' );
241247

248+
$this->assertSame( '%s razpoložljiva posodobitev', _n( '%s update available', '%s updates available', '1', 'wp-tests-domain' ) ); // 1, 101, 201
242249
$this->assertSame( '%s razpoložljiva posodobitev', _n( '%s update available', '%s updates available', 101, 'wp-tests-domain' ) ); // 1, 101, 201
243250
$this->assertSame( '%s razpoložljivi posodobitvi', _n( '%s update available', '%s updates available', 102, 'wp-tests-domain' ) ); // 2, 102, 202
244251
$this->assertSame( '%s razpoložljive posodobitve', _n( '%s update available', '%s updates available', 103, 'wp-tests-domain' ) ); // 3, 4, 103
245252
$this->assertSame( '%s razpoložljivih posodobitev', _n( '%s update available', '%s updates available', 5, 'wp-tests-domain' ) ); // 0, 5, 6
253+
254+
// Test with strings that are not in the translation files.
255+
$this->assertSame( 'Singular', _n( 'Singular', 'Plural', 1, 'wp-tests-domain' ) );
256+
$this->assertSame( 'Singular', _n( 'Singular', 'Plural', '1', 'wp-tests-domain' ) );
257+
$this->assertSame( 'Plural', _n( 'Singular', 'Plural', 2, 'wp-tests-domain' ) );
246258
}
247259

248260
/**

0 commit comments

Comments
 (0)