2011-08-29 18:22:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Command;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
2013-03-11 12:57:32 +00:00
|
|
|
use Symfony\Component\Translation\Catalogue\DiffOperation;
|
|
|
|
use Symfony\Component\Translation\Catalogue\MergeOperation;
|
2011-08-29 18:22:52 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Translation\MessageCatalogue;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A command that parse templates to extract translation messages and add them into the translation files.
|
2011-09-19 13:47:33 +01:00
|
|
|
*
|
2011-08-29 18:22:52 +01:00
|
|
|
* @author Michel Salib <michelsalib@hotmail.com>
|
|
|
|
*/
|
|
|
|
class TranslationUpdateCommand extends ContainerAwareCommand
|
|
|
|
{
|
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('translation:update')
|
|
|
|
->setDefinition(array(
|
|
|
|
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
|
|
|
|
new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle where to load the messages'),
|
|
|
|
new InputOption(
|
|
|
|
'prefix', null, InputOption::VALUE_OPTIONAL,
|
|
|
|
'Override the default prefix', '__'
|
|
|
|
),
|
|
|
|
new InputOption(
|
|
|
|
'output-format', null, InputOption::VALUE_OPTIONAL,
|
|
|
|
'Override the default output format', 'yml'
|
|
|
|
),
|
|
|
|
new InputOption(
|
|
|
|
'dump-messages', null, InputOption::VALUE_NONE,
|
|
|
|
'Should the messages be dumped in the console'
|
|
|
|
),
|
|
|
|
new InputOption(
|
|
|
|
'force', null, InputOption::VALUE_NONE,
|
|
|
|
'Should the update be done'
|
2013-03-11 12:57:32 +00:00
|
|
|
),
|
|
|
|
new InputOption(
|
|
|
|
'clean', null, InputOption::VALUE_NONE,
|
|
|
|
'Should clean not found messages'
|
2011-08-29 18:22:52 +01:00
|
|
|
)
|
|
|
|
))
|
2012-04-11 07:57:57 +01:00
|
|
|
->setDescription('Updates the translation file')
|
2011-08-29 18:22:52 +01:00
|
|
|
->setHelp(<<<EOF
|
2012-02-12 15:37:55 +00:00
|
|
|
The <info>%command.name%</info> command extract translation strings from templates
|
2011-08-29 18:22:52 +01:00
|
|
|
of a given bundle. It can display them or merge the new ones into the translation files.
|
|
|
|
When new translation strings are found it can automatically add a prefix to the translation
|
|
|
|
message.
|
|
|
|
|
2012-02-12 15:37:55 +00:00
|
|
|
<info>php %command.full_name% --dump-messages en AcmeBundle</info>
|
|
|
|
<info>php %command.full_name% --force --prefix="new_" fr AcmeBundle</info>
|
2011-08-29 18:22:52 +01:00
|
|
|
EOF
|
2012-02-12 15:37:55 +00:00
|
|
|
)
|
|
|
|
;
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
// check presence of force or dump-message
|
|
|
|
if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) {
|
|
|
|
$output->writeln('<info>You must choose one of --force or --dump-messages</info>');
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2012-10-14 10:21:53 +01:00
|
|
|
return 1;
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
2011-09-19 13:47:33 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
// check format
|
|
|
|
$writer = $this->getContainer()->get('translation.writer');
|
|
|
|
$supportedFormats = $writer->getFormats();
|
|
|
|
if (!in_array($input->getOption('output-format'), $supportedFormats)) {
|
|
|
|
$output->writeln('<error>Wrong output format</error>');
|
|
|
|
$output->writeln('Supported formats are '.implode(', ', $supportedFormats).'.');
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2012-10-14 10:21:53 +01:00
|
|
|
return 1;
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get bundle directory
|
|
|
|
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
|
|
|
|
$bundleTransPath = $foundBundle->getPath().'/Resources/translations';
|
|
|
|
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));
|
|
|
|
|
|
|
|
// load any messages from templates
|
2013-03-11 12:57:32 +00:00
|
|
|
$extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
|
2011-08-29 18:22:52 +01:00
|
|
|
$output->writeln('Parsing templates');
|
|
|
|
$extractor = $this->getContainer()->get('translation.extractor');
|
|
|
|
$extractor->setPrefix($input->getOption('prefix'));
|
2013-03-11 12:57:32 +00:00
|
|
|
$extractor->extract($foundBundle->getPath().'/Resources/views/', $extractedCatalogue);
|
2011-09-19 13:47:33 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
// load any existing messages from the translation files
|
2013-03-11 12:57:32 +00:00
|
|
|
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
|
2011-08-29 18:22:52 +01:00
|
|
|
$output->writeln('Loading translation files');
|
|
|
|
$loader = $this->getContainer()->get('translation.loader');
|
2013-03-11 12:57:32 +00:00
|
|
|
$loader->loadMessages($bundleTransPath, $currentCatalogue);
|
|
|
|
|
|
|
|
// process catalogues
|
|
|
|
$operation = $input->getOption('clean')
|
|
|
|
? new DiffOperation($currentCatalogue, $extractedCatalogue)
|
|
|
|
: new MergeOperation($currentCatalogue, $extractedCatalogue);
|
2011-09-19 13:47:33 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
// show compiled list of messages
|
2012-05-03 17:14:01 +01:00
|
|
|
if ($input->getOption('dump-messages') === true) {
|
2013-03-11 12:57:32 +00:00
|
|
|
foreach ($operation->getDomains() as $domain) {
|
2011-08-29 18:22:52 +01:00
|
|
|
$output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
|
2013-03-11 12:57:32 +00:00
|
|
|
$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));
|
|
|
|
}
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
2013-03-11 12:57:32 +00:00
|
|
|
|
2012-05-03 17:14:01 +01:00
|
|
|
if ($input->getOption('output-format') == 'xliff') {
|
2012-05-13 14:37:31 +01:00
|
|
|
$output->writeln('Xliff output version is <info>1.2</info>');
|
2012-05-03 17:14:01 +01:00
|
|
|
}
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the files
|
2012-05-03 17:14:01 +01:00
|
|
|
if ($input->getOption('force') === true) {
|
2011-08-29 18:22:52 +01:00
|
|
|
$output->writeln('Writing files');
|
2013-03-11 12:57:32 +00:00
|
|
|
$writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath));
|
2011-08-29 18:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|