Skip to content

Support for all Symfony translation versions #327

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

Merged
merged 3 commits into from
Sep 5, 2019
Merged
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
75 changes: 54 additions & 21 deletions Catalogue/Operation/ReplaceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Translation\Bundle\Catalogue\Operation;

use Symfony\Component\Translation\Catalogue\AbstractOperation;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\MetadataAwareInterface;

/**
Expand All @@ -36,40 +37,72 @@ protected function processDomain($domain)
'new' => [],
'obsolete' => [],
];
$sourceMessages = $this->source->all($domain);
if (defined(sprintf('%s::INTL_DOMAIN_SUFFIX', MessageCatalogueInterface::class))) {
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
} else {
$intlDomain = $domain;
}

foreach ($this->target->all($domain) as $id => $message) {
foreach ($this->source->all($domain) as $id => $message) {
$messageDomain = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->messages[$domain]['all'][$id] = $message;
$this->result->add([$id => $message], $messageDomain);

if (!$this->target->has($id, $domain)) {
// No merge required
$this->messages[$domain]['new'][$id] = $message;
$resultMeta = $this->getMetadata($this->source, $messageDomain, $id);
} else {
// Merge required
$resultMeta = null;
$sourceMeta = $this->getMetadata($this->source, $messageDomain, $id);
$targetMeta = $this->getMetadata($this->target, $this->target->defines($id, $intlDomain) ? $intlDomain : $domain, $id);
if (is_array($sourceMeta) && is_array($targetMeta)) {
// We can only merge meta if both is an array
$resultMeta = $this->mergeMetadata($sourceMeta, $targetMeta);
} elseif (!empty($sourceMeta)) {
$resultMeta = $sourceMeta;
} else {
// Assert: true === empty($sourceMeta);
$resultMeta = $targetMeta;
}
}

// If $id is NOT defined in source.
if (!array_key_exists($id, $sourceMessages)) {
$this->messages[$domain]['obsolete'][$id] = $message;
if (!empty($resultMeta)) {
$this->result->setMetadata($id, $resultMeta, $messageDomain);
}
}

foreach ($sourceMessages as $id => $message) {
if (!empty($message)) {
$this->messages[$domain]['all'][$id] = $message;
foreach ($this->target->all($domain) as $id => $message) {
if ($this->result->has($id, $domain)) {
// We've already merged this
// That message was in source
continue;
}

if (!$this->target->has($id, $domain)) {
$this->messages[$domain]['new'][$id] = $message;
$messageDomain = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->messages[$domain]['all'][$id] = $message;
$this->messages[$domain]['obsolete'][$id] = $message;
$this->result->add([$id => $message], $messageDomain);

// Make sure to add it to the source if even if empty($message)
$this->messages[$domain]['all'][$id] = $message;
$resultMeta = $this->getMetadata($this->target, $messageDomain, $id);
if (!empty($resultMeta)) {
$this->result->setMetadata($id, $resultMeta, $messageDomain);
}
}
}

$this->result->add($this->messages[$domain]['all'], $domain);

$targetMetadata = $this->target instanceof MetadataAwareInterface ? $this->target->getMetadata('', $domain) : [];
$sourceMetadata = $this->source instanceof MetadataAwareInterface ? $this->source->getMetadata('', $domain) : [];
$resultMetadata = $this->mergeMetaData($sourceMetadata, $targetMetadata);

// Write back metadata
foreach ($resultMetadata as $id => $data) {
$this->result->setMetadata($id, $data, $domain);
/**
* @return array|null|string|mixed Can return anything..
*/
private function getMetadata(MessageCatalogueInterface $catalogue, string $domain, string $key = '')
{
if (!$this->target instanceof MetadataAwareInterface) {
return [];
}

/* @var MetadataAwareInterface $catalogue */
return $catalogue->getMetadata($key, $domain);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^7.1",
"symfony/framework-bundle": "^3.4 || ^4.0",
"symfony/validator": "^3.4 || ^4.0",
"symfony/translation": "^3.4 || ^4.0,<4.2",
"symfony/translation": "^3.4 || ^4.0",
"symfony/twig-bundle": "^3.4 || ^4.0",
"symfony/finder": "^3.4 || ^4.0",
"symfony/intl": "^3.4 || ^4.0",
Expand Down