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

extracting from JS code #121

Closed
lxg opened this issue Aug 27, 2016 · 4 comments
Closed

extracting from JS code #121

lxg opened this issue Aug 27, 2016 · 4 comments

Comments

@lxg
Copy link
Contributor

lxg commented Aug 27, 2016

It seems that JsFunctionsScanner skips certain functions from JS calls with more than one parameter. This means that it skips at least all ngettext() and pgettext() calls.

I tried the following with both a JS and a PHP source file:

// test.js
gettext("some message");
pgettext("some context", "some message in a context");
ngettext("%s message", "%s messages", 2);


<?php
// test.php
gettext("some message");
pgettext("some context", "some message in a context");
ngettext("%s message", "%s messages", 2);

Extracted and generated dumps with the following code:

$tJs = Translations::fromJsCodeFile("test.js");
$tPhp = Translations::fromPhpCodeFile("test.php");

echo $tJs->toJsonString([ "json" => JSON_PRETTY_PRINT ]);
echo $tPhp->toJsonString([ "json" => JSON_PRETTY_PRINT ]);

This gave me the following results:

{
    "domain": null,
    "plural-forms": null,
    "messages": {
        "": {
            "some message": [
                ""
            ]
        }
    }
}

{
    "domain": null,
    "plural-forms": null,
    "messages": {
        "": {
            "some message": [
                ""
            ],
            "%s message": [
                ""
            ]
        },
        "some context": {
            "some message in a context": [
                ""
            ]
        }
    }
}

The output for PHP is as expected, however, the JS output is missing the entries for the pgettext() and ngettext() calls.

@lxg
Copy link
Contributor Author

lxg commented Aug 27, 2016

Apparently, there’s a bug in JsFunctionsScanner->getFunctions(): When inserting a print_r($functions); just before the method’s return line, I see the following output:

Array
(
    [0] => Array
        (
            [0] => gettext
            [1] => 2
            [2] => Array
                (
                    [0] => some message
                )
        )

    [1] => Array
        (
            [0] => pgettext
            [1] => 3
            [2] => Array
                (
                    [0] => some context
                )
        )

    [2] => Array
        (
            [0] => ngettext
            [1] => 4
            [2] => Array
                (
                    [0] => %s message
                )
        )
)

Obviously, it doesn’t collect all parameters, but only the first of each function.

Doing the same with PhpFunctionsScanner->getFunctions() gives me the correct parameter lists for the PHP test file.

@lxg
Copy link
Contributor Author

lxg commented Aug 27, 2016

The bug is actually in JsFunctionsScanner->prepareArgument(): It chokes on the whitespace between the commas.

The method apparently tries to determine if the argument is a literal, but checks only for the first character. If there’s a space after the commas separating the parameters of the JS function call, it will not consider the argument to be a literal string.

The simple solution would be to insert $argument = trim($argument); at the beginning of the function. I can confirm that this solves the problem for me.

However, I didn’t dig too deep into the code, so I can’t really say if this is maybe more of a hack than a solution.

oscarotero added a commit that referenced this issue Aug 28, 2016
@oscarotero
Copy link
Member

I just made a commit to fix this. Let me know if it works fine now.

@lxg
Copy link
Contributor Author

lxg commented Aug 28, 2016

Yup, seems to work alright now. Thanks!

@lxg lxg closed this as completed Aug 28, 2016
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

No branches or pull requests

2 participants