I18N: Improve singular lookup of pluralized strings.

Ensures that looking up a singular that is also used as a pluralized string works as expected.
This improves compatibility for cases where for example both `__( 'Product' )` and `_n( 'Product', 'Products’, num )` are used in a project, where both will use the same translation for the singular version.

Although such usage is not really recommended nor documented, it must continue to work in the new i18n library in order to maintain backward compatibility and maintain expected behavior.

See #59656.
Built from https://develop.svn.wordpress.org/trunk@57386


git-svn-id: http://core.svn.wordpress.org/trunk@56892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-01-30 14:01:11 +00:00
parent 27348531f3
commit b7aadef64d
2 changed files with 19 additions and 2 deletions

View File

@ -203,7 +203,24 @@ abstract class WP_Translation_File {
$this->parse_file();
}
return $this->entries[ $text ] ?? false;
if ( isset( $this->entries[ $text ] ) ) {
return $this->entries[ $text ];
}
/*
* Handle cases where a pluralized string is only used as a singular one.
* For example, when both __( 'Product' ) and _n( 'Product', 'Products' )
* are used, the entry key will have the format "ProductNULProducts".
* Fall back to looking up just "Product" to support this edge case.
*/
foreach( $this->entries as $key => $value ) {
if ( str_starts_with( $key, $text . "\0" ) ) {
$parts = explode( "\0", $value );
return $parts[0];
}
}
return false;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57385';
$wp_version = '6.5-alpha-57386';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.