How to parse Timber (twig) templates with poedit and detect quoted strings to translate
Asked Answered
C

3

5

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?

Calciferol answered 29/7, 2016 at 18:47 Comment(1)
See also #34243778 and #33910982Sloane
C
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.

Calciferol answered 8/8, 2016 at 17:9 Comment(1)
Good news, Poedit 2 is here. With the paid PRO version, you get full support for Twig and translations inside HTML attributes also work.Wagstaff
W
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.

Wagstaff answered 6/8, 2016 at 20:40 Comment(3)
Did you try with the example provided in the question? For me, it does not work.Kermie
Just tried. You’re right, your use case doesn’t work with the Python parser. Here’s why: https://mcmap.net/q/1225324/-poedit-doesn-39-t-extract-string-in-html-tags. 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.Wagstaff
Thanks @Wagstaff for the try and for the hint about poedit 2!Kermie
H
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!

Heikeheil answered 14/12, 2016 at 18:10 Comment(1)
Was it really a problem of attribute translation?Kermie

© 2022 - 2024 — McMap. All rights reserved.