Make WordPress Core


Ignore:
Timestamp:
01/30/2024 01:59:46 PM (3 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

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

    r57344 r57386  
    204204                }
    205205
    206                 return $this->entries[ $text ] ?? false;
     206                if ( isset( $this->entries[ $text ] ) ) {
     207                        return $this->entries[ $text ];
     208                }
     209
     210                /*
     211                 * Handle cases where a pluralized string is only used as a singular one.
     212                 * For example, when both __( 'Product' ) and _n( 'Product', 'Products' )
     213                 * are used, the entry key will have the format "ProductNULProducts".
     214                 * Fall back to looking up just "Product" to support this edge case.
     215                 */
     216                foreach( $this->entries as $key => $value ) {
     217                        if ( str_starts_with( $key, $text . "\0" ) ) {
     218                                $parts = explode( "\0", $value );
     219                                return $parts[0];
     220                        }
     221                }
     222
     223                return false;
    207224        }
    208225
Note: See TracChangeset for help on using the changeset viewer.