Skip to content

Commit c3143c2

Browse files
committed
[Translation] Improve handling of non-string messages in MessageCatalogue
1 parent d3092c7 commit c3143c2

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
3232
public function __construct(string $locale, array $messages = [])
3333
{
3434
$this->locale = $locale;
35+
foreach ($messages as $domain => $domainMessages) {
36+
foreach ($domainMessages as $key => $message) {
37+
if (null === $message) {
38+
unset($messages[$domain][$key]);
39+
} else {
40+
$messages[$domain][$key] = (string) $message;
41+
}
42+
}
43+
}
3544
$this->messages = $messages;
3645
}
3746

@@ -157,8 +166,12 @@ public function add(array $messages, string $domain = 'messages')
157166
{
158167
$altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;
159168
foreach ($messages as $id => $message) {
160-
unset($this->messages[$altDomain][$id]);
161-
$this->messages[$domain][$id] = $message;
169+
if (null === $message) {
170+
unset($this->messages[$domain][$id]);
171+
} else {
172+
unset($this->messages[$altDomain][$id]);
173+
$this->messages[$domain][$id] = (string) $message;
174+
}
162175
}
163176

164177
if ([] === ($this->messages[$altDomain] ?? null)) {

src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ public function testAdd()
118118
$this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
119119
}
120120

121+
public function testAddWithNonStringMessages()
122+
{
123+
$catalogue = new MessageCatalogue('en', [
124+
'domain1' => ['foo' => 'foo', 'bar' => new StringableObject('bar'), 'baz' => null],
125+
]);
126+
$this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $catalogue->all('domain1'));
127+
128+
$catalogue->add(['foo1' => 'foo1', 'bar1' => new StringableObject('bar1'), 'baz1' => null], 'domain1');
129+
$this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'foo1' => 'foo1', 'bar1' => 'bar1'], $catalogue->all('domain1'));
130+
131+
$catalogue->add(['bar' => null, 'bar1' => null], 'domain1');
132+
$this->assertSame(['foo' => 'foo', 'foo1' => 'foo1'], $catalogue->all('domain1'));
133+
}
134+
121135
public function testAddIntlIcu()
122136
{
123137
$catalogue = new MessageCatalogue('en', ['domain1+intl-icu' => ['foo' => 'foo']]);
@@ -271,3 +285,18 @@ public function testMetadataMerge()
271285
$this->assertEquals(['messages' => ['a' => 'b'], 'domain' => ['b' => 'c']], $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
272286
}
273287
}
288+
289+
class StringableObject
290+
{
291+
public $value;
292+
293+
public function __construct(string $value)
294+
{
295+
$this->value = $value;
296+
}
297+
298+
public function __toString(): string
299+
{
300+
return $this->value;
301+
}
302+
}

0 commit comments

Comments
 (0)