diff --git a/Command/ExtractTranslationCommand.php b/Command/ExtractTranslationCommand.php index 3f871ad8..d0a7ee56 100644 --- a/Command/ExtractTranslationCommand.php +++ b/Command/ExtractTranslationCommand.php @@ -59,6 +59,7 @@ protected function configure() ->addOption('default-output-format', null, InputOption::VALUE_REQUIRED, 'The default output format (defaults to xlf).') ->addOption('keep', null, InputOption::VALUE_NONE, 'Define if the updater service should keep the old translation (defaults to false).') ->addOption('external-translations-dir', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Load external translation resources') + ->addOption('keeptm', null, InputOption::VALUE_NONE, 'Define if the updater service should keep the old translation messages (defaults to false).') ; } @@ -89,6 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(sprintf('Extracting Translations for locale %s', $locale)); $output->writeln(sprintf('Keep old translations: %s', $config->isKeepOldMessages() ? 'Yes' : 'No')); + $output->writeln(sprintf('Keep old translation messages: %s', $config->isKeepOldTranslationMessages() ? 'Yes' : 'No')); $output->writeln(sprintf('Output-Path: %s', $config->getTranslationsDir())); $output->writeln(sprintf('Directories: %s', implode(', ', $config->getScanDirs()))); $output->writeln(sprintf('Excluded Directories: %s', $config->getExcludedDirs() ? implode(', ', $config->getExcludedDirs()) : '# none #')); @@ -125,6 +127,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } } + if ($config->isKeepOldTranslationMessages()) { + $output->writeln('Not keeping old Translation Messages'); + } + return; } @@ -204,6 +210,12 @@ private function updateWithInput(InputInterface $input, ConfigBuilder $builder) $builder->setKeepOldTranslations(false); } + if ($input->hasParameterOption('--keeptm') || $input->hasParameterOption('--keeptm=true')) { + $builder->setKeepOldTranslationMessages(true); + } else if ($input->hasParameterOption('--keeptm=false')) { + $builder->setKeepOldTranslationMessages(false); + } + if ($loadResource = $input->getOption('external-translations-dir')) { $builder->setLoadResources($loadResource); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index da663552..355db336 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -38,6 +38,7 @@ + diff --git a/Resources/doc/cookbook/config_reference.rst b/Resources/doc/cookbook/config_reference.rst index 8abaceab..230e6dce 100644 --- a/Resources/doc/cookbook/config_reference.rst +++ b/Resources/doc/cookbook/config_reference.rst @@ -46,4 +46,8 @@ On this page you will find all available configuration options and their meaning # If true, we will never remove messages from the translation files. # If false, the translation files are up to date with the source. - keep: false \ No newline at end of file + keep: false + + # If true, new messages (keys with no values) will get their values from being translated by the translator, instead of the key value + # If false, new messages will be set the same as the key + keeptm: false diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index 469a9528..dac9a7f7 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -37,7 +37,8 @@ public function testExtract() $expectedOutput = 'Extracting Translations for locale en'."\n" - .'Keep old translations: No'."\n" + .'Keep old translations: No'."\n" + .'Keep old translation messages: No'."\n" .'Output-Path: '.$outputDir."\n" .'Directories: '.$inputDir."\n" .'Excluded Directories: Tests'."\n" diff --git a/Translation/Config.php b/Translation/Config.php index 0cf5d26b..a7dee790 100644 --- a/Translation/Config.php +++ b/Translation/Config.php @@ -85,6 +85,11 @@ final class Config */ private $keepOldMessages; + /** + * @var bool + */ + private $keepOldTranslationMessages; + /** * @var array */ @@ -103,9 +108,10 @@ final class Config * @param array $excludedNames * @param array $enabledExtractors * @param bool $keepOldMessages + * @param bool $keepOldTranslationMessages * @param array $loadResources */ - public function __construct($translationsDir, $locale, array $ignoredDomains, array $domains, $outputFormat, $defaultOutputFormat, array $scanDirs, array $excludedDirs, array $excludedNames, array $enabledExtractors, $keepOldMessages, array $loadResources) + public function __construct($translationsDir, $locale, array $ignoredDomains, array $domains, $outputFormat, $defaultOutputFormat, array $scanDirs, array $excludedDirs, array $excludedNames, array $enabledExtractors, $keepOldMessages, $keepOldTranslationMessages, array $loadResources) { if (empty($translationsDir)) { throw new InvalidArgumentException('The directory where translations are must be set.'); @@ -144,6 +150,7 @@ public function __construct($translationsDir, $locale, array $ignoredDomains, ar $this->excludedNames = $excludedNames; $this->enabledExtractors = $enabledExtractors; $this->keepOldMessages = $keepOldMessages; + $this->keepOldTranslationMessages = $keepOldTranslationMessages; $this->loadResources = $loadResources; } @@ -261,6 +268,14 @@ public function isKeepOldMessages() return $this->keepOldMessages; } + /** + * @return bool + */ + public function isKeepOldTranslationMessages() + { + return $this->keepOldTranslationMessages; + } + /** * @return array */ diff --git a/Translation/ConfigBuilder.php b/Translation/ConfigBuilder.php index 9839bd24..7db1525c 100644 --- a/Translation/ConfigBuilder.php +++ b/Translation/ConfigBuilder.php @@ -75,6 +75,11 @@ final class ConfigBuilder */ private $keepOldTranslations = false; + /** + * @var bool + */ + private $keepOldTranslationMessages = false; + /** * @var array */ @@ -285,6 +290,17 @@ public function setKeepOldTranslations($value) return $this; } + /** + * @param bool $value + * @return $this + */ + public function setKeepOldTranslationMessages($value) + { + $this->keepOldTranslationMessages = $value; + + return $this; + } + /** * @return Config */ @@ -302,6 +318,7 @@ public function getConfig() $this->excludedNames, $this->enabledExtractors, $this->keepOldTranslations, + $this->keepOldTranslationMessages, $this->loadResources ); } diff --git a/Translation/Updater.php b/Translation/Updater.php index 9f79d6e6..9c8651d3 100644 --- a/Translation/Updater.php +++ b/Translation/Updater.php @@ -24,6 +24,7 @@ use JMS\TranslationBundle\Model\MessageCatalogue; use JMS\TranslationBundle\Translation\Comparison\CatalogueComparator; use Psr\Log\LoggerInterface; +use Symfony\Bundle\FrameworkBundle\Translation\Translator; use Symfony\Component\Finder\Finder; /** @@ -72,18 +73,24 @@ class Updater */ private $writer; + /** + * @var Translator + */ + private $translator; + /** * @param LoaderManager $loader * @param ExtractorManager $extractor * @param LoggerInterface $logger * @param FileWriter $writer */ - public function __construct(LoaderManager $loader, ExtractorManager $extractor, LoggerInterface $logger, FileWriter $writer) + public function __construct(LoaderManager $loader, ExtractorManager $extractor, LoggerInterface $logger, FileWriter $writer, Translator $translator) { $this->loader = $loader; $this->extractor = $extractor; $this->logger = $logger; $this->writer = $writer; + $this->translator = $translator; } /** @@ -277,5 +284,23 @@ private function setConfig(Config $config) } } } + + //keep old translations translated + if ($this->config->isKeepOldTranslationMessages()) { + + $locale = $this->scannedCatalogue->getLocale(); + /** @var MessageCatalogue $domainCatalogue */ + foreach ($this->scannedCatalogue->getDomains() as $domainCatalogue) { + + /** @var Message $message */ + foreach ($domainCatalogue->all() as $message) { + + $translated = $this->translator->trans($message->getId(), array(), $message->getDomain(), $locale); + $message->setLocaleString($translated); + $message->setNew(false); + } + } + } + } }