ttag-org / ttag

:orange_book: simple approach for javascript localization
https://ttag.js.org/
MIT License
345 stars 43 forks source link

Incomplete precompilation output when using context mechanism #151

Closed manuel-woelker closed 5 years ago

manuel-woelker commented 5 years ago

Hi, first of all thanks for the great lib - much appreciated! However I ran into a bit of an issue today:

When using ttag-cli to precompile translations, the resulting code is malformed. I put up a simple demo repo here: https://github.com/manuel-woelker/ttag-precompile-context/blob/master/test.x.js (Run using yarn install && yarn run precompile) Input (in file test.js):

console.log(c('foo').t`with_context`);

Expected output (in test.x.js, using translation file x.po):

console.log("two");

Actual output:

"two";

Interestingly, wrapping the translation part with a bogus function makes it work, e.g.:

console.log(make_it_work(c('foo').t`with_context`));

My best guess is that the transformation function is somehow removing too much of the AST during the replacement.

It could also reproduce this with the babel-plugin-ttag and babel-plugin-macros.

Any ideas what the issue might be? Thanks in advance!

AlexMost commented 5 years ago

Hi @manuel-woelker, glad that you liked our lib. Seems like it's a bug. Going to investigate, will report any progress here.

manuel-woelker commented 5 years ago

Thanks for the quick feedback! Let me know if I can support in any way.

AlexMost commented 5 years ago

You can try the new version of ttag-cli@1.7.2 that fixes this issue. I have tried on your example repo and yarn run precompile produces the following:

"use strict";

var _ttag = require("ttag");

// works
console.log("one"); // doesn't work, only outputs "two;"

console.log("two"); // works - wrapped with a bogus function

console.log(make_it_work("two"));

Please, let me know if that works for you.

manuel-woelker commented 5 years ago

I just verified with 1.7.2 and it's looking great! Thanks again for the quick fix.