Use Poedit to Extract Translations from Twig Templates

By default, Poedit does not have the ability to parse Twig templates. In Twig documenation there is a simple example which shows how to extract translations. This can work with few modifications, but I wanted to make reusable tool which will allow you to parse Twig templates just like you do with plain PHP templates.

Twig Gettext Extractor can be easily installed through composer or cloned from GitHub. Once you install all dependencies you can setup Poedit by adding new Twig parser (described in README).

After that you can use Poedit to parse your Twig templates (Catalog > Update from sources). No special catalog settings required, it will work with default keywords.

Feel free to use it, give feedback, contribute, report issues…

  • GreenHorn

    Is it possible to use Twig Gettext Extractor without Poedit, as command line commnad.

    • umpirsky

      Yes, but you need to provide parameters on your own. Check parser command in README.

  • GreenHorn

    I mean using it like Console command. If so then please provide some examples.

    • umpirsky

      Yes, I understand, it’s same as in README, you just need to provide parameters on your own. Something like ./twig-gettext-extractor –sort-output –force-po -o messages.po -L PHP –files file1.twig file2.twig file3.twig will do the job.

  • GreenHorn

    umpirsky thank you for your quick reply.

    ./twig-gettext-extractor –sort-output –force-po -o messages.po -L PHP –files file1.twig file2.twig file3.twig
    Above command worked only when file1.twig present in /vendor/bin folder.

    As I am new to Symfony and Twig Gettext Extractor. May I know in which folder I can get processed file.

    • umpirsky

      You can provide full path to your twig template. I’m curious, why don’t you use Poedit?

  • GreenHorn

    At present I am on Windows system and using ubuntu over Putty. At present ubuntu specifications don’t allow GUI support.

    May know where I can find processed file. messages.po

    • umpirsky

      You can copy project to windows and extract translations with Poedit. You can also specify absolute path to output file, if you use my example, it is in your current working directory.

  • GreenHorn

    Dear Umpirsky

    Thanks for help.

    • umpirsky

      You’re welcome.