Skip to content

Commit

Permalink
Make sure importer is ICU format aware
Browse files Browse the repository at this point in the history
Add new argument to converter methods to make sure we're able to
recognize existing translations (and use correct domain - either
standard or ICU format).
In case translation is new we're going to use predefined format (using
new configuration option new_message_format).

php-translation#300
  • Loading branch information
snpy committed Jan 28, 2022
1 parent 419d580 commit a0b22d3
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Service/Importer.php
Expand Up @@ -11,6 +11,7 @@

namespace Translation\Bundle\Service;

use Nyholm\NSA;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;
use Translation\Bundle\Catalogue\Operation\ReplaceOperation;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co
$results = [];
foreach ($catalogues as $catalogue) {
$target = new MessageCatalogue($catalogue->getLocale());
$this->convertSourceLocationsToMessages($target, $sourceCollection);
$this->convertSourceLocationsToMessages($target, $sourceCollection, $catalogue);

// Remove all SourceLocation and State form catalogue.
foreach ($catalogue->getDomains() as $domain) {
Expand Down Expand Up @@ -120,8 +121,13 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co
return new ImportResult($results, $sourceCollection->getErrors());
}

private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection): void
{
private function convertSourceLocationsToMessages(
MessageCatalogue $catalogue,
SourceCollection $collection,
MessageCatalogue $currentCatalogue
): void {
$currentMessages = NSA::getProperty($currentCatalogue, 'messages');

/** @var SourceLocation $sourceLocation */
foreach ($collection as $sourceLocation) {
$context = $sourceLocation->getContext();
Expand All @@ -131,19 +137,31 @@ private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, S
continue;
}

$intlDomain = $domain . '+intl-icu' /* MessageCatalogueInterface::INTL_DOMAIN_SUFFIX */;

$key = $sourceLocation->getMessage();
$catalogue->add([$key => null], $domain);

if (array_key_exists($key, $currentMessages[$intlDomain] ?? [])) {
$messageDomain = $intlDomain;
} elseif (array_key_exists($key, $currentMessages[$domain] ?? [])) {
$messageDomain = $domain;
} else {
// New translation
$messageDomain = 'icu' === $this->config['new_message_format'] ? $intlDomain : $domain;
}

$catalogue->add([$key => null], $messageDomain);
$trimLength = 1 + \strlen($this->config['project_root']);

$meta = $this->getMetadata($catalogue, $key, $domain);
$meta = $this->getMetadata($catalogue, $key, $messageDomain);
$meta->addCategory('file-source', \sprintf('%s:%s', \substr($sourceLocation->getPath(), $trimLength), $sourceLocation->getLine()));
if (isset($sourceLocation->getContext()['desc'])) {
$meta->addCategory('desc', $sourceLocation->getContext()['desc']);
}
if (isset($sourceLocation->getContext()['translation'])) {
$meta->addCategory('translation', $sourceLocation->getContext()['translation']);
}
$this->setMetadata($catalogue, $key, $domain, $meta);
$this->setMetadata($catalogue, $key, $messageDomain, $meta);
}
}

Expand Down

0 comments on commit a0b22d3

Please sign in to comment.