EZP-26430: Translation dumper to detect strings to translate in .js #721
Conversation
* | ||
*/ | ||
|
||
var walk = require( 'estree-walker' ).walk, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CS spaces after/before parenthesis, curly braces, ...
*/ | ||
|
||
var walk = require( 'estree-walker' ).walk, | ||
acorn = require( 'acorn' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: did you switch to acorn because estree-walker was not working with espree or just to follow the example in the README ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just followed the example in the README.
|
||
if( node.type && node.type == 'CallExpression' && node.callee.property.name == 'trans' ) { | ||
result.translationsFound.push({ | ||
key: node.arguments[0].value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if the key and/or domain parameters are variables or any other expression ?
34e2ca2
to
21ab1fb
Compare
@@ -0,0 +1,6 @@ | |||
//Somme dummy javascript code with some translation in it. | |||
|
|||
var result1 = Y.eZ.trans('test.translation.result1', 'testdomain'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Y.eZ.trans
always expects 3 parameters see https://github.com/ezsystems/PlatformUIBundle/blob/master/Resources/public/js/services/ez-translator.js#L33
}, | ||
|
||
isY = function (node) { | ||
return node.callee.object && node.callee.object.object && node.callee.object.object.name && node.callee.object.object.name === "Y"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you don't need && node.callee.object.object.name
}, | ||
|
||
isEz = function (node) { | ||
return node.callee.object.property && node.callee.object.property.name && node.callee.object.property.name === "eZ"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here (I guess), && node.callee.object.property.name
can be removed
ast = acorn.parse(sourceCode, {}), | ||
result = {translationsFound: []}; | ||
|
||
walk( ast, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CS: no space after (
|
||
foreach ($files as $file) { | ||
$command = 'node ' . $this->translationDumperPath . ' ' . $file; | ||
$json = exec($command); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use shell_exec instead just in case the output is on several lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and by the way, escapeshellarg on the argument
75ca75a
to
db40073
Compare
@dpobel 's feedback taken into account. Travis is fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
besides the last 2 comments
arguments: | ||
- "@=service('kernel').locateResource('@eZPlatformUIBundle/Resources/public/js')" | ||
- "@=service('kernel').locateResource('@eZPlatformUIBundle/bin/Translation/translation_dumper.js')" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty line to remove
var domain; | ||
|
||
if(isCallExpression(node) && isTransFunction(node) && isY(node) && isEz(node) && hasValues(node)) { | ||
// Parameters not present |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameters is mandatory so domain is always node.arguments[2].value
ping @StephaneDiot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alias for the extractor should probably be changed.
I'll do a PR to this PR to make the resources list dynamic.
|
||
foreach ($files as $file) { | ||
$command = 'node ' . $this->translationDumperPath . ' ' . escapeshellarg($file); | ||
$json = shell_exec($command); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use http://symfony.com/doc/current/components/process.html here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow up ?
- "@=service('kernel').locateResource('@eZPlatformUIBundle/Resources/public/js')" | ||
- "@=service('kernel').locateResource('@eZPlatformUIBundle/bin/Translation/translation_dumper.js')" | ||
tags: | ||
- { name: translation.extractor, alias: handlebars } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the alias be javascript
? You already use handlebars
above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 FWIW
03f96ff
to
892a31d
Compare
Pushed a minor improvement to the dumper's implementation, as suggested by @nicolas-bastien. |
dd42cab
to
fe11825
Compare
Link: https://jira.ez.no/browse/EZP-26430
Description
Some translations strings are located in JS files. This PR provides a script to automatically detect them and add them to the symfony catalog.
This extractor relies on nodejs to parse the javascript files.
Note: Translation strings using variables parameters are not detected at the moment.
Tests
Manual and unit tests.