Skip to content
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
52 changes: 36 additions & 16 deletions Command/ExtractTranslationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ protected function configure()
->addOption('output-format', null, InputOption::VALUE_REQUIRED, 'The output format that should be used (in most cases, it is better to change only the default-output-format).')
->addOption('default-output-format', null, InputOption::VALUE_REQUIRED, 'The default output format (defaults to xliff).')
->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 ressources')
;
->addOption('keeptm', null, InputOption::VALUE_NONE, 'Define if the updater service should keep the old translation messages (defaults to false).')
->addOption('external-translations-dir', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Load external translation ressources');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$builder = $input->getOption('config') ?
$this->getContainer()->get('jms_translation.config_factory')->getBuilder($input->getOption('config'))
: new ConfigBuilder();
$this->getContainer()->get('jms_translation.config_factory')->getBuilder($input->getOption('config'))
: new ConfigBuilder();

$this->updateWithInput($input, $builder);

Expand All @@ -81,11 +81,12 @@ 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 translations messages: <info>%s</info>', $config->isKeepOldTranslationsMessages() ? '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 #'));
$output->writeln(sprintf('Excluded Names: <info>%s</info>', $config->getExcludedNames() ? implode(', ', $config->getExcludedNames()) : '# none #'));
$output->writeln(sprintf('Output-Format: <info>%s</info>', $config->getOutputFormat() ? $config->getOutputFormat() : '# whatever is present, if nothing then '.$config->getDefaultOutputFormat().' #'));
$output->writeln(sprintf('Output-Format: <info>%s</info>', $config->getOutputFormat() ? $config->getOutputFormat() : '# whatever is present, if nothing then ' . $config->getDefaultOutputFormat() . ' #'));
$output->writeln(sprintf('Custom Extractors: <info>%s</info>', $config->getEnabledExtractors() ? implode(', ', array_keys($config->getEnabledExtractors())) : '# none #'));
$output->writeln('============================================================');

Expand All @@ -99,24 +100,36 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('dry-run')) {
$changeSet = $updater->getChangeSet($config);

$output->writeln('Added Messages: '.count($changeSet->getAddedMessages()));
if($input->hasParameterOption('--verbose')){
foreach($changeSet->getAddedMessages() as $message){
$output->writeln($message->getId(). '-> '.$message->getDesc());
}
$output->writeln('Added Messages: ' . count($changeSet->getAddedMessages()));
if ($input->hasParameterOption('--verbose')) {
foreach ($changeSet->getAddedMessages() as $message) {
$output->writeln($message->getId() . '-> ' . $message->getDesc());
}
}

if ($config->isKeepOldMessages()) {
$output->writeln('Deleted Messages: # none as "Keep Old Translations" is true #');
} else {
$output->writeln('Deleted Messages: '.count($changeSet->getDeletedMessages()));
if($input->hasParameterOption('--verbose')){
foreach($changeSet->getDeletedMessages() as $message){
$output->writeln($message->getId(). '-> '.$message->getDesc());
}
$output->writeln('Deleted Messages: ' . count($changeSet->getDeletedMessages()));
if ($input->hasParameterOption('--verbose')) {
foreach ($changeSet->getDeletedMessages() as $message) {
$output->writeln($message->getId() . '-> ' . $message->getDesc());
}
}
}

if ($config->isKeepOldTranslationsMessages()) {
$output->writeln('Not keeping old Translations Messages');
} else {
//todog
/* $output->writeln('Deleted Messages: ' . count($changeSet->getDeletedMessages()));
if ($input->hasParameterOption('--verbose')) {
foreach ($changeSet->getDeletedMessages() as $message) {
$output->writeln($message->getId() . '-> ' . $message->getDesc());
}
}*/
}

return;
}

Expand All @@ -134,7 +147,7 @@ private function updateWithInput(InputInterface $input, ConfigBuilder $builder)
}

$bundle = $this->getApplication()->getKernel()->getBundle($bundle);
$builder->setTranslationsDir($bundle->getPath().'/Resources/translations');
$builder->setTranslationsDir($bundle->getPath() . '/Resources/translations');
$builder->setScanDirs(array($bundle->getPath()));
}

Expand Down Expand Up @@ -192,6 +205,13 @@ private function updateWithInput(InputInterface $input, ConfigBuilder $builder)
$builder->setKeepOldTranslations(false);
}


