Closed swissspidy closed 6 years ago
@swissspidy thanks for this contribution, it looks really good. I think the error with twig is due before this change, the php parser gets only the comments placed just before the gettext function. For example:
/* this comment is extracted */
__('hello world');
/* this comment is not extracted because there's an 'echo' between the comment and the function */
echo __('hello world');
/* this comment is not extracted because there's a variable assignation */
$salute = __('hello world');
Now, all these comments are extracted, so I suggest to provide a way to configure whether the phpcode parser should extract the comments or not, in order to disable it on handle php returned by template compilers like twig or blade.
Now, all these comments are extracted, so I suggest to provide a way to configure whether the phpcode parser should extract the comments or not, in order to disable it on handle php returned by template compilers like twig or blade.
I think it might be even simpler than that.
In tests/bootstrap.php
there's this line:
Gettext\Extractors\PhpCode::$options['extractComments'] = '';
This tells all extractors to extract comments by default, no matter what prefix they have. However, according to http://twig-extensions.readthedocs.io/en/latest/i18n.html, Twig templates will always have the notes:
prefix in the translator comments, no matter what.
Thus, I'd suggest not to use $options['extractComments'] = ''
for the Twig extractor and instead use $options['extractComments'] = 'notes:'
.
See c91726a346a704ca83046ec50abf82d734050556 for an example. Instead of putting that into bootstrap.php
I'd probably enforce that in \Gettext\Extractors\Twig::fromString()
.
Ok, that's great. Yeah, I think putting this option as default here https://github.com/oscarotero/Gettext/blob/master/src/Extractors/Twig.php#L17 should be enought.
Great! I just did that in 502444162b76fe3304ed484ac6040ac0ed9ace39.
Added now, although that shouldn't really make any difference :-)
Thank you very much!
This aims to address #161 by simply adding all comments on the line before to the current function.
It seems to work fine, except that the Twig parser creates files like this:
For some reason, these line comments are now being picked up as translator comments and let the tests fail.