Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse PHP comments on the line before #164

Merged
merged 5 commits into from
Feb 21, 2018
Merged

Parse PHP comments on the line before #164

merged 5 commits into from
Feb 21, 2018

Conversation

swissspidy
Copy link
Contributor

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
Copy link
Member

@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
Copy link
Contributor Author

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 c91726a for an example. Instead of putting that into bootstrap.php I'd probably enforce that in \Gettext\Extractors\Twig::fromString().

@oscarotero
Copy link
Member

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
Copy link
Contributor Author

Great! I just did that in 5024441.

@swissspidy
Copy link
Contributor Author

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

@oscarotero oscarotero merged commit 060eea8 into php-gettext:master Feb 21, 2018
@oscarotero
Copy link
Member

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants