Skip to content

[Translation] Allow to escape pipe #15206

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 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/MessageSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MessageSelector
*/
public function choose($message, $number, $locale)
{
$parts = explode('|', $message);
$parts = preg_split('/(?<!\\\\)\|/', $message)
Copy link
Contributor

Choose a reason for hiding this comment

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

But it doesn't allow to escape backslash itself.

Choose a reason for hiding this comment

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

This should be enough for any case:

$parts = array_map(function($value){
    return str_replace('\|','|',str_replace('\\\\','\\',$value));
},preg_split( '/(?<![^\\\\]\\\\)\|/', $message));

It ignores escaped pipes, ignore escaped backslashes and after split it replaces escaped pipes to pipes and escaped backslashes to backslash.

there needs to be 4 backslashes to escape to one before a pipe though, but hey, it's a way.

Copy link
Contributor

Choose a reason for hiding this comment

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

@emiliopedrollo I used your proposal see #17662

$explicitRules = array();
$standardRules = array();
foreach ($parts as $part) {
Expand Down