Skip to content

babel-plugin-replace-textdomain: detect aliased imports from the i18n ESM module#48355

Open
jsnajdr wants to merge 4 commits intotrunkfrom
remove/i18n-alt-names
Open

babel-plugin-replace-textdomain: detect aliased imports from the i18n ESM module#48355
jsnajdr wants to merge 4 commits intotrunkfrom
remove/i18n-alt-names

Conversation

@jsnajdr
Copy link
Copy Markdown
Member

@jsnajdr jsnajdr commented Apr 28, 2026

Proposed changes

Enhance the babel-plugin-replace-textdomain plugin so that for every function call, it checks if the identifier is coming from the @wordpress/i18n import, even with aliased name:

import { __ as __45 } from '@wordpress/i18n';

__45( 'Sort ascending' );

This "real" name is then checked agains the functions object instead of the alias. Thanks to this, we can reliably detect also the aliased __45 calls, under any name, and can avoid the much less elegant generateI18nVariants helper.

Related product discussion/links

p1774956551856089-slack-C05Q5HSS013

Does this pull request change what data or activity we track or use?

No.

Testing instructions

How I tested this:

  • cd projects/packages/newsletter (many other packages will do, but I used this one)
  • run pnpm build-js
  • check the built file in build/newsletter.js and search for Sort ascending. That's a translated string from the dataviews package. Verify that it has domain appended: the call should be __( 'Sort ascending', 'jetpack-newsletter' ).
  • if the domain was missing, i.e., the call would be the original __( 'Sort ascending' ), that would mean that the Babel plugin is failing to do its job, no matter the method.

@jsnajdr jsnajdr requested a review from a team as a code owner April 28, 2026 15:21
@jsnajdr jsnajdr self-assigned this Apr 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the remove/i18n-alt-names branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack remove/i18n-alt-names
bin/jetpack-downloader test jetpack-mu-wpcom-plugin remove/i18n-alt-names

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Apr 28, 2026
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control Bot commented Apr 28, 2026

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/js-packages/babel-plugin-replace-textdomain/src/index.js 70/70 (100.00%) 0.00% 0 💚

Full summary · PHP report · JS report

Copy link
Copy Markdown
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

I was concerned that this might negatively affect build time, since it's checking the scope of every non-i18n-name call, but some quick tests showed no noticable difference. 👍

let funcName = calleeName;
if ( ! Object.hasOwn( functions, funcName ) ) {
return;
funcName = resolveI18nAlias( path, calleeName );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this do the right thing for the code path via the true case on line 76? e.g. code like

import { __ as __alias } from '@wordpress/i18n';
someObj.__alias( '???' );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, this case was indeed buggy. We need an additional check that the callee is not a member expression. Fixed, together with two new unit tests.

Comment thread projects/js-packages/babel-plugin-replace-textdomain/src/index.js
@jsnajdr jsnajdr force-pushed the remove/i18n-alt-names branch from 10fa5cc to 87b5331 Compare April 29, 2026 07:16
@jsnajdr
Copy link
Copy Markdown
Member Author

jsnajdr commented Apr 29, 2026

I was concerned that this might negatively affect build time, since it's checking the scope of every non-i18n-name call, but some quick tests showed no noticable difference. 👍

Thanks for verifying this. Analyzing the scopes and building the graph is probably already requested by other plugins that are a part of the Babel config, it's a commonly used feature. So there's no extra new work. And the lookups should be fast and cheap.

Copy link
Copy Markdown
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

I was going to suggest adding something to the readme about support for aliased imports, and when I checked to see what the readme currently says I note that the module isn't really WordPress-specific. It advertises itself as simply handling "gettext-style function calls".

That makes me wonder whether we should make the @wordpress/i18n import name configurable (still defaulting to @wordpress/i18n, of course) in case someone has reason to use a different import. That should be fairly straightforward: check something like opts.i18nmodule, and then pass it through to resolveI18nAlias instead of using a module-global.

OTOH, if you really don't want to, that could be done in a followup PR.

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

Labels

[JS Package] Babel Plugin Replace Textdomain [JS Package] Webpack Config RNA [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants