Make WordPress Core


Ignore:
Timestamp:
02/01/2024 11:43:21 AM (2 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n/class-wp-translation-file-mo.php

    r57337 r57513  
    162162                }
    163163            } else {
    164                 $this->entries[ (string) $original ] = $translation;
     164                /*
     165                 * In MO files, the key normally contains both singular and plural versions.
     166                 * However, this just adds the singular string for lookup,
     167                 * which caters for cases where both __( 'Product' ) and _n( 'Product', 'Products' )
     168                 * are used and the translation is expected to be the same for both.
     169                 */
     170                $parts = explode( "\0", (string) $original );
     171
     172                $this->entries[ $parts[0] ] = $translation;
    165173            }
    166174        }
Note: See TracChangeset for help on using the changeset viewer.