From dca1618ca81565f472e6b737454056a78cd1619e Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Fri, 8 Jun 2018 16:39:04 +0200 Subject: [PATCH 01/13] Default translation --- Service/Importer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Service/Importer.php b/Service/Importer.php index ad0287a0..aeafc712 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -83,6 +83,7 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co $meta->removeAllInCategory('file-source'); $meta->removeAllInCategory('state'); $this->setMetadata($catalogue, $key, $domain, $meta); + $catalogue->set($key, $translation !== ''? $translation : '_'.$key); } } From 5fee9781946a3c0959ef4b63355faad8f33afe16 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Fri, 8 Jun 2018 16:41:51 +0200 Subject: [PATCH 02/13] Default translation --- Service/Importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/Importer.php b/Service/Importer.php index aeafc712..f3b57527 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -83,7 +83,6 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co $meta->removeAllInCategory('file-source'); $meta->removeAllInCategory('state'); $this->setMetadata($catalogue, $key, $domain, $meta); - $catalogue->set($key, $translation !== ''? $translation : '_'.$key); } } @@ -97,6 +96,7 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co $meta = $this->getMetadata($result, $key, $domain); $meta->setState('new'); $this->setMetadata($result, $key, $domain, $meta); + $catalogue->set($key, $translation !== ''? $translation : $key); // Add custom translations that we found in the source if (null === $translation) { From 94430465e5b851d2c6acf23ac4a5386376927cff Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Fri, 8 Jun 2018 16:55:32 +0200 Subject: [PATCH 03/13] Default translation --- Service/Importer.php | 383 +++++++++++++++++++++++-------------------- 1 file changed, 209 insertions(+), 174 deletions(-) diff --git a/Service/Importer.php b/Service/Importer.php index f3b57527..553b59e6 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -29,208 +29,243 @@ */ final class Importer { - /** - * @var Extractor - */ - private $extractor; - - /** - * @var array - */ - private $config; - - /** - * @var \Twig_Environment - */ - private $twig; - - /** - * @param Extractor $extractor - * @param \Twig_Environment $twig - */ - public function __construct(Extractor $extractor, \Twig_Environment $twig) - { - $this->extractor = $extractor; - $this->twig = $twig; - } + /** + * @var Extractor + */ + private $extractor; + + /** + * @var array + */ + private $config; + + /** + * @var \Twig_Environment + */ + private $twig; + + /** + * @param Extractor $extractor + * @param \Twig_Environment $twig + */ + public function __construct(Extractor $extractor, \Twig_Environment $twig) + { + $this->extractor = $extractor; + $this->twig = $twig; + } - /** - * @param Finder $finder - * @param MessageCatalogue[] $catalogues - * @param array $config { - * - * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. - * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. - * @var string $project_root The project root will be removed from the source location. - * } - * - * @return ImportResult - */ - public function extractToCatalogues(Finder $finder, array $catalogues, array $config = []) + /** + * @param Finder $finder + * @param MessageCatalogue[] $catalogues + * @param array $config { + * + * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. + * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. + * @var string $project_root The project root will be removed from the source location. + * } + * + * @return ImportResult + */ + public function extractToCatalogues(Finder $finder, array $catalogues, array $config = []) + { + $this->processConfig($config); + $this->disableTwigVisitors(); + $sourceCollection = $this->extractor->extract($finder); + $results = []; + foreach ($catalogues as $catalogue) { - $this->processConfig($config); - $this->disableTwigVisitors(); - $sourceCollection = $this->extractor->extract($finder); - $results = []; - foreach ($catalogues as $catalogue) { - $target = new MessageCatalogue($catalogue->getLocale()); - $this->convertSourceLocationsToMessages($target, $sourceCollection); - - // Remove all SourceLocation and State form catalogue. - foreach ($catalogue->getDomains() as $domain) { - foreach ($catalogue->all($domain) as $key => $translation) { - $meta = $this->getMetadata($catalogue, $key, $domain); - $meta->removeAllInCategory('file-source'); - $meta->removeAllInCategory('state'); - $this->setMetadata($catalogue, $key, $domain, $meta); - } - } + $target = new MessageCatalogue($catalogue->getLocale()); + $this->convertSourceLocationsToMessages($target, $sourceCollection); - $merge = new ReplaceOperation($target, $catalogue); - $result = $merge->getResult(); - $domains = $merge->getDomains(); - - // Mark new messages as new/obsolete - foreach ($domains as $domain) { - foreach ($merge->getNewMessages($domain) as $key => $translation) { - $meta = $this->getMetadata($result, $key, $domain); - $meta->setState('new'); - $this->setMetadata($result, $key, $domain, $meta); - $catalogue->set($key, $translation !== ''? $translation : $key); - - // Add custom translations that we found in the source - if (null === $translation) { - if (null !== $newTranslation = $meta->getTranslation()) { - $result->set($key, $newTranslation, $domain); - // We do not want "translation" key stored anywhere. - $meta->removeAllInCategory('translation'); - } elseif (null !== $newTranslation = $meta->getDesc()) { - $result->set($key, $newTranslation, $domain); - } - } - } - foreach ($merge->getObsoleteMessages($domain) as $key => $translation) { - $meta = $this->getMetadata($result, $key, $domain); - $meta->setState('obsolete'); - $this->setMetadata($result, $key, $domain, $meta); - } - } - $results[] = $result; + // Remove all SourceLocation and State form catalogue. + foreach ($catalogue->getDomains() as $domain) + { + foreach ($catalogue->all($domain) as $key => $translation) + { + $meta = $this->getMetadata($catalogue, $key, $domain); + $meta->removeAllInCategory('file-source'); + $meta->removeAllInCategory('state'); + $this->setMetadata($catalogue, $key, $domain, $meta); } + } - return new ImportResult($results, $sourceCollection->getErrors()); - } + $merge = new ReplaceOperation($target, $catalogue); + $result = $merge->getResult(); + $domains = $merge->getDomains(); - /** - * @param MessageCatalogue $catalogue - * @param SourceCollection $collection - */ - private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) - { - /** @var SourceLocation $sourceLocation */ - foreach ($collection as $sourceLocation) { - $context = $sourceLocation->getContext(); - $domain = isset($context['domain']) ? $context['domain'] : 'messages'; - // Check with white/black list - if (!$this->isValidDomain($domain)) { - continue; - } + // Mark new messages as new/obsolete + foreach ($domains as $domain) + { + foreach ($merge->getNewMessages($domain) as $key => $translation) + { + $meta = $this->getMetadata($result, $key, $domain); + $meta->setState('new'); + $this->setMetadata($result, $key, $domain, $meta); - $key = $sourceLocation->getMessage(); - $catalogue->set($key, null, $domain); - $trimLength = 1 + strlen($this->config['project_root']); + $this->setTarget($catalogue, $key, '_', $domain, $translation); /* @todo make une const prefix */ - $meta = $this->getMetadata($catalogue, $key, $domain); - $meta->addCategory('file-source', sprintf('%s:%s', substr($sourceLocation->getPath(), $trimLength), $sourceLocation->getLine())); - if (isset($sourceLocation->getContext()['desc'])) { - $meta->addCategory('desc', $sourceLocation->getContext()['desc']); + // Add custom translations that we found in the source + if (null === $translation) + { + if (null !== $newTranslation = $meta->getTranslation()) + { + $result->set($key, $newTranslation, $domain); + // We do not want "translation" key stored anywhere. + $meta->removeAllInCategory('translation'); } - if (isset($sourceLocation->getContext()['translation'])) { - $meta->addCategory('translation', $sourceLocation->getContext()['translation']); + elseif (null !== $newTranslation = $meta->getDesc()) + { + $result->set($key, $newTranslation, $domain); } - $this->setMetadata($catalogue, $key, $domain, $meta); + } + } + foreach ($merge->getObsoleteMessages($domain) as $key => $translation) + { + $meta = $this->getMetadata($result, $key, $domain); + $meta->setState('obsolete'); + $this->setMetadata($result, $key, $domain, $meta); } + } + $results[] = $result; } - /** - * @param MessageCatalogue $catalogue - * @param $key - * @param $domain - * - * @return Metadata - */ - private function getMetadata(MessageCatalogue $catalogue, $key, $domain) + return new ImportResult($results, $sourceCollection->getErrors()); + } + + /** + * @param MessageCatalogue $catalogue + * @param SourceCollection $collection + */ + private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) + { + /** @var SourceLocation $sourceLocation */ + foreach ($collection as $sourceLocation) { - return new Metadata($catalogue->getMetadata($key, $domain)); + $context = $sourceLocation->getContext(); + $domain = isset($context['domain']) ? $context['domain'] : 'messages'; + // Check with white/black list + if (!$this->isValidDomain($domain)) + { + continue; + } + + $key = $sourceLocation->getMessage(); + $catalogue->set($key, null, $domain); + $trimLength = 1 + strlen($this->config['project_root']); + + $meta = $this->getMetadata($catalogue, $key, $domain); + $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); } + } + + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $domain + * + * @return Metadata + */ + private function getMetadata(MessageCatalogue $catalogue, $key, $domain) + { + return new Metadata($catalogue->getMetadata($key, $domain)); + } + + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $domain + * @param Metadata $metadata + */ + private function setMetadata(MessageCatalogue $catalogue, $key, $domain, Metadata $metadata) + { + $catalogue->setMetadata($key, $metadata->toArray(), $domain); + } + + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $prefix + * @param $domain + * @param string $translation + */ + private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') + { + $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); + } - /** - * @param MessageCatalogue $catalogue - * @param $key - * @param $domain - * @param Metadata $metadata - */ - private function setMetadata(MessageCatalogue $catalogue, $key, $domain, Metadata $metadata) + /** + * @param string $domain + * + * @return bool + */ + private function isValidDomain($domain) + { + if (!empty($this->config['blacklist_domains']) && in_array($domain, $this->config['blacklist_domains'])) { - $catalogue->setMetadata($key, $metadata->toArray(), $domain); + return false; } - - /** - * @param string $domain - * - * @return bool - */ - private function isValidDomain($domain) + if (!empty($this->config['whitelist_domains']) && !in_array($domain, $this->config['whitelist_domains'])) { - if (!empty($this->config['blacklist_domains']) && in_array($domain, $this->config['blacklist_domains'])) { - return false; - } - if (!empty($this->config['whitelist_domains']) && !in_array($domain, $this->config['whitelist_domains'])) { - return false; - } - - return true; + return false; } - /** - * Make sure the configuration is valid. - * - * @param array $config - */ - private function processConfig($config) - { - $default = [ - 'project_root' => '', - 'blacklist_domains' => [], - 'whitelist_domains' => [], - ]; + return true; + } - $config = array_merge($default, $config); + /** + * Make sure the configuration is valid. + * + * @param array $config + */ + private function processConfig($config) + { + $default = [ + 'project_root' => '', + 'blacklist_domains' => [], + 'whitelist_domains' => [], + ]; - if (!empty($config['blacklist_domains']) && !empty($config['whitelist_domains'])) { - throw new \InvalidArgumentException('Cannot use "blacklist_domains" and "whitelist_domains" at the same time'); - } + $config = array_merge($default, $config); - if (!empty($config['blacklist_domains']) && !is_array($config['blacklist_domains'])) { - throw new \InvalidArgumentException('Config parameter "blacklist_domains" must be an array'); - } + if (!empty($config['blacklist_domains']) && !empty($config['whitelist_domains'])) + { + throw new \InvalidArgumentException('Cannot use "blacklist_domains" and "whitelist_domains" at the same time'); + } - if (!empty($config['whitelist_domains']) && !is_array($config['whitelist_domains'])) { - throw new \InvalidArgumentException('Config parameter "whitelist_domains" must be an array'); - } + if (!empty($config['blacklist_domains']) && !is_array($config['blacklist_domains'])) + { + throw new \InvalidArgumentException('Config parameter "blacklist_domains" must be an array'); + } - $this->config = $config; + if (!empty($config['whitelist_domains']) && !is_array($config['whitelist_domains'])) + { + throw new \InvalidArgumentException('Config parameter "whitelist_domains" must be an array'); } - private function disableTwigVisitors() + $this->config = $config; + } + + private function disableTwigVisitors() + { + foreach ($this->twig->getNodeVisitors() as $visitor) { - foreach ($this->twig->getNodeVisitors() as $visitor) { - if ($visitor instanceof DefaultApplyingNodeVisitor) { - $visitor->setEnabled(false); - } - if ($visitor instanceof RemovingNodeVisitor) { - $visitor->setEnabled(false); - } - } + if ($visitor instanceof DefaultApplyingNodeVisitor) + { + $visitor->setEnabled(false); + } + if ($visitor instanceof RemovingNodeVisitor) + { + $visitor->setEnabled(false); + } } + } } From 0681803964217b8aa2e3682f26d7b694b70125f7 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:15:44 +0200 Subject: [PATCH 04/13] Default translation --- Command/ExtractCommand.php | 5 +++-- Service/Importer.php | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index 8da04b4d..cdb59d66 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -92,13 +92,14 @@ protected function configure() ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false) ->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not') ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want extract translations from.') + ->addArgument('prefixTarget', InputArgument::OPTIONAL, 'The prefix of Target', null) ; } protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->configurationManager->getConfiguration($input->getArgument('configuration')); - + $prefixTarget = $input->getArgument('prefixTarget'); $locales = []; if ($inputLocale = $input->getArgument('locale')) { $locales = [$inputLocale]; @@ -112,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'blacklist_domains' => $config->getBlacklistDomains(), 'whitelist_domains' => $config->getWhitelistDomains(), 'project_root' => $config->getProjectRoot(), - ]); + ],$prefixTarget); $errors = $result->getErrors(); $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues()); diff --git a/Service/Importer.php b/Service/Importer.php index 553b59e6..b4632282 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -64,9 +64,11 @@ public function __construct(Extractor $extractor, \Twig_Environment $twig) * @var string $project_root The project root will be removed from the source location. * } * + * @param string|null $prefixTarget + * * @return ImportResult */ - public function extractToCatalogues(Finder $finder, array $catalogues, array $config = []) + public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], string $prefixTarget = null) { $this->processConfig($config); $this->disableTwigVisitors(); @@ -101,8 +103,10 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co $meta = $this->getMetadata($result, $key, $domain); $meta->setState('new'); $this->setMetadata($result, $key, $domain, $meta); - - $this->setTarget($catalogue, $key, '_', $domain, $translation); /* @todo make une const prefix */ + if (null !== $prefixTarget) + { + $this->setTarget($catalogue, $key, $prefixTarget, $domain, $translation); + } // Add custom translations that we found in the source if (null === $translation) From 233f3ef83a7e3650ab18bd78c3d39e89c9e5e892 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:18:36 +0200 Subject: [PATCH 05/13] fix style --- Command/ExtractCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index cdb59d66..ae5adb9a 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -113,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'blacklist_domains' => $config->getBlacklistDomains(), 'whitelist_domains' => $config->getWhitelistDomains(), 'project_root' => $config->getProjectRoot(), - ],$prefixTarget); + ], $prefixTarget); $errors = $result->getErrors(); $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues()); From aa92c4bf069e60bf311a373e85f80fb57865f430 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:24:34 +0200 Subject: [PATCH 06/13] fix Style --- Service/Importer.php | 379 ++++++++++++++++++++----------------------- 1 file changed, 177 insertions(+), 202 deletions(-) diff --git a/Service/Importer.php b/Service/Importer.php index b4632282..d84f0b60 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -29,170 +29,142 @@ */ final class Importer { - /** - * @var Extractor - */ - private $extractor; - - /** - * @var array - */ - private $config; - - /** - * @var \Twig_Environment - */ - private $twig; - - /** - * @param Extractor $extractor - * @param \Twig_Environment $twig - */ - public function __construct(Extractor $extractor, \Twig_Environment $twig) - { - $this->extractor = $extractor; - $this->twig = $twig; - } + /** + * @var Extractor + */ + private $extractor; + + /** + * @var array + */ + private $config; + + /** + * @var \Twig_Environment + */ + private $twig; + + /** + * @param Extractor $extractor + * @param \Twig_Environment $twig + */ + public function __construct(Extractor $extractor, \Twig_Environment $twig) + { + $this->extractor = $extractor; + $this->twig = $twig; + } - /** - * @param Finder $finder - * @param MessageCatalogue[] $catalogues - * @param array $config { - * - * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. - * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. - * @var string $project_root The project root will be removed from the source location. - * } - * - * @param string|null $prefixTarget - * - * @return ImportResult - */ - public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], string $prefixTarget = null) - { - $this->processConfig($config); - $this->disableTwigVisitors(); - $sourceCollection = $this->extractor->extract($finder); - $results = []; - foreach ($catalogues as $catalogue) + /** + * @param Finder $finder + * @param MessageCatalogue[] $catalogues + * @param array $config { + * + * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. + * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. + * @var string $project_root The project root will be removed from the source location. + * } + * @param string|null $prefixTarget + * @return ImportResult + */ + public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], string $prefixTarget = null) { - $target = new MessageCatalogue($catalogue->getLocale()); - $this->convertSourceLocationsToMessages($target, $sourceCollection); + $this->processConfig($config); + $this->disableTwigVisitors(); + $sourceCollection = $this->extractor->extract($finder); + $results = []; + foreach ($catalogues as $catalogue) { + $target = new MessageCatalogue($catalogue->getLocale()); + $this->convertSourceLocationsToMessages($target, $sourceCollection); + + // Remove all SourceLocation and State form catalogue. + foreach ($catalogue->getDomains() as $domain) { + foreach ($catalogue->all($domain) as $key => $translation) { + $meta = $this->getMetadata($catalogue, $key, $domain); + $meta->removeAllInCategory('file-source'); + $meta->removeAllInCategory('state'); + $this->setMetadata($catalogue, $key, $domain, $meta); + } + } - // Remove all SourceLocation and State form catalogue. - foreach ($catalogue->getDomains() as $domain) - { - foreach ($catalogue->all($domain) as $key => $translation) - { - $meta = $this->getMetadata($catalogue, $key, $domain); - $meta->removeAllInCategory('file-source'); - $meta->removeAllInCategory('state'); - $this->setMetadata($catalogue, $key, $domain, $meta); + $merge = new ReplaceOperation($target, $catalogue); + $result = $merge->getResult(); + $domains = $merge->getDomains(); + + // Mark new messages as new/obsolete + foreach ($domains as $domain) { + foreach ($merge->getNewMessages($domain) as $key => $translation) { + $meta = $this->getMetadata($result, $key, $domain); + $meta->setState('new'); + $this->setMetadata($result, $key, $domain, $meta); + if (null !== $prefixTarget) { + $this->setTarget($catalogue, $key, $prefixTarget, $domain, $translation); + } + + // Add custom translations that we found in the source + if (null === $translation) { + if (null !== $newTranslation = $meta->getTranslation()) { + $result->set($key, $newTranslation, $domain); + // We do not want "translation" key stored anywhere. + $meta->removeAllInCategory('translation'); + } elseif (null !== $newTranslation = $meta->getDesc()) { + $result->set($key, $newTranslation, $domain); + } + } + } + foreach ($merge->getObsoleteMessages($domain) as $key => $translation) { + $meta = $this->getMetadata($result, $key, $domain); + $meta->setState('obsolete'); + $this->setMetadata($result, $key, $domain, $meta); + } + } + $results[] = $result; } - } - $merge = new ReplaceOperation($target, $catalogue); - $result = $merge->getResult(); - $domains = $merge->getDomains(); + return new ImportResult($results, $sourceCollection->getErrors()); + } - // Mark new messages as new/obsolete - foreach ($domains as $domain) - { - foreach ($merge->getNewMessages($domain) as $key => $translation) - { - $meta = $this->getMetadata($result, $key, $domain); - $meta->setState('new'); - $this->setMetadata($result, $key, $domain, $meta); - if (null !== $prefixTarget) - { - $this->setTarget($catalogue, $key, $prefixTarget, $domain, $translation); - } + /** + * @param MessageCatalogue $catalogue + * @param SourceCollection $collection + */ + private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) + { + /** @var SourceLocation $sourceLocation */ + foreach ($collection as $sourceLocation) { + $context = $sourceLocation->getContext(); + $domain = isset($context['domain']) ? $context['domain'] : 'messages'; + // Check with white/black list + if (!$this->isValidDomain($domain)) { + continue; + } - // Add custom translations that we found in the source - if (null === $translation) - { - if (null !== $newTranslation = $meta->getTranslation()) - { - $result->set($key, $newTranslation, $domain); - // We do not want "translation" key stored anywhere. - $meta->removeAllInCategory('translation'); + $key = $sourceLocation->getMessage(); + $catalogue->set($key, null, $domain); + $trimLength = 1 + strlen($this->config['project_root']); + + $meta = $this->getMetadata($catalogue, $key, $domain); + $meta->addCategory('file-source', sprintf('%s:%s', substr($sourceLocation->getPath(), $trimLength), $sourceLocation->getLine())); + if (isset($sourceLocation->getContext()['desc'])) { + $meta->addCategory('desc', $sourceLocation->getContext()['desc']); } - elseif (null !== $newTranslation = $meta->getDesc()) - { - $result->set($key, $newTranslation, $domain); + if (isset($sourceLocation->getContext()['translation'])) { + $meta->addCategory('translation', $sourceLocation->getContext()['translation']); } - } - } - foreach ($merge->getObsoleteMessages($domain) as $key => $translation) - { - $meta = $this->getMetadata($result, $key, $domain); - $meta->setState('obsolete'); - $this->setMetadata($result, $key, $domain, $meta); + $this->setMetadata($catalogue, $key, $domain, $meta); } - } - $results[] = $result; } - return new ImportResult($results, $sourceCollection->getErrors()); - } - - /** - * @param MessageCatalogue $catalogue - * @param SourceCollection $collection - */ - private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) - { - /** @var SourceLocation $sourceLocation */ - foreach ($collection as $sourceLocation) + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $domain + * + * @return Metadata + */ + private function getMetadata(MessageCatalogue $catalogue, $key, $domain) { - $context = $sourceLocation->getContext(); - $domain = isset($context['domain']) ? $context['domain'] : 'messages'; - // Check with white/black list - if (!$this->isValidDomain($domain)) - { - continue; - } - - $key = $sourceLocation->getMessage(); - $catalogue->set($key, null, $domain); - $trimLength = 1 + strlen($this->config['project_root']); - - $meta = $this->getMetadata($catalogue, $key, $domain); - $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); + return new Metadata($catalogue->getMetadata($key, $domain)); } - } - - /** - * @param MessageCatalogue $catalogue - * @param $key - * @param $domain - * - * @return Metadata - */ - private function getMetadata(MessageCatalogue $catalogue, $key, $domain) - { - return new Metadata($catalogue->getMetadata($key, $domain)); - } - - /** - * @param MessageCatalogue $catalogue - * @param $key - * @param $domain - * @param Metadata $metadata - */ - private function setMetadata(MessageCatalogue $catalogue, $key, $domain, Metadata $metadata) - { - $catalogue->setMetadata($key, $metadata->toArray(), $domain); - } /** * @param MessageCatalogue $catalogue @@ -206,70 +178,73 @@ private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); } - /** - * @param string $domain - * - * @return bool - */ - private function isValidDomain($domain) - { - if (!empty($this->config['blacklist_domains']) && in_array($domain, $this->config['blacklist_domains'])) + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $domain + * @param Metadata $metadata + */ + private function setMetadata(MessageCatalogue $catalogue, $key, $domain, Metadata $metadata) { - return false; + $catalogue->setMetadata($key, $metadata->toArray(), $domain); } - if (!empty($this->config['whitelist_domains']) && !in_array($domain, $this->config['whitelist_domains'])) + + /** + * @param string $domain + * + * @return bool + */ + private function isValidDomain($domain) { - return false; + if (!empty($this->config['blacklist_domains']) && in_array($domain, $this->config['blacklist_domains'])) { + return false; + } + if (!empty($this->config['whitelist_domains']) && !in_array($domain, $this->config['whitelist_domains'])) { + return false; + } + + return true; } - return true; - } + /** + * Make sure the configuration is valid. + * + * @param array $config + */ + private function processConfig($config) + { + $default = [ + 'project_root' => '', + 'blacklist_domains' => [], + 'whitelist_domains' => [], + ]; - /** - * Make sure the configuration is valid. - * - * @param array $config - */ - private function processConfig($config) - { - $default = [ - 'project_root' => '', - 'blacklist_domains' => [], - 'whitelist_domains' => [], - ]; + $config = array_merge($default, $config); - $config = array_merge($default, $config); + if (!empty($config['blacklist_domains']) && !empty($config['whitelist_domains'])) { + throw new \InvalidArgumentException('Cannot use "blacklist_domains" and "whitelist_domains" at the same time'); + } - if (!empty($config['blacklist_domains']) && !empty($config['whitelist_domains'])) - { - throw new \InvalidArgumentException('Cannot use "blacklist_domains" and "whitelist_domains" at the same time'); - } + if (!empty($config['blacklist_domains']) && !is_array($config['blacklist_domains'])) { + throw new \InvalidArgumentException('Config parameter "blacklist_domains" must be an array'); + } - if (!empty($config['blacklist_domains']) && !is_array($config['blacklist_domains'])) - { - throw new \InvalidArgumentException('Config parameter "blacklist_domains" must be an array'); - } + if (!empty($config['whitelist_domains']) && !is_array($config['whitelist_domains'])) { + throw new \InvalidArgumentException('Config parameter "whitelist_domains" must be an array'); + } - if (!empty($config['whitelist_domains']) && !is_array($config['whitelist_domains'])) - { - throw new \InvalidArgumentException('Config parameter "whitelist_domains" must be an array'); + $this->config = $config; } - $this->config = $config; - } - - private function disableTwigVisitors() - { - foreach ($this->twig->getNodeVisitors() as $visitor) + private function disableTwigVisitors() { - if ($visitor instanceof DefaultApplyingNodeVisitor) - { - $visitor->setEnabled(false); - } - if ($visitor instanceof RemovingNodeVisitor) - { - $visitor->setEnabled(false); - } + foreach ($this->twig->getNodeVisitors() as $visitor) { + if ($visitor instanceof DefaultApplyingNodeVisitor) { + $visitor->setEnabled(false); + } + if ($visitor instanceof RemovingNodeVisitor) { + $visitor->setEnabled(false); + } + } } - } } From 2c15ad57887e107ffe93ceaca6fe4a3d37349299 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:28:30 +0200 Subject: [PATCH 07/13] fix style --- Service/Importer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Service/Importer.php b/Service/Importer.php index d84f0b60..faf57b98 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -64,7 +64,9 @@ public function __construct(Extractor $extractor, \Twig_Environment $twig) * @var string $project_root The project root will be removed from the source location. * } * @param string|null $prefixTarget + * * @return ImportResult + * */ public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], string $prefixTarget = null) { @@ -97,7 +99,7 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co $meta->setState('new'); $this->setMetadata($result, $key, $domain, $meta); if (null !== $prefixTarget) { - $this->setTarget($catalogue, $key, $prefixTarget, $domain, $translation); + $this->setTarget($catalogue, $key, $prefixTarget, $domain, $translation); } // Add custom translations that we found in the source @@ -175,7 +177,7 @@ private function getMetadata(MessageCatalogue $catalogue, $key, $domain) */ private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') { - $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); + $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); } /** From ae7519ff6881d852a92784e57a4a10432bfeabc8 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:29:44 +0200 Subject: [PATCH 08/13] fix style --- Service/Importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/Importer.php b/Service/Importer.php index faf57b98..8bb3a779 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -63,10 +63,10 @@ public function __construct(Extractor $extractor, \Twig_Environment $twig) * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. * @var string $project_root The project root will be removed from the source location. * } + * * @param string|null $prefixTarget * * @return ImportResult - * */ public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], string $prefixTarget = null) { From e7c53963bc1428f1c4bddcf952ec2cc18f0022f1 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:40:29 +0200 Subject: [PATCH 09/13] fix style --- .idea/codeStyles/codeStyleConfig.xml | 5 ++++ .idea/composerJson.xml | 10 ++++++++ Service/Importer.php | 36 ++++++++++++++-------------- 3 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/composerJson.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..79ee123c --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/composerJson.xml b/.idea/composerJson.xml new file mode 100644 index 00000000..1b074300 --- /dev/null +++ b/.idea/composerJson.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Service/Importer.php b/Service/Importer.php index 8bb3a779..860a62bb 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -45,7 +45,7 @@ final class Importer private $twig; /** - * @param Extractor $extractor + * @param Extractor $extractor * @param \Twig_Environment $twig */ public function __construct(Extractor $extractor, \Twig_Environment $twig) @@ -55,15 +55,15 @@ public function __construct(Extractor $extractor, \Twig_Environment $twig) } /** - * @param Finder $finder + * @param Finder $finder * @param MessageCatalogue[] $catalogues - * @param array $config { + * @param array $config { * - * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. - * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. - * @var string $project_root The project root will be removed from the source location. + * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. + * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. + * @var string $project_root The project root will be removed from the source location. * } - * + * * @param string|null $prefixTarget * * @return ImportResult @@ -168,17 +168,17 @@ private function getMetadata(MessageCatalogue $catalogue, $key, $domain) return new Metadata($catalogue->getMetadata($key, $domain)); } - /** - * @param MessageCatalogue $catalogue - * @param $key - * @param $prefix - * @param $domain - * @param string $translation - */ - private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') - { - $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); - } + /** + * @param MessageCatalogue $catalogue + * @param $key + * @param $prefix + * @param $domain + * @param string $translation + */ + private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') + { + $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); + } /** * @param MessageCatalogue $catalogue From adb8db7d76621db12ad6195c9710f7c02e5a490e Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:41:44 +0200 Subject: [PATCH 10/13] fix style --- .idea/codeStyles/codeStyleConfig.xml | 5 ----- .idea/composerJson.xml | 10 ---------- 2 files changed, 15 deletions(-) delete mode 100644 .idea/codeStyles/codeStyleConfig.xml delete mode 100644 .idea/composerJson.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml deleted file mode 100644 index 79ee123c..00000000 --- a/.idea/codeStyles/codeStyleConfig.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/composerJson.xml b/.idea/composerJson.xml deleted file mode 100644 index 1b074300..00000000 --- a/.idea/composerJson.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file From 24e03a49ee6d9ebfcb2536abe67c3b6179492089 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:45:05 +0200 Subject: [PATCH 11/13] fix style --- Service/Importer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Service/Importer.php b/Service/Importer.php index 860a62bb..2d33f98c 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -45,7 +45,7 @@ final class Importer private $twig; /** - * @param Extractor $extractor + * @param Extractor $extractor * @param \Twig_Environment $twig */ public function __construct(Extractor $extractor, \Twig_Environment $twig) @@ -55,13 +55,13 @@ public function __construct(Extractor $extractor, \Twig_Environment $twig) } /** - * @param Finder $finder + * @param Finder $finder * @param MessageCatalogue[] $catalogues - * @param array $config { + * @param array $config { * - * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. - * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. - * @var string $project_root The project root will be removed from the source location. + * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. + * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. + * @var string $project_root The project root will be removed from the source location. * } * * @param string|null $prefixTarget From 849ae59d641a15e48f3e328f8b2b2c6e5ed03bc2 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:45:59 +0200 Subject: [PATCH 12/13] fix style --- Service/Importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/Importer.php b/Service/Importer.php index 2d33f98c..84339a1e 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -177,7 +177,7 @@ private function getMetadata(MessageCatalogue $catalogue, $key, $domain) */ private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') { - $catalogue->set($key, $translation !== '' ? $translation : $prefix . $key, $domain); + $catalogue->set($key, '' !== $translation ? $translation : $prefix . $key, $domain); } /** From d5e25ba9b712578466f82b800b4a4da5157c3a52 Mon Sep 17 00:00:00 2001 From: Filinto Romain Date: Mon, 11 Jun 2018 09:46:43 +0200 Subject: [PATCH 13/13] fix style --- Service/Importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/Importer.php b/Service/Importer.php index 84339a1e..a89ee245 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -177,7 +177,7 @@ private function getMetadata(MessageCatalogue $catalogue, $key, $domain) */ private function setTarget(MessageCatalogue $catalogue, $key, $prefix, $domain, $translation = '') { - $catalogue->set($key, '' !== $translation ? $translation : $prefix . $key, $domain); + $catalogue->set($key, '' !== $translation ? $translation : $prefix.$key, $domain); } /**