Skip to content

Commit

Permalink
[Translation] Fix for translation:update command updating ICU messages
Browse files Browse the repository at this point in the history
  • Loading branch information
artemoliynyk authored and nicolas-grekas committed Apr 30, 2020
1 parent f8d3b06 commit 567cee5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Expand Up @@ -356,7 +356,14 @@ private function filterCatalogue(MessageCatalogue $catalogue, string $domain): M
{
$filteredCatalogue = new MessageCatalogue($catalogue->getLocale());

if ($messages = $catalogue->all($domain)) {
// extract intl-icu messages only
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
if ($intlMessages = $catalogue->all($intlDomain)) {
$filteredCatalogue->add($intlMessages, $intlDomain);
}

// extract all messages and subtract intl-icu messages
if ($messages = array_diff($catalogue->all($domain), $intlMessages)) {
$filteredCatalogue->add($messages, $domain);
}
foreach ($catalogue->getResources() as $resource) {
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Expand Up @@ -72,6 +72,11 @@ public function getDomains()
public function all($domain = null)
{
if (null !== $domain) {
// skip messages merge if intl-icu requested explicitly
if (false !== strpos($domain, self::INTL_DOMAIN_SUFFIX)) {
return $this->messages[$domain] ?? [];
}

return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
Expand Up @@ -67,6 +67,30 @@ public function testAll()
$this->assertEquals($messages, $catalogue->all());
}

public function testAllIntICU()
{
$messages = [
'domain1+intl-icu' => ['foo' => 'bar'],
'domain2+intl-icu' => ['bar' => 'foo'],
'domain2' => ['biz' => 'biz'],
];
$catalogue = new MessageCatalogue('en', $messages);

// separated domains
$this->assertSame(['foo' => 'bar'], $catalogue->all('domain1+intl-icu'));
$this->assertSame(['bar' => 'foo'], $catalogue->all('domain2+intl-icu'));

// merged, intl-icu ignored
$this->assertSame(['bar' => 'foo', 'biz' => 'biz'], $catalogue->all('domain2'));

// intl-icu ignored
$messagesExpected = [
'domain1' => ['foo' => 'bar'],
'domain2' => ['bar' => 'foo', 'biz' => 'biz'],
];
$this->assertSame($messagesExpected, $catalogue->all());
}

public function testHas()
{
$catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo'], 'domain2+intl-icu' => ['bar' => 'bar']]);
Expand Down

0 comments on commit 567cee5

Please sign in to comment.