Skip to content
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

Added option to keep initial translated messages #370

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions Command/ExtractTranslationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).')
;
}

Expand Down Expand Up @@ -89,6 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln(sprintf('Extracting Translations for locale <info>%s</info>', $locale));
$output->writeln(sprintf('Keep old translations: <info>%s</info>', $config->isKeepOldMessages() ? 'Yes' : 'No'));
$output->writeln(sprintf('Keep old translation messages: <info>%s</info>', $config->isKeepOldTranslationMessages() ? 'Yes' : 'No'));
$output->writeln(sprintf('Output-Path: <info>%s</info>', $config->getTranslationsDir()));
$output->writeln(sprintf('Directories: <info>%s</info>', implode(', ', $config->getScanDirs())));
$output->writeln(sprintf('Excluded Directories: <info>%s</info>', $config->getExcludedDirs() ? implode(', ', $config->getExcludedDirs()) : '# none #'));
Expand Down Expand Up @@ -125,6 +127,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($config->isKeepOldTranslationMessages()) {
$output->writeln('Not keeping old Translation Messages');
}

return;
}

Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<argument type="service" id="jms_translation.extractor_manager" />
<argument type="service" id="logger" />
<argument type="service" id="jms_translation.file_writer" />
<argument type="service" id="translator.default" />
</service>

<service id="jms_translation.config_factory" class="%jms_translation.config_factory.class%" />
Expand Down
6 changes: 5 additions & 1 deletion Resources/doc/cookbook/config_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
3 changes: 2 additions & 1 deletion Tests/Functional/Command/ExtractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 16 additions & 1 deletion Translation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ final class Config
*/
private $keepOldMessages;

/**
* @var bool
*/
private $keepOldTranslationMessages;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor nitpick, this variable should be named keepOldTranslationMessages (without the s on Translation)

/**
* @var array
*/
Expand All @@ -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.');
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -261,6 +268,14 @@ public function isKeepOldMessages()
return $this->keepOldMessages;
}

/**
* @return bool
*/
public function isKeepOldTranslationMessages()
{
return $this->keepOldTranslationMessages;
}

/**
* @return array
*/
Expand Down
17 changes: 17 additions & 0 deletions Translation/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ final class ConfigBuilder
*/
private $keepOldTranslations = false;

/**
* @var bool
*/
private $keepOldTranslationMessages = false;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here (keepOldTranslationMessages)

/**
* @var array
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -302,6 +318,7 @@ public function getConfig()
$this->excludedNames,
$this->enabledExtractors,
$this->keepOldTranslations,
$this->keepOldTranslationMessages,
$this->loadResources
);
}
Expand Down
27 changes: 26 additions & 1 deletion Translation/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this seems odd to me that you are translating as you go. If all you want to do is keep any messages that were already there, why is this necessary?

$message->setNew(false);
}
}
}

}
}