ttag-org / ttag

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

ttag-cli version 1.5.0 and up not working #194

Closed matpen closed 4 years ago

matpen commented 4 years ago

I am just approaching ttag for the first time (thank you for the nice library, by the way) and setting up some experiments. I just noticed some strange behavior with ttag-cli: it seems like all versions from 1.5.0 to 1.8.0 fail to extract translations, while 1.3.0-1 and 1.4.0 work correctly.

For this experiment I am using the quickstart example, which I clone directly from github, but I tried with other simple JavaScript files, with same results:

# Clone the repo to test the example
cd /tmp
git clone https://github.com/ttag-org/ttag.git
cd ttag/examples/quickstart

# Test version 1.4.0: this works correctly
npm i ttag-cli@1.4.0 \
    && ./node_modules/.bin/ttag extract counter.js \
    && cat translations.pot

# command output:
# [ ... ]
# + ttag-cli@1.4.0
# added 218 packages from 119 contributors in 17.49s
# [ ... ]
# ✔ [ttag] translations extracted to translations.pot
# ... output of cat follows (file looks correct)
# msgid ""
# msgstr ""
# "Content-Type: text/plain; charset=utf-8\n"
# [ ... ]

# Test version 1.5.0: this does not work, the file is empty
npm i ttag-cli@1.5.0 \
    && ./node_modules/.bin/ttag extract counter.js \
    && cat translations.pot

# command output:
# [ ... ]
# + ttag-cli@1.5.0
# added 315 packages from 135 contributors and audited 2604 packages in 10.746s
# [ ... ]
# ✔ [ttag] translations extracted to translations.pot
# ... no more output from cat, file is empty
AlexMost commented 4 years ago

Hi @matpen! Thanks for the report, need to check that.

matpen commented 4 years ago

My pleasure! Thank you for the attention!

AlexMost commented 4 years ago

I guess the trouble is in require. If you will change require to import - it works. Will try to figure out what is wrong with requrie discoverage.

That thing is extracted - ok:

import { t, ngettext, msgid } from 'ttag';

function startCount(n){
    console.log(t`starting count up to ${n}`); // using 't' tag for 1 to 1 translations
    for (let i = 0; i <= n; i++) {
       // use ngettext function for handling plural forms
       console.log(ngettext(msgid`${i} tick passed`, `${i} ticks passed`, i));
    }
}
matpen commented 4 years ago

This is an interesting find. Indeed, if we change the extract command to

./node_modules/.bin/ttag extract --discover=t --discover=ngettext --discover=msgid counter.js

it works even with the latest version of ttag-cli.

So the problem must be somewhere in the interaction of the babel plugin with require.

AlexMost commented 4 years ago

fixed in ttag-cli@1.8.1. Have checked that locally, seems like it works. Thanks for the report and, please, let me know if you will encounter some more issues!

matpen commented 4 years ago

I just tested 1.8.2 and it seems to work correctly! Thank you for working on that!