-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.1] Extracting translation messages from templates #1283
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7184f67
-- add use of translation:update command from michelsalib:ExtraToolsB…
d0aae9a
-- extract translation reader and writer to services
7da09f5
-- tweak transaltion extractor
a61492f
-- use transaltion writer/formatter with proper DI
44bccd9
tweak the twig bundle
b1efbb6
tweak the twig bundle
a8b8a8a
tweak the twig bundle
b102111
Added PHP Translation extractor.
xaav fb2171d
Updated extractor.
xaav 6014581
-- refactor the logic of translation:update command to use twig and p…
7c03294
-- tweak the translation loader, writer and extractor
e6a7c76
tweak the TranslatorPass
5b395d4
Merge remote-tracking branch 'upstream/master'
14b3984
-- update the translation:update Command to ContainerAwareCommand
c2ed4ee
Merge remote-tracking branch 'upstream/master'
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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
<?php | ||
|
||
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; | ||
|
||
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 8000 39;, null, InputOption::VALUE_OPTIONAL, | ||
'Override the default prefix', '__' | ||
), | ||
new InputOption( | ||
'output-format', null, InputOption::VALUE_OPTIONAL, | ||
'Override the default output format', 'yml' | ||
), | ||
new InputOption( | ||
'source-lang', null, InputOption::VALUE_OPTIONAL, | ||
'Set the source language attribute in xliff files', 'en' | ||
), | ||
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' | ||
) | ||
)); | ||
} | ||
|
||
/** | ||
* {@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 | ||
$fileWriter = $this->getContainer()->get('translation.writer'); | ||
$supportedFormats = $fileWriter->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'); | ||
$fileWriter->writeTranslations($catalogue, $bundleTransPath, $input->getOption('output-format')); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
<?php | ||
|
||
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))); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationWriterPass.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,26 @@ | ||
<?php | ||
|
||
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 TranslationWriterPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('translation.writer')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('translation.writer'); | ||
|
||
foreach ($container->findTaggedServiceIds('translation.formatter') as $id => $attributes) { | ||
$definition->addMethodCall('addFormatter', 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
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
110 changes: 110 additions & 0 deletions
110
src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.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,110 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Translation; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Extractor\ExtractorInterface; | ||
|
||
/** | ||
* Extract translation messages from a php template | ||
*/ | ||
class PhpExtractor implements ExtractorInterface | ||
{ | ||
const MESSAGE_TOKEN = 300; | ||
const IGNORE_TOKEN = 400; | ||
|
||
/** | ||
* Prefix for found message | ||
* | ||
* @var string | ||
*/ | ||
private $prefix = ''; | ||
|
||
/** | ||
* The sequence that captures translation messages | ||
* | ||
* @var array | ||
*/ | ||
protected $sequences = array( | ||
array( | ||
'$view', | ||
'[', | ||
'\'translator\'', | ||
']', | ||
'->', | ||
'trans', | ||
'(', | ||
self::MESSAGE_TOKEN, | ||
')', | ||
), | ||
); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function load($directory, MessageCatalogue $catalog) | ||
{ | ||
// load any existing translation files | ||
$finder = new Finder(); | ||
$files = $finder->files()->name('*.php')->in($directory); | ||
foreach ($files as $file) { | ||
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setPrefix($prefix) | ||
{ | ||
$this->prefix = $prefix; | ||
} | ||
|
||
/** | ||
* Normalize a token | ||
* | ||
* @param mixed $token | ||
* @return string | ||
*/ | ||
protected function normalizeToken($token) | ||
{ | ||
if (is_array($token)) { | ||
return $token[1]; | ||
} | ||
|
||
return $token; | ||
} | ||
|
||
/** | ||
* Extract trans message from php tokens | ||
* | ||
* @param array $tokens | ||
* @param MessageCatalogue $catalog | ||
*/ | ||
protected function parseTokens($tokens, MessageCatalogue $catalog) | ||
{ | ||
foreach ($tokens as $key => $token) { | ||
foreach ($this->sequences as $sequence) { | ||
$message = ''; | ||
|
||
foreach ($sequence as $id => $item) { | ||
if($this->normalizeToken($tokens[$key + $id]) == $item) { | ||
continue; | ||
} elseif (self::MESSAGE_TOKEN == $item) { | ||
$message = $this->normalizeToken($tokens[$key + $id]); | ||
} elseif (self::IGNORE_TOKEN == $item) { | ||
continue; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if ($message) { | ||
$catalog->set($message, $this->prefix.$message); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
you have to use curly braces even when there is only one line in the
if
block. And you need to add a space between theif
and the brace