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

Allow phrases to contain more than nine numeric placeholders #1375

Merged
merged 3 commits into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions lib/internal/Magento/Framework/Phrase/Renderer/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ public function render(array $source, array $arguments)
$text = end($source);

if ($arguments) {
$placeholders = [];
foreach (array_keys($arguments) as $key) {
$placeholders[] = '%' . (is_int($key) ? strval($key + 1) : $key);
}
$text = str_replace($placeholders, $arguments, $text);
$placeholders = array_map([$this, 'keyToPlaceholder'], array_keys($arguments));
$pairs = array_combine($placeholders, $arguments);
$text = strtr($text, $pairs);
}

return $text;
}

private function keyToPlaceholder($key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just make it a closure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that initially, but the anonymous function didn't inherit any variables from its parent scope (no use directive) so it seemed like a needless performance overhead to instantiate a Closure every time render() is called.

{
return '%' . (is_int($key) ? strval($key + 1) : $key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ public function renderPlaceholderDataProvider()
'three text two text one'
],
['text %1 text %2 text', [], 'text %1 text %2 text'],
['%1 text %2', ['one'], 'one text %2']
['%1 text %2', ['one'], 'one text %2'],
[
'%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11',
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'],
'one two three four five six seven eight nine ten eleven',
],
['A %table has four legs', ['tab' => 'Tab-Leiste', 'able' => '', 'table' => 'Tabelle'], 'A Tabelle has four legs'],
];
}
}