4

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?

  • See also https://stackoverflow.com/questions/34243278/poedit-doesnt-extract-string-in-html-tags and https://stackoverflow.com/questions/33910482/xgettext-does-not-extract-string-in-html-attribute – caw Feb 01 '19 at 21:42

3 Answers3

4

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.

  • 1
    Good news, [Poedit 2 is here](https://poedit.net/news/introducing-poedit2). With the paid PRO version, you get full support for Twig and translations inside HTML attributes also work. – Gchtr Apr 06 '17 at 06:21
1

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.

Gchtr
  • 2,749
  • 2
  • 18
  • 28
  • 1
    Did you try with the example provided in the question? For me, it does not work. – François Lefebvre Aug 08 '16 at 17:04
  • Just tried. You’re right, your use case doesn’t work with the Python parser. Here’s why: http://stackoverflow.com/a/34249157/1059980. I guess we have to wait for Poedit 2, where template languages seem to be supported better and until then, work with the solution you came up with. – Gchtr Aug 09 '16 at 06:45
  • Thanks @Gchtr for the try and for the hint about poedit 2! – François Lefebvre Aug 10 '16 at 14:11
0

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!

Marco
  • 712
  • 1
  • 10
  • 23