Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] adds --clean option translation:update command #7331

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

Merged
merged 1 commit into from
Mar 18, 2013
Merged
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
[FrameworkBundle] adds --clean option translation:update command
[FrameworkBundle] moved operation classes to component

[FrameworkBundle] fixed catalogue operation classes

[Translation] renamed catalogue operation classes

[Translation] renamed operation classes
  • Loading branch information
jfsimon committed Mar 18, 2013
commit 57aa3d51a79db9c7dda2683fa74f3f25cfca5265
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Translation\Catalogue\DiffOperation;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -26,12 +28,6 @@
*/
class TranslationUpdateCommand extends ContainerAwareCommand
{
/**
* Compiled catalogue of messages.
* @var MessageCatalogue
*/
protected $catalogue;

/**
* {@inheritDoc}
*/
Expand All @@ -57,6 +53,10 @@ protected function configure()
new InputOption(
'force', null, InputOption::VALUE_NONE,
'Should the update be done'
),
new InputOption(
'clean', null, InputOption::VALUE_NONE,
'Should clean not found messages'
)
))
->setDescription('Updates the translation file')
Expand Down Expand Up @@ -100,26 +100,41 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundleTransPath = $foundBundle->getPath().'/Resources/translations';
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));

// create catalogue
$catalogue = new MessageCatalogue($input->getArgument('locale'));

// load any messages from templates
$extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
$output->writeln('Parsing templates');
$extractor = $this->getContainer()->get('translation.extractor');
$extractor->setPrefix($input->getOption('prefix'));
$extractor->extract($foundBundle->getPath().'/Resources/views/', $catalogue);
$extractor->extract($foundBundle->getPath().'/Resources/views/', $extractedCatalogue);

// load any existing messages from the translation files
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
$output->writeln('Loading translation files');
$loader = $this->getContainer()->get('translation.loader');
$loader->loadMessages($bundleTransPath, $catalogue);
$loader->loadMessages($bundleTransPath, $currentCatalogue);

// process catalogues
$operation = $input->getOption('clean')
? new DiffOperation($currentCatalogue, $extractedCatalogue)
: new MergeOperation($currentCatalogue, $extractedCatalogue);

// show compiled list of messages
if ($input->getOption('dump-messages') === true) {
foreach ($catalogue->getDomains() as $domain) {
foreach ($operation->getDomains() as $domain) {
$output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
$output->writeln(Yaml::dump($catalogue->all($domain), 10));
$newKeys = array_keys($operation->getNewMessages($domain));
$allKeys = array_keys($operation->getMessages($domain));
foreach (array_diff($allKeys, $newKeys) as $id) {
$output->writeln($id);
}
foreach ($newKeys as $id) {
$output->writeln(sprintf('<fg=green>%s</>', $id));
}
foreach (array_keys($operation->getObsoleteMessages($domain)) as $id) {
$output->writeln(sprintf('<fg=red>%s</>', $id));
}
}

if ($input->getOption('output-format') == 'xliff') {
$output->writeln('Xliff output version is <info>1.2</info>');
}
Expand All @@ -128,7 +143,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// save the files
if ($input->getOption('force') === true) {
$output->writeln('Writing files');
$writer->writeTranslations($catalogue, $input->getOption('output-format'), array('path' => $bundleTransPath));
$writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath));
}
}
}
146 changes: 146 additions & 0 deletions src/Symfony/Component/Translation/Catalogue/AbstractOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Catalogue;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;

/**
* Base catalogues binary operation class.
*
* @author Jean-François Simon <[email protected]>
*/
abstract class AbstractOperation implements OperationInterface
{
/**
* @var MessageCatalogueInterface
*/
protected $source;

/**
* @var MessageCatalogueInterface
*/
protected $target;

/**
* @var MessageCatalogue
*/
protected $result;

/**
* @var null|array
*/
private $domains;

/**
* @var array
*/
protected $messages;

/**
* @param MessageCatalogueInterface $source
* @param MessageCatalogueInterface $target
*
* @throws \LogicException
*/
public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
{
if ($source->getLocale() !== $target->getLocale()) {
throw new \LogicException('Operated catalogues must belong to the same locale.');
}

$this->source = $source;
$this->target = $target;
$this->result = new MessageCatalogue($source->getLocale());
$this->domains = null;
$this->messages = array();
}

/**
* {@inheritdoc}
*/
public function getDomains()
{
if (null === $this->domains) {
$this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
}

return $this->domains;
}

/**
* {@inheritdoc}
*/
public function getMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException('Invalid domain: '.$domain.'.');
}

