Closed rgpublic closed 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
@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?
@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?
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
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 :-)
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.
Fixed in v1.1.1
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.
Ok, I've made a change. Please, test now.
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.
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');
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.
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:
$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...