Skip to content

Conversation

adaamz
Copy link

@adaamz adaamz commented Jul 22, 2025

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 😉

Copy link

codecov bot commented Jul 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ 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.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Comment on lines +838 to +839
# start of new template string or object definition
if character == '{':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this handle something like

hello(`there is {nothing} to worry about`)

?
AISI, nothing would then be passed to extract_javascript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants