php-gettext / PHP-Scanner

PHP code scanner for gettext
MIT License
14 stars 5 forks source link

Comment extraction doesnt seem to work...? #6

Closed rgpublic closed 4 years ago

rgpublic commented 4 years ago

Is it just me or doesnt the comment extraction work with the new version? With the old version I think there was enableCommentsExtraction. With the new version there is no such method and the extraction also doesn't happen automatically it seems. I use this code for testing:

$translations=Translations::create();
$phpScanner=new PhpScanner($translations);
$php=<<<PHP
<?php
//I love comment extraction
echo gettext('hello world');
PHP;
$phpScanner->scanString($php,"hello.php");
print_r($translations);
exit;

$translation->extractedComments seems to be empty.

This feature was very useful for parsing the PHP compiled from Twig template files, because the compiled PHP code included the original line number in the Twig file so you get a kind of source-map feature where you can see where the real line of the respective translation was...

oscarotero commented 4 years ago

Ok, seems like the functions with echo are not extracted, so:

//This does not work
echo gettext('hello world');

//This works
gettext('hello world');

I'm fixing it

oscarotero commented 4 years ago

@rgpublic I made a commit to extract the comments of functions after echo, print and return. Could you please test it before release a new version?

rgpublic commented 4 years ago

@oscarotero Hm. It doesnt seem to work yet for me. Even without the patch and without that "echo" it doesnt extract anything. I'm using this code:

$translations=Translations::create();
$phpScanner=new PhpScanner($translations);
$php=<<<PHP
<?php
//I love comment extraction
gettext('hello world');
PHP;
$phpScanner->scanString($php,"hello.php");
print_r($translations);
exit;

Does exactly that code work for you?

oscarotero commented 4 years ago

Are you using the dev-master version? (the patch is not released yet). I'm using this code for tests and it works fine: https://github.com/php-gettext/PHP-Scanner/blob/master/tests/assets/functions.php

rgpublic commented 4 years ago

Yes, I did. Hm. I think the difference is that you are using directly PhpFunctionsScanner and I use PhpScanner. With low-level PhpFunctionsScanner it seems to work,yes. But I don't want to just scan for functions. I want to turn these parsed code/functions into full-fledged Translation objects! If I understand correctly, this is exactly what PhpScanner (in contrast to PhpFunctionsScanner) is good for - inside the methods gettext() and finally saveTranslation(). But these comments never seem to end up in ParsedFunction or in the final Translation object... But perhaps I'm just to dumb to use it. Could very well be. Please tell me what I'm doing wrong :-)

oscarotero commented 4 years ago

Ok, you're right. Extracted comments weren't saved. I've fixed it. To test it, you need to install the dev-master version of gettext/gettext and gettext/php-scanner, because I had to modify both packages.

Let me know if it works fine now.

oscarotero commented 4 years ago

Fixed in v1.1.1

rgpublic commented 4 years ago

Sorry. For the delay and bothering you again, but it still doesn't seem to work properly when the t() call is nested inside some other call like:

<?php
    // line 17
    echo renderVar(t("Ausgezeichnet"));

If I try to extract that with:

$scanner->extractCommentsStartingWith("line ");

it doesnt seem to work. Without the renderVar() call it works. Unfortunately I cannot influence this as this is how the Twig templates look when they are compiled to PHP.

This used to work with 4.x, though. Might be due to the new parsing method which (apart from this problem) I think was generally a really great idea. It's much better to rely on nikic/php-parser instead of trying to do all the work yourself.

oscarotero commented 4 years ago

Ok, I've made a change. Please, test now.

rgpublic commented 4 years ago

Finally coming back to this issue. Yes, I can confirm that your change fixes this. Unfortunately I found that it doesn't really help me in all cases, because sometimes when Twig templates are compiled, the comment with the line isn't the immediately preceding statement :-( For example this Twig code:

{{ something_else() }} {{ "Example"| t}}

is basically compiled into:

// line 17
echo something_else();
echo renderVar(t("Example"));

The "line 17" isn't picked up anymore by GetText 5.x. It used to work with 4.x, so for me this is a regression, but I could of course understand if you don't want to support that in the new version. Sigh.

lbarbula commented 4 years ago

A more straightforward case that also doesn't seem to work is a comment around a gettext block being assigned to a variable. Such as:

//Translators: testing please
$gettext_string = gettext('Bar Foo');
oscarotero commented 4 years ago

FYI, there's a work in progress repository to implement a scanner for twig templates, using the twig AST, instead the compiled php code: https://github.com/php-gettext/Twig-Scanner

Any help is welcome.