Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translation] Crashes when translation for plural form not found in PO file #24593

Closed
BjornTwachtmann opened this issue Oct 17, 2017 · 0 comments

Comments

@BjornTwachtmann
Copy link
Contributor

BjornTwachtmann commented Oct 17, 2017

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

@fabpot fabpot closed this as completed Dec 11, 2017
fabpot added a commit that referenced this issue Dec 11, 2017
…anslated plural forms from .po files (BjornTwachtmann)

This PR was squashed before being merged into the 3.3 branch (closes #24594).

Discussion
----------

[Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24593
| License       | MIT

This PR fixes the bug in #24593. It's not the absolutely ideal way to address the issue, but I don't see how else to handle it without either dropping support for pips in translation strings, or rearchitecting the code that feeds translations to the MessageSelector as pipe separated in the first place.

Commits
-------

fea815b [Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants