Skip to content

Commit

Permalink
changes MessageCatalogue.php to returns intl-icu messages only if req…
Browse files Browse the repository at this point in the history
…uested

reverted MessageCatalogueInterface.php to initial version to keep BC
  • Loading branch information
artemoliynyk committed Apr 22, 2020
1 parent fecc728 commit b4478f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function filterCatalogue(MessageCatalogue $catalogue, string $domain): MessageCatalogue
{
$filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;

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

// 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
10 changes: 6 additions & 4 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ public function getDomains()
/**
* {@inheritdoc}
*/
public function all(string $domain = null, $strict = false)
public function all(string $domain = null)
{
if (null !== $domain) {
if ($strict) {

// skip messages merge if intl-icu requested explicitly
if (false !== strpos($domain, self::INTL_DOMAIN_SUFFIX)) {
return $this->messages[$domain] ?? [];
} else {
return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
}

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

$allMessages = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public function getDomains();
* If $domain is null, it returns all messages.
*
* @param string $domain The domain name
* @param bool $strict - return messages for defined domain only
*
* @return array An array of messages
*/
public function all(string $domain = null, $strict = false);
public function all(string $domain = null);

/**
* Sets a message translation.
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function testAll()
$this->assertEquals($messages, $catalogue->all());

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

$this->assertEquals(['foo' => 'bar'], $catalogue->all('domain1'));
Expand Down Expand Up @@ -69,13 +69,13 @@ public function testAllIntICU()
$catalogue = new MessageCatalogue('en', $messages);

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

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

// intl-icu ignored
$messagesExpected = [
'domain1' => ['foo' => 'bar'],
'domain2' => ['bar' => 'foo', 'biz' => 'biz'],
Expand Down

0 comments on commit b4478f3

Please sign in to comment.