Skip to content

Commit eca2e2c

Browse files
author
Ji Lu
committed
MAGETWO-36450: Set placeholder as default render for \Magento\Framework\Phrase if no renderer is set.
1 parent 948f734 commit eca2e2c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/internal/Magento/Framework/Phrase.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Zend\Stdlib\JsonSerializable;
1111
use Magento\Framework\Phrase\RendererInterface;
12+
use Magento\Framework\Phrase\Renderer\Placeholder;
1213

1314
class Phrase implements JsonSerializable
1415
{
@@ -51,7 +52,11 @@ public static function setRenderer(RendererInterface $renderer)
5152
*/
5253
public static function getRenderer()
5354
{
54-
return self::$renderer;
55+
if (self::$renderer) {
56+
return self::$renderer;
57+
} else {
58+
self::$renderer = new \Magento\Framework\Phrase\Renderer\Placeholder();
59+
}
5560
}
5661

5762
/**
@@ -93,7 +98,7 @@ public function getArguments()
9398
*/
9499
public function render()
95100
{
96-
return self::$renderer ? self::$renderer->render([$this->text], $this->arguments) : $this->text;
101+
return $this->getRenderer()->render([$this->text], $this->arguments);
97102
}
98103

99104
/**
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Phase
1+
# Phrase
22

3-
Class *\Magento\Framework\Phrase* calls renderer to make the translation of the text. **Phase** provides *RedererInterface* and a few renderers to support different kinds of needs of translation of the text. Here are list of renderers in this library:
3+
Class *\Magento\Framework\Phrase* calls renderer to make the translation of the text. **Phrase** provides *RendererInterface* and a few renderers to support different kinds of needs of translation of the text. Here are list of renderers in this library:
44

5+
* Placeholder render - it replaces placeholders with parameters for substitution. It is the default render if none is set for the Phrase.
56
* Translate render - it is a base renderer that implements text translations.
67
* Inline render - it adds inline translate part to text translation and returns the strings by a template.
7-
* Placeholder render - it replaces placeholders with parameters for substitution.
88
* Composite render - it can have several renderers, calls each renderer for processing the text. Array of renderer class names pass into composite render constructor as a parameter.

lib/internal/Magento/Framework/Test/Unit/PhraseTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ public function testGetArguments()
124124
$this->assertEquals([], $phrase1->getArguments());
125125
$this->assertEquals($arguments, $phrase2->getArguments());
126126
}
127+
128+
/**
129+
* Test default rendering
130+
*
131+
* @return void
132+
*/
133+
public function testDefaultRendering()
134+
{
135+
$text = 'parameter1 is replaced by %1 parameter2 is replaced by %2';
136+
$arguments = ['arg1', 'arg2'];
137+
$result = 'parameter1 is replaced by arg1 parameter2 is replaced by arg2';
138+
$phrase = new Phrase($text, $arguments);
139+
140+
$this->assertEquals($text, $phrase->getText());
141+
$this->assertEquals($arguments, $phrase->getArguments());
142+
$this->assertTrue($phrase->getRenderer() instanceof \Magento\Framework\Phrase\Renderer\Placeholder);
143+
$this->assertEquals($result, $phrase->render());
144+
}
127145
}

0 commit comments

Comments
 (0)