-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
-- add command that extracts translation messages from templates
-- add missing files -- tweak translation command files -- dumpers are now responsive for writting the files -- moved the twig extractor the bridge -- clear temp files after unit tests -- check the presence of dumper in translation writer -- General cleaning of the code -- clean phpDoc -- fix PHPDoc -- fixing class name in configuration -- add unit tests for extractors (php and twig) -- moved test to correct location -- polish the code -- polish the code
- Loading branch information
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('o 98D utput-format') == 'xliff') | ||
$output->writeln('Xliff output version is <info>1.2/info>'); | ||
} | ||
|
||
1023B // 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))); | ||
} | ||
} | ||
} |
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.
Allow use of UTF-8 catalogues in non-UTF-8 applications #2313
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
Uh oh!
There was an error while loading. Please reload this page.
Allow use of UTF-8 catalogues in non-UTF-8 applications #2313
Changes from 1 commit
a348efe
89f4791
21b29c2
5712798
4ac380e
f2761dd
d675c28
dea46c7
bbb68b7
834cc13
ed472d3
748cb84
a00c1f5
1d74073
6c93316
d84aecf
22548d0
e465474
f75d8ff
769b71f
9685b00
100c644
1c0b489
f23413c
7139154
bde551a
924b9f7
227eebc
522adde
6353de2
ac54080
3843afa
428a01c
1134c1a
1297504
69a50ab
6acc4f3
defdb82
89df6bb
b7c2a2e
8198078
f15cbc6
addfbd4
49e1eee
afc0971
4cd2dec
73692c6
85c3806
a0a97c6
0bc2a6d
ef322f6
5dcfeb2
b99bb1e
18d3dfe
b9b6084
f4784f7
218eaba
beeec5e
aecfd0a
e6e5146
a7b5c1c
47b7860
5140353
26a65d6
affb0cb
b016b04
1fc01fc
b3141b4
ce056d6
cfc202b
ea930e5
8faf278
92d35c8
53b4cd8
400159d
fabec37
b5783df
f9ecdfe
c5e0c80
c697b8f
365e73a
3a4d1a6
5526072
4ca09a9
132fbe3
bede420
83b69e6
a1491e8
61487cb
046a125
4061114
42407d6
bc8ed44
5146a1f
9fe87be
78c630c
88ebe0c
fe76d74
fe13a6c
847c665
c6b15b3
6fa1d64
0826d1c
c645314
27506bc
8c64202
19b2be1
92727f8
b8303f3
0c39eec
1b66af1
184bbe7
ed05e7a
80abf56
9703aac
9b025b7
ac765fc
7a20b89
cb9383d
903ab81
9a886ac
83199ae
c0e6118
89b9674
1ec6c8d
a0329c3
e2463ca
01a24db
774ac80
022a9a7
e13998d
7d3c2df
dd8e1e0
9683d3c
e473ea1
30de28e
e503bc4
916e4b1
3e90ba4
56b6d53
2b27c38
8b511c0
db3a2f2
0547059
e98584e
c9ddd6f
a0d0124
4dae5b8
e1aabd2
2838ff9
50570ba
f611b3e
798cf52
6d8c4a8
58d78f0
ddb94c4
1dde274
e5294fe
98abc8e
69468cb
645bd82
b6c8f63
ee12b67
902bbf2
85ba3d0
8819db3
a969434
5fe5556
b23d47d
0679220
f8a6a4b
67c33a8
c832b71
78ebe11
e4c743f
ea40156
d94f652
a1d4246
1103ca8
5dccc97
416eaa9
54454ba
9817772
07e5690
3ad395c
9b37637
6aec789
063e6f9
c4a0f79
7b204ed
eaf8ea3
bcc7357
0131a69
382a421
a57a4af
885bb33
fa57912
8cb0cc6
258a1fd
bfb99bf
ac31286
029223d
e02915b
d171d39
0f7bf41
fd00ed0
f532976
e74beb8
0c38b95
5306ca8
b9ba117
d6c4bfb
1cf3e91
41a55ba
f5ab6ec
02b30d9
245ff6d
8b240d4
21cf0ac
3223c5a
b3ea939
1c23ce9
1467bdb
84808f4
73fb30f
ee0fe7a
f9b2be9
b639d74
c78921f
0299d38
30ddf24
6b16757
51b3b59
eac2a77
dffda22
31840b9
8404b35
fbe9aa5
55e7e1a
d81131e
1fa3d8e
92420ad
9219cf9
4909169
79bea56
69922c4
3f567b8
0e852fe
deb6dea
5473d3b
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.