Support ES6 template tags
ES6 has template tags like this:
gettext`Some translatable string with ${variable} replacement`
We'd better support this.
Do you have any suggestion on the output of the expressions?
gettext(`Hello ${name}, welcome`)
gettext(`Hello ${name.toUpperCase() + ' ' + lastName.toLowerCase()}, welcome`)
gettext(`Hello ${"Mr" + 'Rask'+getSomething(2)}, welcome`)
Because the expressions can be any valid JavaScript, and we only have the AST, I don't see any way to reproduce the exact contents. The actual AST of the expression is a TemplateExpression node that as far as I can tell can have basically all the available node types as child nodes.
I see the following alternatives:
Only support simple variable expressions
This is the only thing I would personally use, and I already have a working implementation of this. Everything else can be represented by the empty string or some placeholder. The examples above would output
"Hello ${name}, welcome"
"Hello ${}, welcome"
"Hello ${}, welcome"
Generate a representation of the AST
Using escodegen. The examples above would output something like
"Hello ${name}, welcome"
"Hello ${name.toUpperCase() + \" \" + lastName.toLowerCase()}, welcome"
"Hello ${\"Mr\" + \"Rask\" + getSomething(2)}, welcome"
I think for the last two examples we should throw an exception and stop parsing since there's no way to create a reliable representation as you already mentioned. An empty string wouldn't be okay since there may be multiples of these sequences.
Alternatively, we may use numeric indices, or calculate a has from the source code inside the ${} portion and use that as the placeholder name.
@jacobrask still interested in this? This would be great to have in jsxgettext!