Skip to content

Commit

Permalink
Improve compatibility with pluralized strings referenced as singulars (
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Jan 30, 2024
1 parent efa850e commit 35b07b8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
27 changes: 26 additions & 1 deletion lib/class-ginger-mo-translation-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,32 @@ public function translate( string $text ) {
$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.
*/

$found_entry = array_filter(
$this->entries,
static function ( $key ) use ( $text ) {
return str_starts_with( $key, $text . "\0" );
},
ARRAY_FILTER_USE_KEY
);

// $found_entry will have the format [ "ProductNULProducts"=> "ProduktNULProdukte" ].
if ( count( $found_entry ) === 1 ) {
$parts = explode( "\0", ( array_values( $found_entry ) )[0] );
return $parts[0];
}

return false;
}

/**
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ parameters:
ignoreErrors:
# WordPress core includes a polyfill for these.
- message: '/^Function str_ends_with not found\./'
- message: '/^Function str_starts_with not found\./'
- message: '/^Function str_contains not found\./'
# We explicitly want to test with improper values.
- message: '/^Parameter #\d \$(text|single|plural|context) of function (__|_n|_x) expects string, null given\.$/'
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,25 @@
require __DIR__ . '/includes/Ginger_MO_TestCase.php';

define( 'GINGER_MO_TEST_DATA', __DIR__ . '/data/', false );

if ( ! function_exists( 'str_starts_with' ) ) {
/**
* Polyfill for `str_starts_with()` function added in PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack begins with needle.
*
* @since 5.9.0
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` starts with `$needle`, otherwise false.
*/
function str_starts_with( $haystack, $needle ) {
if ( '' === $needle ) {
return true;
}

return 0 === strpos( $haystack, $needle );
}
}
Binary file modified tests/phpunit/unit/data/example-simple.mo
Binary file not shown.
1 change: 1 addition & 0 deletions tests/phpunit/unit/data/example-simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
'contextoriginal with context' => ['translation with context'],
'plural0' . "\0" . 'plural1' => ['translation0', 'translation1'],
'contextplural0 with context' . "\0" . 'plural1 with context' => ['translation0 with context', 'translation1 with context'],
'Product' . "\0" . 'Products' => 'Produkt' . "\0" . 'Produkte',
],
];
5 changes: 4 additions & 1 deletion tests/phpunit/unit/data/example-simple.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ msgid_plural "plural1 with context"
msgstr[0] "translation0 with context"
msgstr[1] "translation1 with context"


msgid "Product"
msgid_plural "Products"
msgstr[0] "Produkt"
msgstr[1] "Produkte"
4 changes: 4 additions & 0 deletions tests/phpunit/unit/tests/Ginger_MO_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public function test_simple_translation_files( string $file ) {
$this->assertSame( 'translation1 with context', $ginger_mo->translate_plural( array( 'plural0 with context', 'plural1 with context' ), 0, 'context', 'unittest' ) );
$this->assertSame( 'translation0 with context', $ginger_mo->translate_plural( array( 'plural0 with context', 'plural1 with context' ), 1, 'context', 'unittest' ) );
$this->assertSame( 'translation1 with context', $ginger_mo->translate_plural( array( 'plural0 with context', 'plural1 with context' ), 2, 'context', 'unittest' ) );

$this->assertSame( 'Produkt', $ginger_mo->translate( 'Product', '', 'unittest' ) );
$this->assertSame( 'Produkt', $ginger_mo->translate_plural( array( 'Product', 'Products' ), 1, '', 'unittest' ) );
$this->assertSame( 'Produkte', $ginger_mo->translate_plural( array( 'Product', 'Products' ), 2, '', 'unittest' ) );
}

/**
Expand Down

0 comments on commit 35b07b8

Please sign in to comment.