Skip to content

Script Modules: Fix missing translations of script modules - #84651

Draft
jorgefilipecosta wants to merge 8 commits into
trunkfrom
fix/script-module-translations
Draft

jorgefilipecosta wants to merge 8 commits into
trunkfrom
fix/script-module-translations

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Core PR: WordPress/wordpress-develop#14260
Trac ticket: https://core.trac.wordpress.org/ticket/66591
Needed by #84643 and #83368.

Right now some strings of script modules show in English even when the language packs translate them:

  • WordPress only prints the translations of the enqueued script modules and their dependencies. A script module that only a classic script imports, through its module_dependencies, loads with no translations. Actions API: Extract the core actions into the @wordpress/core-actions script modules #84643 moves the DataViews core actions into script modules that wp-editor imports this way, and Fields API #83368 does the same for the core fields. Their strings are translated today because they are bundled into wp-editor, so without a fix they would go back to English.
  • The script modules that the plugin serves look their translations up in languages/plugins/{locale}-{md5}.json, but the plugin language packs name these files gutenberg-{locale}-{md5}.json. This already affects released screens, e.g. most of Settings > Connectors is in English.
  • The full-page template of wp-build never prints the translations of script modules, so full-page screens like the extensible site editor show the strings of their script modules in English.

This PR:

  • Replaces the hooks of WP_Script_Modules::print_script_module_translations() with a function that also prints the translations of the script modules that classic scripts depend on, and of their dependencies. It only uses public methods, and prints each script module once whether or not WordPress includes the fix from the core PR.
  • Makes gutenberg_override_translation_file() use the plugin translation files for the script modules served from the plugin's build/ folder too, as it does for the plugin's scripts.
  • Makes the full-page template of wp-build fire admin_print_footer_scripts, as wp-admin/admin-footer.php does, instead of calling the footer printers one by one.
  • Adds a test plugin with an e2e test for the first change, and PHPUnit tests for the first two.

Some notes:

  • The Gutenberg function works like the core method with the fix, with one difference: WordPress skips an enqueued script module whose dependencies are not all registered, since it can't load, while the Gutenberg function still prints its translations, which go unused. I considered porting the dependency sorting of the core class, but it's private and the difference has no visible effect. So I opted for a plain walk over get_registered().
  • WordPress hooks all the footer printers to admin_print_footer_scripts since 6.7, which the template already requires for print_script_module_data(). Besides the translations, the full-page screens now also get the late styles and the live regions of @wordpress/a11y, and print the script modules before the classic scripts, like the other admin pages.
  • https://core.trac.wordpress.org/ticket/60234 discusses loading the translations of dynamically imported script modules on import, instead of printing them with the page. Both PRs follow what WordPress does today for the dynamic dependencies of enqueued script modules.
  • I checked Actions API: Extract the core actions into the @wordpress/core-actions script modules #84643 merged with this PR, in Portuguese (Portugal), with language pack files that move the strings of the core actions from build/scripts/editor/index.js to the build/modules/core-actions/ files, as GlotPress will once that PR ships. "Export as JSON" shows as "Exportar como JSON" in the pattern actions of the site editor only with the first two changes, and in the extensible site editor only with all three. Without them, the post editor prints no translations for the @wordpress/core-actions script modules either.
  • I compared the HTML of 15 admin and front-end pages before and after this PR, in English and in Portuguese, on WordPress trunk, and checked the main ones again with a classic theme and on WordPress 7.0.7. The only differences are the new translation scripts and, in the extensible site editor, the footer order and the live regions. No page logs new PHP notices or console errors.

Before

The test page of the e2e test, where one line comes from an enqueued script module and the other from a script module that only a classic script imports:

The test page on trunk: the line written by the enqueued script module ends with "(translated)", the line written by the script module that a classic script imports does not.

Settings > Connectors, in Portuguese (Portugal):

Settings > Connectors in Portuguese on trunk: the title, the description, the AI plugin card and the note at the bottom are in English.

The extensible site editor, in Portuguese (Portugal), where the back link at the top of the sidebar comes from a script module:

