How to parse Timber (twig) templates with poedit a

2019-05-07 21:07发布

问题:

I want to parse twig templates for Timber with poedit and I need to translate quoted contents. The problem is that I can't find a parser that does not skip quoted content.

Example:

<htmltag attribute="{{ __('value','textdomain') }}" />

Does someone know of a parser for poedit that detects quoted content, like html attribute content?

回答1:

The workaround I found is to set a variable and use it as my attribute value.

{% set attr_value = __('value', 'textdomain') %}
<a href='{{ attr_value }}'>link</a>

This way, the PHP and Python parsers work, as suggested in the Timber documentation.

Using Twig-Gettext-Extractor, I get a Twig-Error-Syntax because of unrecognized __() function.



回答2:

Twig files are not automatically recognized when you use Poedit. To work around this, Timber’s documentation offers three options:

  • Either start each .twig file with {#<?php#}
  • Or use The Twig Gettext Extractor, a «poedit-friendly tool which extracts translations from twig templates».
  • Or use a Python parser. Refer to the documentation on how to set this up in Poedit.

I went with option 3 and it worked nicely.



回答3:

Had the same problem with OSX Sierra, and none of the three solutions worked for me:

  • Using {#<?php#} on top of my .twig files caused poedit not to tecognize my strings
  • Using Twig-Gettext-Extractor throw many errors
  • Using python compile throw many errors too

I solved using the first option ( {#<?php#} on top of my .twig files ) but I also edited PoEdit PHP extractor in order it can search also in .twig files. I hope this can help,

cheers!