8000 [2.1] Extracting translation messages from templates by michelsalib · Pull Request #2051 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.1] Extracting translation messages from templates #2051

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
Sep 9, 2011
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
147 changes: 147 additions & 0 deletions src/Symfony/Bridge/Twig/Translation/TwigExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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\Bridge\Twig\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Bridge\Twig\Node\TransNode;

/**
* TwigExtractor extracts translation messages from a twig template.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class TwigExtractor implements ExtractorInterface
{
/**
* Default domain for found messages.
*
* @var string
*/
private $defaultDomain = '';

/**
* Prefix for found message.
*
* @var string
*/
private $prefix = '';

/**
* The twig environment.
* @var \Twig_Environment
*/
private $twig;

public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig;
}

/**
* {@inheritDoc}
*/
public function extract($directory, MessageCatalogue $catalogue)
{
// load any existing translation files
$finder = new Finder();
$files = $finder->files()->name('*.twig')->in($directory);
foreach ($files as $file) {
$tree = $this->twig->parse($this->twig->tokenize(file_get_contents($file->getPathname())));
$this->crawlNode($tree, $catalogue);
}
}

/**
* {@inheritDoc}
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}

/**
* Extracts trans message from a twig tree.
*
* @param \Twig_Node $node The twig tree root
* @param MessageCatalogue $catalogue The catalogue
*/
private function crawlNode(\Twig_Node $node, MessageCatalogue $catalogue)
{
if ($node instanceof TransNode && !$node->getNode('body') instanceof \Twig_Node_Expression_GetAttr) {
// trans block
$message = $node->getNode('body')->getAttribute('data');
$domain = $node->getNode('domain')->getAttribute('value');
$catalogue->set($message, $this->prefix.$message, $domain);
} elseif ($node instanceof \Twig_Node_Print) {
// trans filter (be carefull of how you chain your filters)
$message = $this->extractMessage($node->getNode('expr'));
$domain = $this->extractDomain($node->getNode('expr'));
if ($message !== null && $domain !== null) {
$catalogue->set($message, $this->prefix.$message, $domain);
}
} else {
// continue crawling
foreach ($node as $child) {
if ($child != null) {
$this->crawlNode($child, $catalogue);
}
}
}
}

/**
* Extracts a message from a \Twig_Node_Print.
* Return null if not a constant message.
*
* @param \Twig_Node $node
* @return The message (or null)
*/
private function extractMessage(\Twig_Node $node)
{
if ($node->hasNode('node')) {
return $this->extractMessage($node->getNode('node'));
}
if ($node instanceof \Twig_Node_Expression_Constant) {
return $node->getAttribute('value');
}

return null;
}

/**
* Extracts a domain from a \Twig_Node_Print.
* Return null if no trans filter.
*
* @param \Twig_Node $node
* @return The domain (or null)
*/
private function extractDomain(\Twig_Node $node)
{
// must be a filter node
if (!$node instanceof \Twig_Node_Expression_Filter) {
return null;
}
// is a trans filter
if ($node->getNode('filter')->getAttribute('value') === 'trans') {
if ($node->getNode('arguments')->hasNode(1)) {
return $node->getNode('arguments')->getNode(1)->getAttribute('value');
}

return $this->defaultDomain;
}

return $this->extractDomain($node->getNode('node'));
}
}

F438
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?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;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
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.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class TranslationUpdateCommand extends ContainerAwareCommand
{
/**
* Compiled catalogue of messages.
* @var MessageCatalogue
*/
protected $catalogue;

/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('translation:update')
->setDescription('Update the translation file')
->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,
Copy link
Member

Choose a reason for hiding this comment

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

I don't get why we need a prefix option.

Copy link
Author

Choose a reason for hiding this comment

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

I did that, so new found translation message can be prefixed. It is helpfull to maintain your translations.

'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'
)
))
->setHelp(<<<EOF
The <info>translation:update</info> command extract translation strings from templates
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.

<info>php app/console translation:update --dump-messages en AcmeBundle</info>
<info>php app/console translation:update --force --prefix="new_" fr AcmeBundle</info>
EOF
);
}

/**
* {@inheritDoc}
*/
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>');
return;
}

// 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).'.');
return;
}

// 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()));

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

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

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

// show compiled list of messages
if($input->getOption('dump-messages') === true){
foreach ($catalogue->getDomains() as $domain) {
$output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
$output->writeln(Yaml::dump($catalogue->all($domain),10));
}
if($input->getOption('output-format') == 'xliff')
$output->writeln('Xliff output version is <info>1.2/info>');
}

// save the files
if($input->getOption('force') === true) {
$output->writeln('Writing files');
$writer->writeTranslations($catalogue, $input->getOption('output-format'), array('path' => $bundleTransPath));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* Adds tagged translation.formatter services to translation writer
*/
class TranslationDumperPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('translation.writer')) {
return;
}

$definition = $container->getDefinition('translation.writer');

foreach ($container->findTaggedServiceIds('translation.dumper') as $id => $attributes) {
$definition->addMethodCall('addDumper', array($attributes[0]['alias'], new Reference($id)));
10000 }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* Adds tagged translation.extractor services to translation extractor
*/
class TranslationExtractorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('translation.extractor')) {
return;
}

$definition = $container->getDefinition('translation.extractor');

foreach ($container->findTaggedServiceIds('translation.extractor') as $id => $attributes) {
$definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

Expand All @@ -26,6 +27,14 @@ public function process(ContainerBuilder $container)
foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attributes) {
$loaders[$id] = $attributes[0]['alias'];
}

if ($container->hasDefinition('translation.loader')) {
$definition = $container->getDefinition('translation.loader');
foreach ($loaders as $id => $format) {
$definition->addMethodCall('addLoader', array($format, new Reference($id)));
}
}

$container->findDefinition('translator.default')->replaceArgument(2, $loaders);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Scope;
Expand Down Expand Up @@ -57,6 +59,8 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FormPass());
$container->addCompilerPass(new TranslatorPass());
$container->addCompilerPass(new AddCacheWarmerPass());
$container->addCompilerPass(new TranslationExtractorPass());
$container->addCompilerPass(new TranslationDumperPass());

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
Loading
0