From 3af51954e09d8b832b2b80e465ad825c93781b32 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 11:45:16 +0000 Subject: [PATCH] I18N: Improve singular lookup of pluralized strings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- wp-includes/l10n/class-wp-translation-file-mo.php | 10 +++++++++- wp-includes/l10n/class-wp-translation-file-php.php | 8 ++------ wp-includes/l10n/class-wp-translations.php | 11 +++-------- wp-includes/version.php | 2 +- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/wp-includes/l10n/class-wp-translation-file-mo.php b/wp-includes/l10n/class-wp-translation-file-mo.php index 225b48a836..bf39cc70ec 100644 --- a/wp-includes/l10n/class-wp-translation-file-mo.php +++ b/wp-includes/l10n/class-wp-translation-file-mo.php @@ -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; } } diff --git a/wp-includes/l10n/class-wp-translation-file-php.php b/wp-includes/l10n/class-wp-translation-file-php.php index 9f5b5abd98..f93dd0163f 100644 --- a/wp-includes/l10n/class-wp-translation-file-php.php +++ b/wp-includes/l10n/class-wp-translation-file-php.php @@ -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'] ); } diff --git a/wp-includes/l10n/class-wp-translations.php b/wp-includes/l10n/class-wp-translations.php index 5992c855b6..e177e1d8c5 100644 --- a/wp-includes/l10n/class-wp-translations.php +++ b/wp-includes/l10n/class-wp-translations.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 1eb408e027..2753e3bee4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.