The extensible site editor in Portuguese on trunk: the back link at the top of the sidebar reads "Dashboard".

After

The test page with this PR: both lines end with "(translated)".

Settings > Connectors in Portuguese with this PR: all of the page is in Portuguese.

The extensible site editor in Portuguese with this PR: the back link at the top of the sidebar reads "Painel".

Testing Instructions

Go through the steps on trunk first, then on this branch after npm run build.

  1. Start the test environment with npm run wp-env-test start.

  2. Save this snippet as packages/e2e-tests/mu-plugins/module-translations-test.php, which the test environment loads as a must-use plugin. It registers a test/hello script module that only a classic script imports, gives it a translation, and makes it show __( 'Hello' ) in an alert on the Dashboard:

    <?php
    add_action( 'load-index.php', function () {
    	wp_register_script_module( 'test/hello', 'data:text/javascript,alert(wp.i18n.__("Hello"))', array(), null );
    	wp_register_script( 'test-classic', false, array( 'wp-i18n' ), false, array( 'in_footer' => true, 'module_dependencies' => array( 'test/hello' ) ) );
    	wp_add_inline_script( 'test-classic', 'addEventListener("DOMContentLoaded",()=>import("test/hello"))' );
    	wp_enqueue_script( 'test-classic' );
    } );
    add_filter( 'pre_load_script_translations', function ( $translations, $file, $handle ) {
    	return 'test/hello' === $handle ? '{"locale_data":{"messages":{"":{},"Hello":["Hello (translated)"]}}}' : $translations;
    }, 10, 3 );
    
  3. Open http://localhost:8889/wp-admin/. On trunk, the alert says "Hello". With this PR, it says "Hello (translated)".

  4. Install Portuguese (Portugal) and switch the admin user to it:

    npm run wp-env-test -- run cli wp language core install pt_PT
    npm run wp-env-test -- run cli wp language plugin install gutenberg pt_PT
    npm run wp-env-test -- run cli wp user meta update admin locale pt_PT
    
  5. Open http://localhost:8889/wp-admin/options-general.php?page=options-connectors-wp-admin. On trunk, the title, the description, the AI plugin card and the note at the bottom are in English. With this PR, all of the page is in Portuguese.

  6. Open http://localhost:8889/wp-admin/admin.php?page=site-editor-v2. On trunk, the back link at the top of the sidebar reads "Dashboard". With this PR, it reads "Painel".

  7. Delete the snippet and switch the admin user back to English with npm run wp-env-test -- run cli wp user meta delete admin locale. Then run npm run test:unit:php:base -- --filter 'Gutenberg_Script_Module_Translations_Test|Override_Translation_File_Test|Tests_Fields_API' and npm run test:e2e -- test/e2e/specs/admin/script-module-translations.spec.js.

AI usage disclosure: fix and description drafted with AI assistance and reviewed by me.

