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

ZF2 Twig Templates #11

Closed Saeven closed 10 years ago

Saeven commented 10 years ago

I saw a similar issue here: https://github.com/umpirsky/Twig-Gettext-Extractor/issues/7

I am running this command:

twig-gettext-extractor --sort-output --force-po -o "/var/folders/lw/0f57k9dj617_ntr760qw2lcr0000gp/T/poeditOgHph2/3extracted.pot" --from-code=UTF-8  -k_ -ktranslate -ksetLabel -ksetValue -ksetLegend -k_refresh -L PHP --files  "list of files"

And unfortunately the command dies with:

PHP Fatal error:  Uncaught exception 'Twig_Error_Syntax' with message 'The function "formLabel" does not exist. Did you mean "form_label" in "/Users/Saeven/Documents/workspace/Launchfire Application/vendor/launchfire/core/view/ldp/forgot/index.twig" at line 13' in /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php:555
Stack trace:
#0 /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php(334): Twig_ExpressionParser->getFunctionNodeClass('formLabel', 13)
#1 /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php(147): Twig_ExpressionParser->getFunctionNode('formLabel', 13)
#2 /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php(84): Twig_ExpressionParser->parsePrimaryExpression()
#3 /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php(41): Twig_ExpressionParser->getPrimary()
#4 /U in /Users/Saeven/Documents/workspace/Launchfire Application/vendor/twig/twig/lib/Twig/ExpressionParser.php on line 555

Right now, it's dying over a template syntax that works, be it something like:

{{ formLabel( form.get('password') ) }}

Looks like any ZF helper causes breakage. Have you run into this?

Saeven commented 10 years ago

Alright, figured it out - answer was right in front of me. Just need to edit twig-gettext-extractor and add your ZF2 helpers…

$twig->addFunction(new \Twig_SimpleFunction('formLabel', true)); $twig->addFunction(new \Twig_SimpleFunction('formInput', true)); $twig->addFunction(new \Twig_SimpleFunction('formElementErrors', true)); $twig->addFunction(new \Twig_SimpleFunction('formCaptcha', true)); $twig->addFunction(new \Twig_SimpleFunction('formHidden', true));

umpirsky commented 10 years ago

@Saeven Yes, you can add them there https://github.com/umpirsky/Twig-Gettext-Extractor/blob/master/twig-gettext-extractor#L41