I18N: Improve singular lookup of pluralized strings.

Ensures that string lookup in MO files only uses the singular string.

This matches expected behavior with gettext files and 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. Maintains backward compatibility and feature parity with the pomo library and the PHP translation file format.

Replaces [57386], which was reverted in [57505], with a more accurate and performant solution.

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


git-svn-id: http://core.svn.wordpress.org/trunk@57014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-02-01 11:45:16 +00:00
parent 7a7f7ec0d3
commit 3af51954e0
4 changed files with 15 additions and 16 deletions

View File

@ -161,7 +161,15 @@ class WP_Translation_File_MO extends WP_Translation_File {
$this->headers[ strtolower( $name ) ] = $value;
}
} else {
$this->entries[ (string) $original ] = $translation;
/*
* In MO files, the key normally contains both singular and plural versions.
* However, this just adds the singular string for lookup,
* which caters for cases where both __( 'Product' ) and _n( 'Product', 'Products' )
* are used and the translation is expected to be the same for both.
*/
$parts = explode( "\0", (string) $original );
$this->entries[ $parts[0] ] = $translation;
}
}

View File

@ -28,12 +28,8 @@ class WP_Translation_File_PHP extends WP_Translation_File {
}
if ( isset( $result['messages'] ) && is_array( $result['messages'] ) ) {
foreach ( $result['messages'] as $singular => $translations ) {
if ( is_array( $translations ) ) {
$this->entries[ $singular ] = implode( "\0", $translations );
} elseif ( is_string( $translations ) ) {
$this->entries[ $singular ] = $translations;
}
foreach ( $result['messages'] as $original => $translation ) {
$this->entries[ (string) $original ] = $translation;
}
unset( $result['messages'] );
}

View File

@ -95,15 +95,10 @@ class WP_Translations {
$entry->context = $parts[0];
}
// Look for plural original.
$parts = explode( "\0", $original );
$entry->singular = $parts[0];
if ( isset( $parts[1] ) ) {
$entry->is_plural = true;
$entry->plural = $parts[1];
}
$entry->singular = $original;
$entry->translations = explode( "\0", $translations );
$entry->is_plural = count( $entry->translations ) > 1;
return $entry;
}

View File

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