Description
Q | A |
---|---|
Bug report? | yes |
Feature request? | no |
BC Break report? | no |
RFC? | no |
Symfony version | 3.2.0+ |
I have found a serious bug in Symfony translate 3.2.0+ that causes an InvalidArgumentException to be thrown on a common use case involving PO files.
Esentially when a translation has 2 plural forms (e.g. "There is 1 apple" and "There are 2 apples") in a PO file that are not set in a target language an exception is thrown, rather than an empty string returned as in previous versions.
I am attaching a minimal test case.
PO file:
msgid "There is currently 1 bug"
msgid_plural "There are currently %count% bugs"
msgstr[0] "Es gibt derzeit ein Fehler"
msgstr[1] "Es gibt derzeit %count% Fehler"
# Untranslated (breaks) (this is the way most standard tools like gettext will represent an untranslated entry)
msgid "There is currently 1 issue"
msgid_plural "There are currently %count% issues"
msgstr[0] ""
msgstr[1] ""
PHP code:
require __DIR__.'/vendor/autoload.php';
$translate = new \Symfony\Component\Translation\Translator('de_DE');
$translate->addLoader('pofile', new \Symfony\Component\Translation\Loader\PoFileLoader());
$translate->addResource('pofile', __DIR__.'/test.po', 'de_DE');
// This works fine
echo $translate->transChoice('There are currently %count% bugs', 0) . "\n";
echo $translate->transChoice('There are currently %count% bugs', 1) . "\n";
// This crashes with an InvalidArgumentException
echo $translate->transChoice('There are currently %count% issues', 0) . "\n";
Looking at the code, the bug was introduced in #17662.
I have created a PR, #24594, to work around this bug, it handles this case, and also cases where 3 or more plural forms are empty in a PO file (see #24359).
The PR is based against the 3.2 branch. If that's incorrect let me know and I'll rebase it.
Happy to provide further details if needed.
Bjorn