php-gettext / Gettext

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
MIT License
687 stars 134 forks source link

Parse PHP comments on the line before #164

Closed swissspidy closed 6 years ago

swissspidy commented 6 years ago

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:

/*  */
class __TwigTemplate_79a7d3b32ac8bc226e4f26ef9125e6a65dde2b398ad080a22197a3f33e8538b5 extends Twig_Template
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->parent = false;

        $this->blocks = array(
        );
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
        // line 1
        echo "<header>
    <h1>";
        // line 2
        echo gettext("text 1");
        echo "</h1>
</header"...

For some reason, these line comments are now being picked up as translator comments and let the tests fail.

oscarotero commented 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.

swissspidy commented 6 years ago

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().

oscarotero commented 6 years ago

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.

swissspidy commented 6 years ago

Great! I just did that in 502444162b76fe3304ed484ac6040ac0ed9ace39.

swissspidy commented 6 years ago

Added now, although that shouldn't really make any difference :-)

oscarotero commented 6 years ago

Thank you very much!