babel icon indicating copy to clipboard operation
babel copied to clipboard

Extract parametrized gettext from javascript files

Open adaamz opened this issue 7 months ago • 1 comments

Hello, I prepared PR which allows extraction of translations from JS files that use gettext function with parameters.

Usually code that uses parametrized translations looks like this:

gettext("Hello %(user)s", {user: userName})

which works (gets extracted) but problem is with template-strings:

`${gettext("Hello %(user)s", {user: userName})}`

It probably got confused with the curly braces for object initialization and thinks that end of object initialization is end of whole template-string (which is not true - there are 3 more chars). Later this incomplete template string get parsed as JS code again and ends up as incomplete/invalid and the translation is not extracted at all.

My fix is simply marking all { as start of "nesting" which allows extraction of non-nested template-strings.

I tried to support also nested template-strings but didn't get it to work at all... it needs a bit more complicated parser than just regex and I guess nobody use that atm as I don't see any issue about it.

Hope you find it usefull. Thanks for review and possibly merge 😉

adaamz avatar Jul 22 '25 22:07 adaamz

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 91.93%. Comparing base (fb1c19c) to head (6ef6337).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1221   +/-   ##
=======================================
  Coverage   91.93%   91.93%           
=======================================
  Files          27       27           
  Lines        4688     4688           
=======================================
  Hits         4310     4310           
  Misses        378      378           
Flag Coverage Δ
macos-14-3.10 90.97% <100.00%> (ø)
macos-14-3.11 90.91% <100.00%> (ø)
macos-14-3.12 91.12% <100.00%> (ø)
macos-14-3.13 91.12% <100.00%> (ø)
macos-14-3.8 90.84% <100.00%> (ø)
macos-14-3.9 90.90% <100.00%> (ø)
macos-14-pypy3.10 90.97% <100.00%> (ø)
ubuntu-24.04-3.10 90.99% <100.00%> (ø)
ubuntu-24.04-3.11 90.93% <100.00%> (ø)
ubuntu-24.04-3.12 91.14% <100.00%> (ø)
ubuntu-24.04-3.13 91.14% <100.00%> (ø)
ubuntu-24.04-3.8 90.86% <100.00%> (ø)
ubuntu-24.04-3.9 90.92% <100.00%> (ø)
ubuntu-24.04-pypy3.10 90.99% <100.00%> (ø)
windows-2022-3.10 90.98% <100.00%> (ø)
windows-2022-3.11 90.92% <100.00%> (ø)
windows-2022-3.12 91.13% <100.00%> (ø)
windows-2022-3.13 91.13% <100.00%> (ø)
windows-2022-3.8 90.96% <100.00%> (ø)
windows-2022-3.9 90.91% <100.00%> (ø)
windows-2022-pypy3.10 90.98% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jul 29 '25 07:07 codecov[bot]