if ($input->hasParameterOption('--keeptm') || $input->hasParameterOption('--keeptm=true')) {
$builder->setKeepOldTranslationsMessages(true);
} else if ($input->hasParameterOption('--keeptm=false')) {
$builder->setKeepOldTranslationsMessages(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
1 change: 1 addition & 0 deletions Tests/Functional/ExtractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testExtract()
$expectedOutput =
'Extracting Translations for locale en'."\n"
.'Keep old translations: No'."\n"
.'Keep old translations messages: No'."\n"
.'Output-Path: '.$outputDir."\n"
.'Directories: '.$inputDir."\n"
.'Excluded Directories: Tests'."\n"
Expand Down
13 changes: 12 additions & 1 deletion Translation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ final class Config
private $enabledExtractors;

private $keepOldMessages;
private $keepOldTranslationsMessages;
private $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, $keepOldTranslationsMessages, array $loadResources)
{
if (empty($translationsDir)) {
throw new InvalidArgumentException('The directory where translations are must be set.');
Expand Down Expand Up @@ -84,6 +85,7 @@ public function __construct($translationsDir, $locale, array $ignoredDomains, ar
$this->excludedNames = $excludedNames;
$this->enabledExtractors = $enabledExtractors;
$this->keepOldMessages = $keepOldMessages;
$this->keepOldTranslationsMessages = $keepOldTranslationsMessages;
$this->loadResources = $loadResources;
}

Expand Down Expand Up @@ -201,6 +203,15 @@ public function isKeepOldMessages()
return $this->keepOldMessages;
}

/**
* @return Boolean
*/
public function isKeepOldTranslationsMessages()
{
return $this->keepOldTranslationsMessages;
}


/**
* @return array
*/
Expand Down
10 changes: 10 additions & 0 deletions Translation/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class ConfigBuilder
private $excludedNames = array('*Test.php', '*TestCase.php');
private $enabledExtractors = array();
private $keepOldTranslations = false;
private $keepOldTranslationsMessages = false;
private $loadResources = array();

/**
Expand Down Expand Up @@ -188,6 +189,14 @@ public function setKeepOldTranslations($value)
return $this;
}


public function setKeepOldTranslationsMessages($value)
{
$this->keepOldTranslationsMessages = $value;

return $this;
}

public function getConfig()
{
return new Config(
Expand All @@ -202,6 +211,7 @@ public function getConfig()
$this->excludedNames,
$this->enabledExtractors,
$this->keepOldTranslations,
$this->keepOldTranslationsMessages,
$this->loadResources
);
}
Expand Down
35 changes: 28 additions & 7 deletions Translation/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\Comparison\CatalogueComparator;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\Translation\MessageCatalogue as SymfonyMessageCatalogue;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
Expand Down Expand Up @@ -52,19 +54,21 @@ class Updater
private $scannedCatalogue;
private $logger;
private $writer;
private $translator;

/**
* @param LoaderManager $loader
* @param ExtractorManager $extractor
* @param \Symfony\Component\HttpKernel\Log\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 @@ -105,8 +109,7 @@ public function updateTranslation($file, $format, $domain, $locale, $id, $trans)
$catalogue
->get($id, $domain)
->setLocaleString($trans)
->setNew(false)
;
->setNew(false);

$this->writer->write($catalogue, $domain, $file, $format);
}
Expand Down Expand Up @@ -136,8 +139,8 @@ public function process(Config $config)
$format = $this->detectOutputFormat($name);

// delete translation files of other formats
foreach (Finder::create()->name('/^'.$name.'\.'.$this->config->getLocale().'\.[^\.]+$/')->in($this->config->getTranslationsDir())->depth('< 1')->files() as $file) {
if ('.'.$format === substr($file, -1 * strlen('.'.$format))) {
foreach (Finder::create()->name('/^' . $name . '\.' . $this->config->getLocale() . '\.[^\.]+$/')->in($this->config->getTranslationsDir())->depth('< 1')->files() as $file) {
if ('.' . $format === substr($file, -1 * strlen('.' . $format))) {
continue;
}

Expand All @@ -148,7 +151,7 @@ public function process(Config $config)
}
}

$outputFile = $this->config->getTranslationsDir().'/'.$name.'.'.$this->config->getLocale().'.'.$format;
$outputFile = $this->config->getTranslationsDir() . '/' . $name . '.' . $this->config->getLocale() . '.' . $format;
$this->logger->info(sprintf('Writing translation file "%s".', $outputFile));
$this->writer->write($this->scannedCatalogue, $name, $outputFile, $format);
}
Expand All @@ -171,7 +174,7 @@ private function detectOutputFormat($currentDomain)
$otherDomainFormat = $localeFormat = $otherLocaleFormat = null;
foreach (FileUtils::findTranslationFiles($this->config->getTranslationsDir()) as $domain => $locales) {
foreach ($locales as $locale => $fileData) {
list($format, ) = $fileData;
list($format,) = $fileData;

if ($currentDomain !== $domain) {
$otherDomainFormat = $format;
Expand Down Expand Up @@ -245,6 +248,7 @@ private function setConfig(Config $config)
}
}

//keep old translations
if ($this->config->isKeepOldMessages()) {
foreach ($this->existingCatalogue->getDomains() as $domainCatalogue) {
foreach ($domainCatalogue->all() as $message) {
Expand All @@ -256,5 +260,22 @@ private function setConfig(Config $config)
}
}
}

//keep old translations translated
if ($this->config->isKeepOldTranslationsMessages()) {

$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);
}
}
}
}
}