lajax / yii2-translate-manager

Translation Manager
MIT License
227 stars 90 forks source link

Can't translate js in php file #54

Closed NickvdMeij closed 8 years ago

NickvdMeij commented 8 years ago

Hi,

For some reason, the translate manager doesn't pick up the lajax.t() within a php file (specifically, the views of our webpage where we have some inline javascript).

Because this is a php file, it only scans for the ::t function. Even though I've tried adding the lajax.t() to the phpTranslators in the translatemanager config, the translatemanager still won't pick up on those javascript translate functions. Is there a way to get this working?

moltam commented 8 years ago

I've tested this, and the File Scanner currently can't extract messages from HTML inside a PHP file. That's why the lajax.t translator not recognized.

Maybe try something like this:

<script>
    console.log("<?= \Yii::t('app', 'Javascript text in PHP file.') ?>");
</script>
NickvdMeij commented 8 years ago

@moltam We dont use the HTML scripttags. We use the following syntax:

$ga =<<<JS
    lajax.l("Hello World")
JS;

$this->registerJs($ga, View::POS_HEAD);

Could it have something to do with the PHP heredoc syntax?

moltam commented 8 years ago

I think it doesn't matter where is the JS code, in heredoc, HTML content, or string literals. When it comes to parse these, they are just strings for the PHP tokenizer (the extension uses this), and because of this they won't parsed as PHP code, and no translations are recognized.

Although parsing JS in PHP files isn't an impossible task, but the extension currently does not support it.

NickvdMeij commented 8 years ago

@moltam I'll take a look at it, and if possible fix it and make a PR ;)

NickvdMeij commented 8 years ago

A fix is available in pullrequest #62

moltam commented 8 years ago

Another solution is to use yii2-js-register extension.

This way Yii::t() is usable in inline javascript (in a simpler way):

<?php JSRegister::begin(['position' => \yii\web\View::POS_HEAD]); ?>
<script>
    $(".modal").modal({
        title: "<?= Yii::t('app', 'Javascript text in PHP file.') ?>"
    });
</script>
<?php JSRegister::end(); ?>