Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] Improve handling of non-string messages in MessageCatalogue #50252

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
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
17 changes: 15 additions & 2 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
public function __construct(string $locale, array $messages = [])
{
$this->locale = $locale;
foreach ($messages as $domain => $domainMessages) {
foreach ($domainMessages as $key => $message) {
if (null === $message) {
unset($messages[$domain][$key]);
} else {
$messages[$domain][$key] = (string) $message;
}
}
}
$this->messages = $messages;
}

Expand Down Expand Up @@ -157,8 +166,12 @@ public function add(array $messages, string $domain = 'messages')
{
$altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;
foreach ($messages as $id => $message) {
unset($this->messages[$altDomain][$id]);
$this->messages[$domain][$id] = $message;
if (null === $message) {
unset($this->messages[$domain][$id]);
} else {
unset($this->messages[$altDomain][$id]);
$this->messages[$domain][$id] = (string) $message;
Copy link
Member

Choose a reason for hiding this comment

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

But the (string) $message cast will throw an exception if for objects/resources that can't be converted into strings. Maybe it's what we want (to fail loudly so you can catch these errors) but I just wanted to mention it. Thanks.

}
}

if ([] === ($this->messages[$altDomain] ?? null)) {
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ public function testAdd()
$this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
}

public function testAddWithNonStringMessages()
{
$catalogue = new MessageCatalogue('en', [
'domain1' => ['foo' => 'foo', 'bar' => new StringableObject('bar'), 'baz' => null],
]);
$this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $catalogue->all('domain1'));

$catalogue->add(['foo1' => 'foo1', 'bar1' => new StringableObject('bar1'), 'baz1' => null], 'domain1');
$this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'foo1' => 'foo1', 'bar1' => 'bar1'], $catalogue->all('domain1'));

$catalogue->add(['bar' => null, 'bar1' => null], 'domain1');
$this->assertSame(['foo' => 'foo', 'foo1' => 'foo1'], $catalogue->all('domain1'));
}

public function testAddIntlIcu()
{
$catalogue = new MessageCatalogue('en', ['domain1+intl-icu' => ['foo' => 'foo']]);
Expand Down Expand Up @@ -271,3 +285,18 @@ public function testMetadataMerge()
$this->assertEquals(['messages' => ['a' => 'b'], 'domain' => ['b' => 'c']], $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
}
}

class StringableObject
{
public $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function __toString(): string
{
return $this->value;
}
}