@jorgefilipecosta jorgefilipecosta added [Type] Bug An existing feature does not function as intended Internationalization (i18n) Issues or PRs related to internationalization efforts [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps [Package] wp-build /packages/wp-build labels Oct 13, 2026
@github-actions github-actions Bot added the [Package] E2E Tests /packages/e2e-tests label Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

📦 Bundle size

Size Change: 0 B

Total Size: 8.56 MB

7ac0fba Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 893ff3b trunk % Change
timeToFirstByte 39 ms +8.97% -8.08% 35.95 ms +5.42% -3.62% 8.48%
largestContentfulPaint 68 ms +5.88% -8.82% 64 ms +6.25% -0% 6.25%
lcpMinusTtfb 27.75 ms +6.13% -9.01% 28.15 ms +15.1% -7.28% -1.42%
wpBeforeTemplate 20.46 ms +8.99% -4.59% 18.7 ms +7.91% -2.35% 9.41%
wpTemplate 14.37 ms +9.95% -5.78% 13.81 ms +4.85% -5.65% 4.06%
wpTotal 35.64 ms +9.26% -7.77% 32.84 ms +6.33% -4.23% 8.53%
wpMemoryUsage 7.70 MB +0% -0% 7.67 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 893ff3b trunk % Change
timeToFirstByte 33.65 ms +12.33% -8.47% 32.3 ms +9.91% -7.59% 4.18%
largestContentfulPaint 76 ms +2.63% -5.26% 72 ms +2.78% -5.56% 5.56%
lcpMinusTtfb 42.05 ms +1.9% -5.71% 38.25 ms +3.79% -1.7% 9.93%
wpBeforeTemplate 18.32 ms +19.92% -5.9% 17.49 ms +12.64% -5.83% 4.75%
wpTemplate 12.07 ms +7.62% -9.61% 11.05 ms +7.96% -2.81% 9.23%
wpTotal 30.83 ms +12.59% -9.21% 29.37 ms +10.73% -7.12% 4.97%
wpMemoryUsage 6.40 MB +0% -0% 6.36 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 893ff3b trunk % Change
mediaProcessingJpeg 241.07 ms +0.82% -2.98% 233.92 ms +1.84% -0.71% 3.06%
mediaProcessingAvif 3851.93 ms +0.91% -1.51% 3919.9 ms +0.64% -0.17% -1.73%
mediaProcessingJpegToAvif 2683.89 ms +0.12% -0.4% 2668.34 ms +1.05% -0.24% 0.58%

media-upload

Metric 893ff3b trunk % Change
jpegUploadProcessing 1424.89 ms +1.39% -0.75% 1425.62 ms +0.73% -0.57% -0.05%
pngUploadProcessing 192.79 ms +3.01% -3.04% 197.19 ms +7.39% -1.23% -2.23%
largeJpegUploadProcessing 1411.53 ms +1.03% -0.59% 1415.01 ms +1.33% -0.62% -0.25%
multipleImageUploadProcessing 1471.77 ms +1.22% -0.56% 1498.87 ms +0.64% -0.52% -1.81%

post-editor

Metric 893ff3b trunk % Change
serverResponse 293.61 ms +4.4% -8.79% 291.78 ms +1.97% -9.58% 0.63%
firstPaint 230.47 ms +41.26% -14.93% 265.69 ms +33.37% -26.63% -13.26%
domContentLoaded 642.54 ms +6.01% -1.48% 600.44 ms +3.41% -6.34% 7.01%
loaded 643.7 ms +6% -1.48% 601.46 ms +3.43% -6.3% 7.02%
firstContentfulPaint 387.03 ms +3.82% -15.88% 344.49 ms +9% -40.56% 12.35%
firstBlock 2120.57 ms +2.6% -1.41% 1972.7 ms +3.76% -0.83% 7.5%
type 15.56 ms +4.31% -7.71% 13.94 ms +0.72% -2.44% 11.62%
typeWithoutInspector 16.04 ms +3.18% -9.04% 15.12 ms +1.32% -1.65% 6.08%
typeWithTopToolbar 20.19 ms +12.09% -5.45% 17.49 ms +6.17% -8.23% 15.44%
typeContainer 6.88 ms +7.7% -3.92% 6.08 ms +8.39% -3.78% 13.16%
focus 91.68 ms +11.68% -3.42% 77.09 ms +5.15% -2% 18.93%
firstFocus 177.63 ms +0% -0% 167.68 ms +0% -0% 5.93%
selectAll 333.37 ms +5.13% -1.19% 326.75 ms +3.74% -4.8% 2.03%
undo 25.93 ms +6.86% -4.94% 23.68 ms +11.28% -5.07% 9.5%
listViewOpen 53.75 ms +4.84% -15.09% 46.67 ms +10.28% -6.15% 15.17%
inserterOpen 30.02 ms +5.26% -2.86% 26.93 ms +14.15% -5.05% 11.47%
inserterHover 9.54 ms +5.56% -3.67% 8.9 ms +4.16% -9.44% 7.19%
inserterSearch 7.28 ms +9.48% -12.23% 6.61 ms +3.78% -6.51% 10.14%
loadPatterns 475.54 ms +6.69% -1.83% 437.74 ms +6.34% -1.51% 8.64%
wpTotal 286.64 ms +4.44% -8.96% 284.16 ms +2.25% -9.65% 0.87%
wpMemoryUsage 14.75 MB +0% -0% 14.72 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 893ff3b trunk % Change
serverResponse 312.33 ms +4.82% -4.31% 327.09 ms +2.01% -1.42% -4.51%
firstPaint 213.76 ms +8.29% -4.72% 191.25 ms +24.24% -12.06% 11.77%
domContentLoaded 719.01 ms +4.07% -0.97% 698.21 ms +3.73% -0.75% 2.98%
loaded 720 ms +4.09% -0.94% 699.4 ms +3.72% -0.81% 2.95%
firstContentfulPaint 402.72 ms +7.69% -1.28% 401.96 ms +6.19% -5.29% 0.19%
firstBlock 3163.74 ms +1.49% -1.67% 3101.92 ms +0.8% -1.75% 1.99%
type 15.15 ms +3.63% -3.96% 15.8 ms +1.46% -1.14% -4.11%
navigate 95.2 ms +3.8% -2.41% 99.92 ms +16.86% -1.82% -4.72%
loadPatterns 1054.28 ms +16.26% -13.1% 1065.61 ms +13.07% -8.94% -1.06%
loadPages 1008.83 ms +2.52% -3.22% 1020.72 ms +2.06% -1.14% -1.16%
wpTotal 303.85 ms +4.87% -4.38% 318.22 ms +2.11% -1.32% -4.52%
wpMemoryUsage 12.77 MB +0% -0% 12.74 MB +0% -0% 0.24%
wpDbQueries 44 +2.27% -0% 44 +2.27% -0% 0%

7ac0fba Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Undo restores the post selection when another synced entity is loaded in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 6,
+   "offset": 6,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
moves selected text into another paragraph in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Array [
    Object {
      "attributes": Object {
-       "content": "First third",
+       "content": "First second third",
      },
      "name": "core/paragraph",
    },
    Object {
      "attributes": Object {
-       "content": "Fourth second fifth",
+       "content": "Fourth fifth",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:1167:41
should cut and paste individual blocks with collapsed selection in /test/e2e/specs/editor/various/copy-cut-paste.spec.js, passed after 1 failed attempt.
Error: expect(string).toMatchSnapshot(expected) failed

  @@ -1,3 +1,3 @@
   <!-- wp:paragraph -->
  -<p>2Cut - collapsed selection</p>
  +<p>2</p>
   <!-- /wp:paragraph -->


    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/copy-cut-paste.spec.js:45:49
should indent and outdent a list item when its content is fully selected in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 7
+ Received  + 7

  <!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
- <li>a<!-- wp:list -->
- <ul class="wp-block-list"><!-- wp:list-item -->
- <li>b</li>
+ <li></li>
+ <!-- /wp:list-item --></ul>
- <!-- /wp:list-item --></ul>
+ <!-- /wp:list -->
+
- <!-- /wp:list --></li>
- <!-- /wp:list-item --></ul>
+ <!-- wp:paragraph -->
+ <p>b</p>
- <!-- /wp:list -->
+ <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:819:52
should insert a line break on shift+enter in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 1
+ Received  + 1

  <!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
- <li>a<br></li>
+ <li><br></li>
  <!-- /wp:list-item --></ul>
  <!-- /wp:list -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:1210:52
should keep the list type when indenting an ordered list item in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 1
+ Received  + 1

@@ -1,8 +1,8 @@
  <!-- wp:list {"ordered":true} -->
  <ol class="wp-block-list"><!-- wp:list-item -->
- <li>one<!-- wp:list {"ordered":true} -->
+ <li>ne<!-- wp:list {"ordered":true} -->
  <ol class="wp-block-list"><!-- wp:list-item -->
  <li>two</li>
  <!-- /wp:list-item --></ol>
  <!-- /wp:list --></li>
  <!-- /wp:list-item --></ol>

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:916:52
should merge a following sibling list into the outermost list with Delete from a nested item (#77245) in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

@@ -8,11 +8,11 @@
          "innerBlocks": Array [
            Object {
              "innerBlocks": Array [
                Object {
                  "attributes": Object {
-                   "content": "b‸",
+                   "content": "b",
                  },
                  "name": "core/list-item",
                },
              ],
              "name": "core/list",
@@ -25,11 +25,11 @@
    },
    Object {
      "innerBlocks": Array [
        Object {
          "attributes": Object {
-           "content": "1",
+           "content": "‸1",
          },
          "innerBlocks": Array [
            Object {
              "innerBlocks": Array [
                Object {

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:2854:41
should replace selected non-breaking spaces from the toolbar in /test/e2e/specs/editor/various/rich-text.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "one two three",
+       "content": "e two three",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/rich-text.spec.js:1050:41
should replace the selection across blocks with an inline image in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

@@ -1,9 +1,9 @@
  Array [
    Object {
      "attributes": Object {
-       "content": "ab",
+       "content": "b",
      },
      "name": "core/paragraph",
    },
    Object {
      "attributes": Object {

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:2421:41
should update the URL from the last navigation if only varies in the URL fragment in /test/e2e/specs/interactivity/router-navigate.spec.ts, passed after 1 failed attempt.
Error: expect(locator).toHaveText(expected) failed

Locator:  getByTestId('title')
Expected: "Link 1"
Received: "Main"
Timeout:  5000ms

Call log:
  - Expect "toHaveText" getByTestId('title') with timeout 5000ms
  - waiting for getByTestId('title')
    14 × locator resolved to <h2 data-testid="title">Main</h2>
       - unexpected value "Main"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigate.spec.ts:160:25

7ac0fba Run

…ssic scripts depend on

`WP_Script_Modules::print_script_module_translations()` only prints the translations of the enqueued script modules and their dependencies, so a script module that only a classic script imports through its `module_dependencies` loads with no translations. Its hooks are now replaced on `after_setup_theme`, right after WordPress adds them, with `gutenberg_print_script_module_translations()`, which also prints the translations of those script modules and of their dependencies. It only uses public methods, and prints each script module once whether or not WordPress includes the fix. A hook that was removed stays removed.

The tests port the core cases, compare the output for the enqueued script modules with the one of WordPress, and cover the field modules that the Fields API adds to the `module_dependencies` of `wp-editor`.
…script modules

`gutenberg_override_translation_file()` only prepends the plugin slug to the translation files of the `wp-*` scripts. So the script modules that the plugin serves from its `build/` folder, e.g. from `build/modules/` and `build/routes/`, looked their translations up in `languages/plugins/{locale}-{md5}.json`, while the plugin language packs name these files `gutenberg-{locale}-{md5}.json`, and their strings stayed in English. These script modules now get the plugin translation file too.
…-page template

The full-page template called the footer printers one by one, so it never printed the translations of the script modules, which WordPress prints on `admin_print_footer_scripts` since 7.0, and the strings of the script modules of full-page screens, like the extensible site editor, stayed in English. The footer now fires `admin_print_footer_scripts`, as `wp-admin/admin-footer.php` does. That prints the import map, the footer scripts, the script modules and their data and translations, in the order of the other admin pages, along with the late styles and the live regions of `@wordpress/a11y`, which the template missed too. WordPress hooks all of these printers to the action since 6.7, which the template already requires for `print_script_module_data()`.
…dules that classic scripts import

The "Gutenberg Test Script Module Translations" test plugin adds a Tools page with two lines, each one written by a script module: one that the page enqueues, and one that only a classic script imports, declared in its `module_dependencies`. The plugin ships English translation files for both script modules, so each line ends with "(translated)" when its translations are printed, even on a site in English. Without the fix, only the first line is translated.
@jorgefilipecosta
jorgefilipecosta force-pushed the fix/script-module-translations branch from 3603f48 to 058ab3e Compare October 27, 2026 21:45

This branch has not been deployed

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

Labels

[Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps Internationalization (i18n) Issues or PRs related to internationalization efforts [Package] E2E Tests /packages/e2e-tests [Package] wp-build /packages/wp-build [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant