Skip to content

wp-build: Keep translator comments next to their calls in built JavaScript - #87189

Merged
jsnajdr merged 5 commits into
WordPress:trunkfrom
yyppsk:fix/wp-build-translator-comments
Nov 24, 2026
Merged

jsnajdr merged 5 commits into
WordPress:trunkfrom
yyppsk:fix/wp-build-translator-comments

Conversation

@yyppsk

@yyppsk yyppsk commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What?

Closes #87161

Keeps translators: comments next to the gettext call they describe in the JavaScript that @wordpress/build produces, so wp i18n make-pot extracts them from the built files.

Why?

Rolldown's printer only keeps legal (/*!, @license), annotation (@__PURE__) and JSDoc (/** */) comments, and keeps even JSDoc comments only in some positions. So every /* translators: … */ and // translators: … comment is lost in two places:

  • the transpile step that writes each package's build-module and build, which the script and module bundles are built from (so the .min.js.map files don't have them either);
  • the route and widget bundles, which Rolldown builds from source.

On trunk, make-pot finds 0 translator comments for the 581 strings in build/routes, and 2 for the 3,519 strings in the unminified build/scripts files.

#87055 and #87183 also address this. #87055 rewrites translator comments to /** translators: … */ where they stand, and #87183 emits translation stubs for --no-script-debug builds. This PR is an alternative to #87055 for the regular build. The difference is that it also handles comments in positions where Rolldown drops even a JSDoc comment: before a conditional branch, before an array element, in an empty JSX expression container followed by the call, and before a return argument, a logical operand or a variable initializer. It doesn't overlap with #87183, which covers builds without unminified files.

How?

A new Rolldown plugin, translatorCommentsPlugin in packages/wp-build/lib/translator-comments-plugin.mjs:

  1. Parses the module with parseSync from rolldown/utils, so comment-like text in strings, template literals and regular expressions is never touched. The hook filter skips modules that don't contain translators, and a module with no translator comment returns before anything else is done.
  2. For each translator comment, finds the call make-pot would give it to: the first __(), _x(), _n() or _nx() call starting on the line the comment ends on or the next one, or the call the comment is in, before the first argument. A comment with no such call is left alone.
  3. If the comment directly precedes the call, or the statement that starts on the call's line, in a position Rolldown's printer keeps (a statement, a call argument, an object property value, a JSX attribute value or a template literal expression), it becomes a JSDoc comment in place. A // translators: comment becomes /** translators: … */.
  4. Otherwise it moves into the call, before the first argument: __( /** translators: … */ 'Draft' ). The printer always keeps a comment before a call argument and prints it on its own line, and make-pot reads the leading comments of a gettext call's arguments. A moved multi-line comment leaves a line break behind, so automatic semicolon insertion is unchanged.

Only comments are changed, so every *.min.js file stays byte-for-byte the same. The edits go through RolldownMagicString, so the source maps stay correct.

The plugin runs in the transpile step and in the unminified route (route.js, content.js) and widget (render.js, widget.js) builds. The script and module bundles are built from build-module, which now has the comments in positions the printer keeps, so they need no plugin of their own. The plugin only knows make-pot's rules, not anything Gutenberg-specific, so it works the same for any plugin built with @wordpress/build.

The worker bundles (wpWorkers) are built without the plugin, on purpose. A worker bundle is inlined into the package's worker-code module as a JSON string and runs from a Blob URL in its own global scope, so make-pot can't extract strings from it and no script translations are loaded for it: a gettext call in a worker can't be translated either way. Neither worker in Gutenberg (@wordpress/vips and @wordpress/video-conversion) depends on @wordpress/i18n or has a gettext call or translator comment in its sources or its built bundle.

