-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} | ||
|
131 changes: 131 additions & 0 deletions
131
src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
'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)); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationDumperPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationExtractorPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.