if (!isset($this->messages[$domain]['all'])) {
$this->processDomain($domain);
}

return $this->messages[$domain]['all'];
}

/**
* {@inheritdoc}
*/
public function getNewMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException('Invalid domain: '.$domain.'.');
}

if (!isset($this->messages[$domain]['new'])) {
$this->processDomain($domain);
}

return $this->messages[$domain]['new'];
}

/**
* {@inheritdoc}
*/
public function getObsoleteMessages($domain)
{
if (!in_array($domain, $this->getDomains())) {
throw new \InvalidArgumentException('Invalid domain: '.$domain.'.');
}

if (!isset($this->messages[$domain]['obsolete'])) {
$this->processDomain($domain);
}

return $this->messages[$domain]['obsolete'];
}

/**
* {@inheritdoc}
*/
public function getResult()
{
foreach ($this->getDomains() as $domain) {
if (!isset($this->messages[$domain])) {
$this->processDomain($domain);
}
}

return $this->result;
}

/**
* @param string $domain
*/
abstract protected function processDomain($domain);
}
49 changes: 49 additions & 0 deletions src/Symfony/Component/Translation/Catalogue/DiffOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Catalogue;

/**
* Diff operation between two catalogues.
*
* @author Jean-François Simon <[email protected]>
*/
class DiffOperation extends AbstractOperation
{
/**
* {@inheritdoc}
*/
protected function processDomain($domain)
{
$this->messages[$domain] = array(
'all' => array(),
'new' => array(),
'obsolete' => array(),
);

foreach ($this->source->all($domain) as $id => $message) {
if ($this->target->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->result->add(array($id => $message), $domain);
} else {
$this->messages[$domain]['obsolete'][$id] = $message;
}
}

foreach ($this->target->all($domain) as $id => $message) {
if (!$this->source->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->messages[$domain]['new'][$id] = $message;
$this->result->add(array($id => $message), $domain);
}
}
}
}
45 changes: 45 additions & 0 deletions src/Symfony/Component/Translation/Catalogue/MergeOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Catalogue;

/**
* Merge operation between two catalogues.
*
* @author Jean-François Simon <[email protected]>
*/
class MergeOperation extends AbstractOperation
{
/**
* {@inheritdoc}
*/
protected function processDomain($domain)
{
$this->messages[$domain] = array(
'all' => array(),
'new' => array(),
'obsolete' => array(),
);

foreach ($this->source->all($domain) as $id => $message) {
$this->messages[$domain]['all'][$id] = $message;
$this->result->add(array($id => $message), $domain);
}

foreach ($this->target->all($domain) as $id => $message) {
if (!$this->source->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->messages[$domain]['new'][$id] = $message;
$this->result->add(array($id => $message), $domain);
}
}
}
}
63 changes: 63 additions & 0 deletions src/Symfony/Component/Translation/Catalogue/OperationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Catalogue;

use Symfony\Component\Translation\MessageCatalogueInterface;

/**
* Represents an operation on catalogue(s).
*
* @author Jean-François Simon <[email protected]>
*/
interface OperationInterface
{
/**
* Returns domains affected by operation.
*
* @return array
*/
public function getDomains();

/**
* Returns all valid messages after operation.
*
* @param string $domain
*
* @return array
*/
public function getMessages($domain);

/**
* Returns new messages after operation.
*
* @param string $domain
*
* @return array
*/
public function getNewMessages($domain);

/**
* Returns obsolete messages after operation.
*
* @param string $domain
*
* @return array
*/
public function getObsoleteMessages($domain);

/**
* Returns resulting catalogue.
*
* @return MessageCatalogueInterface
*/
public function getResult();
}
Loading