Testing Instructions

  1. On trunk, run npm run build. Copy build/routes to .pot-test/before/routes, and the *.js files of build/scripts without the *.min.js files to .pot-test/before/scripts. Save the checksums of the minified files: find build -name '*.min.js' -print0 | sort -z | xargs -0 sha256sum > min-before.sha.
  2. Check out this branch, run npm run build and do the same into .pot-test/after and min-after.sha.
  3. diff min-before.sha min-after.sha prints nothing: all 162 *.min.js files are identical.
  4. With npm run wp-env-test start running, for each of before and after, and each of routes and scripts, run npm run wp-env-test -- run --env-cwd=wp-content/plugins/gutenberg/.pot-test/<before|after> cli php -d memory_limit=-1 /usr/local/bin/wp i18n make-pot <routes|scripts> <routes|scripts>.pot --ignore-domain (in Git Bash on Windows, prefix it with MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*').
  5. Count the #. translators: lines in each POT file with grep -c '^#\. translators:'. Compare with the table below.
  6. In .pot-test/after, check that these strings have their comments:
    • %s settings updated. (routes/experiments-home/stage.tsx) in routes.pot
    • Save as pending and Publishing… (editor), "%s" successfully created. (edit-post) in scripts.pot
    • Invalid and Draft (navigation-link/shared/invalid-draft-display.jsx, comments before conditional branches), Block (editor/src/components/sidebar/header.jsx, a comment in an empty JSX expression container) and n/j/Y (date-format-picker, a comment before an array element) in scripts.pot
  7. In build/scripts/edit-post/index.min.js.map, the embedded source of packages/edit-post/build-module/components/layout/index.mjs now has the /** translators: %s: Title of the created post or template, e.g: "Hello world". */ comment on the line before the "%s" successfully created. call.
  8. npm run test:unit -- packages/wp-build

Testing Instructions for Keyboard

Not applicable: build tooling only.

Screenshots or screencast

wp i18n make-pot --ignore-domain on the unminified build output:

Output Strings translators: comments before translators: comments after
build/routes 581 0 127
build/scripts (*.js without *.min.js) 3,519 2 604

The number of strings is the same before and after. As a cross-check, I ran make-pot on the .js and .jsx sources of packages/*/src and routes (make-pot doesn't scan .ts or .tsx files). Of the strings in both POT files, 278 have a translator comment in the source. All 278 have it in the build after this change, against 2 before. 5 more strings get a comment in the build that make-pot doesn't pick up in the source, because the comment is in an empty JSX expression container, such as Block above.

Use of AI Tools

Assisted with Claude Opus 5.5 for testing and verifying accuracy.

…cript

Rolldown's printer only keeps legal, annotation and JSDoc comments, and
keeps JSDoc comments only in some positions: before a statement, a call
argument, an object property value or a template literal expression. So
`/* translators: */` and `// translators:` comments were dropped from the
transpiled `build` and `build-module` files, which the script and module
bundles are built from, and from the route and widget bundles. `wp i18n
make-pot` then extracted the strings from the built files without them.

A new Rolldown plugin finds each translator comment that `make-pot` would
give to a `__()`, `_x()`, `_n()` or `_nx()` call, using the parser so text
in strings, template literals and regular expressions is left alone. When
the comment already sits where the printer keeps it, it becomes a JSDoc
comment in place. Otherwise, as before a conditional branch, an array
element, a `return` argument or in an empty JSX expression container, it
moves into the call, before its first argument, which the printer always
keeps and `make-pot` reads. Only comments change, so the minified output
stays byte-for-byte the same, and the edits go through a magic string so
the source maps stay correct.

The plugin runs in the transpile step and in the unminified route and
widget builds.
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yyppsk <pranjalpratapsingh@git.wordpress.org>
Co-authored-by: simison <simison@git.wordpress.org>
Co-authored-by: kraftbj <kraftbj@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the [Package] wp-build /packages/wp-build label Nov 20, 2026
Trunk dropped the Unreleased Bug Fixes entry for WordPress#75396 and its heading, which the merge had brought back. Remove that entry again and keep this pull request's entry in its own Bug Fixes section.
…e types

Replace the loose Node typedef, which allowed any property, with the ESTree types that rolldown/utils exports, and type the gettext calls as call expressions. This fixes the jsdoc/reject-any-type lint warning.
@yyppsk yyppsk changed the title [WIP] wp-build: Keep translator comments next to their calls in built JavaScript wp-build: Keep translator comments next to their calls in built JavaScript Nov 24, 2026
@yyppsk
yyppsk marked this pull request as ready for review November 24, 2026 07:09
@simison
simison requested review from jsnajdr and obenland November 24, 2026 10:24
@simison

simison commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

I appreciate the PR but why couldn't we keep these as improvements & comments on #87055 and work in a single thread instead? cc @obenland

@jsnajdr jsnajdr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is an unpleasant fix for something that should be fixed in Rolldown in the first place, but I'm afraid at the moment we can't do anything else.

The new Rolldown plugins converts all translators comments to JSDoc and also moves them to a place in the AST where Rolldown will preserve them (next to an expression).

Babel and webpack used to preserve all comments. The problem is that comments are not really a regular part of an AST, the parser needs to implement something on top of the regular JS parser to keep track of comments correctly. Babel did that work, and e.g. Prettier does that work, too. But the newer ESBuild and Rolldown tools probably don't bother implementing this; it makes the parser faster.

webpack method of bundling is also very friendly to preserving comments and other stuff in the sources. It keeps the original source intact and only "patches" the places where import and export statements are. It doesn't do a parse+print cycle on every source file.

Even ESBuild was already dropping some translators comments, although just a small percentage. Comments before statements or before JSX attributes were already lost:

// comment before a plain assignment
missingText = __( 'Select post' );
// comment before a return
return __( 'Custom' );
// comment before a JSX attribute
label={ __( 'More' ) }

Now Rolldown makes this much more serious, losing almost all comments.

This should be backported to WP 7.2, as I'm afraid we'll be losing translator comments there, too, on the shipped scripts.

@jsnajdr jsnajdr added Backport to WP 7.2 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta [Type] Regression Related to a regression in the latest release labels Nov 24, 2026
@jsnajdr
jsnajdr merged commit e540c01 into WordPress:trunk Nov 24, 2026
118 of 122 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Nov 24, 2026
@github-actions

Copy link
Copy Markdown

There was a conflict while trying to cherry-pick the commit to the wp/7.2 branch. Please resolve the conflict manually and create a PR to the wp/7.2 branch.

PRs to wp/7.2 are similar to PRs to trunk, but you should base your PR on the wp/7.2 branch instead of trunk.

# Checkout the wp/7.2 branch instead of trunk.
git checkout wp/7.2

# Create a new branch for your PR.
git checkout -b my-branch

# Cherry-pick the commit.
git cherry-pick e540c0180a99aa1502d9a88fc4df6bd228495e81

# Check which files have conflicts.
git status

# Resolve the conflict...
# Add the resolved files to the staging area.
git status
git add .
git cherry-pick --continue

# Push the branch to the repository
git push origin my-branch

# Create a PR and set the base to the wp/7.2 branch.
# See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request.

@Mamaduka Mamaduka removed the Backport to WP 7.2 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Nov 24, 2026
@Mamaduka

Copy link
Copy Markdown
Member

Based on my checks, the wp/7.2 branch still uses ESBuild. Migration (#85476) should've landed after the release branch was created.

@jsnajdr

jsnajdr commented Nov 25, 2026

Copy link
Copy Markdown
Member

Based on my checks, the wp/7.2 branch still uses ESBuild.

Thanks, that's good news 🙂

@yyppsk

yyppsk commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @simison, fair point. I started this separately because the remaining cases (ternary branches, array elements, empty JSX containers) needed the comment moved into the call rather than converted in place, which was a different approach from #87055. In hindsight, suggesting it on #87055 first would have been better. Thanks @obenland for identifying the problem and the JSDoc approach, and @jsnajdr for the review.

@jsnajdr

jsnajdr commented Nov 26, 2026

Copy link
Copy Markdown
Member

This is an unpleasant fix for something that should be fixed in Rolldown in the first place

I filed an OXC issue that would help solve our problem upstream: oxc-project/oxc#30338

benitoalba pushed a commit to benitoalba/gutenberg that referenced this pull request Nov 29, 2026
Resolve the conflict in packages/wp-build/CHANGELOG.md by keeping
trunk's Unreleased section and listing the WordPress#86766 entry under its
Bug Fixes heading, next to WordPress#87189.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EisotksEYkwb8knGHGSXxP
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
…cript (WordPress#87189)

* wp-build: Keep translator comments next to their calls in built JavaScript

Rolldown's printer only keeps legal, annotation and JSDoc comments, and
keeps JSDoc comments only in some positions: before a statement, a call
argument, an object property value or a template literal expression. So
`/* translators: */` and `// translators:` comments were dropped from the
transpiled `build` and `build-module` files, which the script and module
bundles are built from, and from the route and widget bundles. `wp i18n
make-pot` then extracted the strings from the built files without them.

A new Rolldown plugin finds each translator comment that `make-pot` would
give to a `__()`, `_x()`, `_n()` or `_nx()` call, using the parser so text
in strings, template literals and regular expressions is left alone. When
the comment already sits where the printer keeps it, it becomes a JSDoc
comment in place. Otherwise, as before a conditional branch, an array
element, a `return` argument or in an empty JSX expression container, it
moves into the call, before its first argument, which the printer always
keeps and `make-pot` reads. Only comments change, so the minified output
stays byte-for-byte the same, and the edits go through a magic string so
the source maps stay correct.

The plugin runs in the transpile step and in the unminified route and
widget builds.

* Changelog: Add wp-build entry for translator comments

* Changelog: Move wp-build translator comments entry under Bug Fixes

Trunk dropped the Unreleased Bug Fixes entry for WordPress#75396 and its heading, which the merge had brought back. Remove that entry again and keep this pull request's entry in its own Bug Fixes section.

* wp-build: Type translator comments plugin nodes with Rolldown's ESTree types

Replace the loose Node typedef, which allowed any property, with the ESTree types that rolldown/utils exports, and type the gettext calls as call expressions. This fixes the jsdoc/reject-any-type lint warning.

Co-authored-by: yyppsk <pranjalpratapsingh@git.wordpress.org>
Co-authored-by: simison <simison@git.wordpress.org>
Co-authored-by: kraftbj <kraftbj@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] wp-build /packages/wp-build [Type] Regression Related to a regression in the latest release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp-build: Translator comments are missing from built JavaScript

4 participants