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

Skip to content

[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
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
-- tweak the translation loader, writer and extractor
  • Loading branch information
Michel Salib committed Jun 13, 2011
commit 7c0329478bb273654bd28cc5763ac8c58ff0fc65
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

// load any messages from templates
$output->writeln('Parsing templates');
$templateExtractor = $this->container->get('translation.extractor.template');
$templateExtractor->setPrefix($input->getOption('prefix'));
$templateExtractor->extractMessages($foundBundle->getPath() . '/Resources/views/', $catalogue);
$extractor = $this->container->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('Parsing translation files');
$fileExtractor = $this->container->get('translation.extractor.file');
$fileExtractor->extractMessages($bundleTransPath, $catalogue);
$output->writeln('Loading translation files');
$loader = $this->container->get('translation.loader');
$loader->loadMessages($bundleTransPath, $catalogue);

// show compiled list of messages
if($input->getOption('dump-messages') === true){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

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

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

foreach ($container->findTaggedServiceIds('translation.extractor') as $id => $attributes) {
$definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id)));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

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

class TranslatorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('translator.real')) {
return;
if ($container->hasDefinition('translator.real')) {
$loaders = array();
foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attributes) {
$loaders[$id] = $attributes[0]['alias'];
}
$container->findDefinition('translator.real')->replaceArgument(2, $loaders);
}

$loaders = array();
foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attributes) {
$loaders[$id] = $attributes[0]['alias'];
if ($container->hasDefinition('translation.loader')) {
$definition = $container->getDefinition('translation.loader');
foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attributes) {
$definition->addMethodCall('addLoader', array($attributes[0]['alias'], new Reference($id)));
}
}
$container->findDefinition('translator.real')->replaceArgument(2, $loaders);
}
}
6 changes: 2 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
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\TranslationTemplateExtractorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationFileExtractorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationWriterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
Expand Down Expand Up @@ -51,8 +50,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FormPass());
$container->addCompilerPass(new TranslatorPass());
$container->addCompilerPass(new AddCacheWarmerPass());
$container->addCompilerPass(new TranslationTemplateExtractorPass());
$container->addCompilerPass(new TranslationFileExtractorPass());
$container->addCompilerPass(new TranslationExtractorPass());
$container->addCompilerPass(new TranslationWriterPass());

if ($container->getParameter('kernel.debug')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<parameter key="translation.formatter.xliff.class">Symfony\Component\Translation\Formatter\XliffFormatter</parameter>
<parameter key="translation.formatter.yml.class">Symfony\Component\Translation\Formatter\YamlFormatter</parameter>
<parameter key="translation.extractor.php.class">Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor</parameter>
<parameter key="translation.extractor.template.class">Symfony\Bundle\FrameworkBundle\Translation\TemplateTranslationExtractor</parameter>
<parameter key="translation.extractor.file.class">Symfony\Bundle\FrameworkBundle\Translation\FileTranslationExtractor</parameter>
<parameter key="translation.writer.class">Symfony\Bundle\FrameworkBundle\Translation\FileTranslationWriter</parameter>
<parameter key="translation.loader.class">Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader</parameter>
<parameter key="translation.extractor.class">Symfony\Component\Translation\Extractor\TranslationExtractor</parameter>
<parameter key="translation.writer.class">Symfony\Component\Translation\Writer\TranslationWriter</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -71,9 +71,9 @@
<tag name="translation.extractor" alias="php" />
</service>

<service id="translation.extractor.template" class="%translation.extractor.template.class%"/>
<service id="translation.loader" class="%translation.loader.class%"/>

<service id="translation.extractor.file" class="%translation.extractor.file.class%"/>
<service id="translation.extractor" class="%translation.extractor.class%"/>

<service id="translation.writer" class="%translation.writer.class%"/>
</services>
Expand Down
23 changes: 14 additions & 9 deletions src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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
Expand Down Expand Up @@ -41,9 +43,14 @@ class PhpExtractor implements ExtractorInterface
/**
* {@inheritDoc}
*/
public function load($file, MessageCatalogue $catalog)
public function load($directory, MessageCatalogue $catalog)
{
$this->parseTokens(token_get_all(file_get_contents($file)), $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);
}
}

/**
Expand All @@ -62,8 +69,9 @@ public function setPrefix($prefix)
*/
protected function normalizeToken($token)
{
if(is_array($token))
if (is_array($token)) {
return $token[1];
Copy link
Member

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 the if and the brace

}

return $token;
}
Expand All @@ -83,14 +91,11 @@ protected function parseTokens($tokens, MessageCatalogue $catalog)
foreach ($sequence as $id => $item) {
if($this->normalizeToken($tokens[$key + $id]) == $item) {
continue;
}
elseif (self::MESSAGE_TOKEN == $item) {
} elseif (self::MESSAGE_TOKEN == $item) {
$message = $this->normalizeToken($tokens[$key + $id]);
}
elseif (self::IGNORE_TOKEN == $item) {
} elseif (self::IGNORE_TOKEN == $item) {
continue;
}
else {
} else {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Symfony\Component\Translation\Loader\LoaderInterface;

/**
* Extract translation messages from translation files
* Load translation messages from translation files
*/
class FileTranslationExtractor
class TranslationLoader
{
/**
* Loaders used for import
Expand All @@ -29,12 +29,12 @@ public function addLoader($format, LoaderInterface $loader)
}

/**
* Extract translation messages from a directory to the catalogue
* Load translation messages from a directory to the catalogue
*
* @param string $directory the directory to look into
* @param MessageCatalogue $catalogue the catalogue
*/
public function extractMessages($directory, MessageCatalogue $catalogue)
public function loadMessages($directory, MessageCatalogue $catalogue)
{
foreach($this->loaders as $format => $loader) {
// load any existing translation files
Expand Down
14 changes: 10 additions & 4 deletions src/Symfony/Bundle/TwigBundle/Translation/TwigExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Symfony\Bundle\TwigBundle\Translation;

use Symfony\Bundle\FrameworkBundle\Translation\ExtractorInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Bridge\Twig\Node\TransNode;
use Twig_Environment;
Expand Down Expand Up @@ -40,10 +41,15 @@ public function __construct(Twig_Environment $twig)
/**
* {@inheritDoc}
*/
public function load($file, MessageCatalogue $catalogue)
public function load($directory, MessageCatalogue $catalogue)
{
$tree = $this->twig->parse($this->twig->tokenize(file_get_contents($file->getPathname())));
$this->crawlNode($tree, $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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Translation;
namespace Symfony\Component\Translation\Extractor;

use Symfony\Component\Translation\MessageCatalogue;

/**
* Interface implemented by all extractors
*/
interface ExtractorInterface {
interface ExtractorInterface
{
/**
* Extract translation messages from a template file to the catalogue
* Extract translation messages from a template directory to the catalogue
*
* @param string $file the path to the template file to look into
* @param string $directory the path to look into
* @param MessageCatalogue $catalogue the catalogue
*/
function load($file, MessageCatalogue $catalogue);
function load($directory, MessageCatalogue $catalogue);

/**
* Set the prefix that should be used for new found messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Translation;
namespace Symfony\Component\Translation\Extractor;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;

/**
* Extract translation messages from translation files
* Extract translation messages from template files
*/
class TemplateTranslationExtractor
class TranslationExtractor
{
/**
* Extractors
Expand All @@ -20,11 +19,11 @@ class TemplateTranslationExtractor
/**
* Add a loader to the translation extractor
* @param string $format The format of the loader
* @param ExtractorInterface $extactor
* @param ExtractorInterface $extractor
*/
public function addExtractor($format, ExtractorInterface $extactor)
public function addExtractor($format, ExtractorInterface $extractor)
{
$this->extractors[$format] = $extactor;
$this->extractors[$format] = $extractor;
}

/**
Expand All @@ -45,13 +44,8 @@ public function setPrefix($prefix)
*/
public function extractMessages($directory, MessageCatalogue $catalogue)
{
foreach($this->extractors as $format => $extactor) {
// load any existing translation files
$finder = new Finder();
$files = $finder->files()->name('*.'.$format)->in($directory);
foreach ($files as $file) {
$extactor->load($file, $catalogue);
}
foreach ($this->extractors as $extractor) {
$extractor->load($directory, $catalogue);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Translation;
namespace Symfony\Component\Translation\Writer;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Formatter\FormatterInterface;

/**
* Write translation messages to translation files
*/
class FileTranslationWriter
{
class TranslationWriter
{
/**
* Formatters used for export
*
Expand Down
0