umpirsky / Twig-Gettext-Extractor

The Twig Gettext Extractor is Poedit friendly tool which extracts translations from twig templates.
MIT License
113 stars 32 forks source link

Exception trows in Twig\Gettext\Extractor::addTemplate method #2

Closed uran1980 closed 11 years ago

uran1980 commented 11 years ago

Hi, @umpirsky

When I use assetic bundle functionality in my twig templates I recive Exeptions in Twig\Gettext\Extractor::addTemplate method.

When I use in twig template asset, for example:

<img src="{{ asset('.../placeholders/120x120.gif') }}" alt="">

Twig\Gettext\Extractor::addTemplate method throws exception with message:

The function "asset" does not exist in "...\FrontendBundle\Resources\views\Default\_includes\content.html.twig" at line 12

When I use in twig template stylesheets, for example:

{% block stylesheets %}
    {{ parent() }}
    {% stylesheets output='compiled/css/frontend.css' filter='cssrewrite'
        'bundles/myprojectfrontend/css/frontend-base.css'
        'bundles/myprojectfrontend/css/frontend-content.css'
    %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}"  />
    {% endstylesheets %}
{% endblock stylesheets %}

Twig\Gettext\Extractor::addTemplate method throws exception with message:

Unexpected tag name "stylesheets" (expecting closing tag for the "block" tag defined near line 6) in "...\FrontendBundle\Resources\views\layout.html.twig" at line 7

I partially solve this by adding try-catch statement to Twig\Gettext\Extractor::addTemplate method: 2013-01-02-10-30-sshot-1

Now problematic twig templates are skiped from processing, and translations in it are too (that is bad). I understend that root problem in twig engine. Any suggestions?

Thanks.

umpirsky commented 11 years ago

Hi.

If you look at twig-gettext-extractor#L41 you will see that there is a place where I registered some default extensions, and left place for new ones.

What you can do is copy this script to your bin folder for example cp vendor/bin/twig-gettext-extractor bin/twig-gettext-extractor, and then customize it by adding something like:

$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension());

and now use your bin/twig-gettext-extractor with Poedit.

You can do same for every other extension, even for your custom extensions ;)

uran1980 commented 11 years ago
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension());

does not work. I try that, as AsseticExtension need AssetFactory:

$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension(
    new Assetic\Factory\AssetFactory($rootDir)
));

But recive Exeption:

An exception has been thrown during the compilation of a template ("There is no filter manager.") in "...\FrontendBundle\Resources\views\layout.html.twig".

at the moment I can't figure out how to add AsseticExtension throw $twig->addExtention() method in a correct way...

umpirsky commented 11 years ago

@uran1980 Full trace would be nice to have, but as far as I can see, you are missing FilterManager, you can set it with:

$assetFactory->setFilterManager(new Assetic\FilterManager);
uran1980 commented 11 years ago

Yes it work. Greate!!!

// setup filter manager
$filterManager = new Assetic\FilterManager();
$filterManager->set('cssrewrite', new Assetic\Filter\CssRewriteFilter());
// set other filters if needed...

// setup asset factory
$assetFactory = new Assetic\Factory\AssetFactory($rootDir);
$assetFactory->setFilterManager($filterManager);

// add extension
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension($assetFactory));

And now I am struggle with next final Exception:

2013-01-02 19:58:33] twig-gettext-extractor.log.INFO: Array
(
    [method] => Twig\Gettext\Extractor::addTemplate
    [line] => 67
    [exeption] => Exception: The function "asset" does not exist in "...\FrontendBundle\Resources\views\Default\_includes\content.html.twig" at line 12
)
 [] []

So now I need add to twig-gettext-extractor support for recognize asset function :) Thanks for advice :+1:

umpirsky commented 11 years ago

Can you please post "...\FrontendBundle\Resources\views\Default_includes\content.html.twig" at line 12?

uran1980 commented 11 years ago

Yes, it is a plain twig template: 2013-01-02-19-40-sshot-1

umpirsky commented 11 years ago

Try to change:

// add extension
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension($assetFactory));

to

// add extension
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension($assetFactory), array('asset'));
uran1980 commented 11 years ago

I change to:

// add extension
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension(
    $assetFactory,
    array('asset')
));

But no result, same Exception as before:

[2013-01-02 21:14:55] twig-gettext-extractor.log.INFO: Array
(
    [method] => Twig\Gettext\Extractor::addTemplate
    [line] => 67
    [exeption] => Exception: The function "asset" does not exist in "...\FrontendBundle\Resources\views\Default\_includes\content.html.twig" at line 12
)
 [] []
uran1980 commented 11 years ago

I solve it finaly and that work pirfect :)

// setup filter manager
$filterManager = new Assetic\FilterManager();
$filterManager->set('cssrewrite', new Assetic\Filter\CssRewriteFilter());
// set other filters if needed...

// add functions
$twig->addFunction('asset', new Twig_SimpleFunction('asset', true));
// set other functions if needed...

// setup asset factory
$assetFactory = new Assetic\Factory\AssetFactory($rootDir);
$assetFactory->setFilterManager($filterManager);

// add extension
$twig->addExtension(new Assetic\Extension\Twig\AsseticExtension($assetFactory));

Now all my trans in all twig templates are processed by PoEdit.

Thanks again @umpirsky for your advice.

umpirsky commented 11 years ago

Great.

I think there must be a simpler way around :)