Skip to content

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

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

Closed
wants to merge 2 commits into from
Closed

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

wants to merge 2 commits into from

Conversation

BjornTwachtmann
Copy link
Contributor

@BjornTwachtmann BjornTwachtmann commented Oct 17, 2017

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.

preg_match_all('/(?:\|\||[^\|])++/', $message, $parts);
$parts = array();
if (preg_match('/^\|+$/', $message)) {
$parts = explode('|', $message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is broken if previous parts of the message have escaped pipes in them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof ,sorry, but I don't understand. What do you mean by "previous parts of the message"? Is this function called recursively, or on partial strings?

As far as I can see the regex matches any $message which has only pipes in it. My assumtion is that this means the message itself is empty. Could you explain if this is incorrect, I'm not that familiar with Translate internals.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm this fix the issue I have reported previously #24359

When for example a translation has 3 empty plurals then the value of $message is "||" because PoFileLoader.php use pipes as a concatenator. This PR fix the cases where the messages are empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if only some of the plurals are empty ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only some of the plurals are empty, $message should contain characters other than | , and the /^|+$/ regex will not match. Execution should then proceed into the elseif block and behaviour will be exactly the same as the current code.

I appreciate that my patch doesn't fix the case you describe. However it doesn't break that case any further, and I cannot figure out how to fix that case at this stage in the program's execution. As I mentioned in the linked ticket, I could only fix that by revisiting the idea of representing multiple translations as a pipe separated string, and I'm not familiar enough with all of the use cases for Translate to attempt that.

This does fix the most common case in PO files, that's all I'm attempting to do. It would make sense for somebody with more knowledge of translate to address the edge cases you describe, but until that can be done this will at least prevent a lot of problems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I missed the fact that it was anchored on both ends

@javiereguiluz javiereguiluz changed the title [Translation][transChoice] Fix InvalidArgumentException when using un… [Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files Oct 18, 2017
@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Oct 18, 2017
@@ -49,10 +49,16 @@ class MessageSelector
*/
public function choose($message, $number, $locale)
{
preg_match_all('/(?:\|\||[^\|])++/', $message, $parts);
$parts = array();
if (preg_match('/^\|+$/', $message)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change the regex to ^\|++$ to avoid useless backtracking (which could cause issues with long strings). Backtracking will never allow to find a match in a different way for this regex.

@BjornTwachtmann
Copy link
Contributor Author

@stof , changes made as requested :)

@nicolas-grekas nicolas-grekas changed the base branch from 3.2 to 3.3 November 5, 2017 15:31
@nicolas-grekas
Copy link
Member

But tests are failing.

@BjornTwachtmann
Copy link
Contributor Author

@nicolas-grekas I've rebased the branch on 3.3 and all tests pass now. I think there were some failing tests in the 3.2 branch

@BjornTwachtmann
Copy link
Contributor Author

@nicolas-grekas @stof tests are passing, is this okay to merge now?

@Tasiobg
Copy link

Tasiobg commented Dec 7, 2017

This has been rebased on 3.3 and tests are passing. Can this be merged please?

@xabbuh
Copy link
Member

xabbuh commented Dec 8, 2017

Are 2.7 and 2.8 not affected?

@BPScott
Copy link
Contributor

BPScott commented Dec 8, 2017

@xabbuh the escaping code that this touches was added in v3.2 in #17662

@fabpot
Copy link
Member

fabpot commented Dec 11, 2017

Thank you @BjornTwachtmann.

fabpot added a commit that referenced this pull request 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
@fabpot fabpot closed this Dec 11, 2017
This was referenced Dec 15, 2017
@fabpot fabpot mentioned this pull request Jan 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants