Skip to content

Commit 3a0a32d

Browse files
committed
Script Modules: Force-print wp-i18n before translation inline scripts.
1 parent 5b592e7 commit 3a0a32d

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/wp-includes/class-wp-script-modules.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,6 @@ public function print_script_module_translations(): void {
387387

388388
$set_locale_data_js_function = <<<'JS'
389389
( domain, translations ) => {
390-
if ( ! window.wp?.i18n?.setLocaleData ) {
391-
return;
392-
}
393390
const localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
394391
localeData[""].domain = domain;
395392
wp.i18n.setLocaleData( localeData, domain );
@@ -414,6 +411,12 @@ public function print_script_module_translations(): void {
414411
);
415412
$script_id = "wp-script-module-translation-data-{$id}";
416413
$output .= "\n//# sourceURL=" . rawurlencode( $script_id );
414+
415+
// Ensure wp-i18n is printed; the inline script below relies on wp.i18n.setLocaleData().
416+
if ( ! wp_script_is( 'wp-i18n', 'done' ) ) {
417+
wp_scripts()->do_items( array( 'wp-i18n' ) );
418+
}
419+
417420
wp_print_inline_script_tag( $output, array( 'id' => $script_id ) );
418421
}
419422
}

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,23 @@ private function get_registered_script_modules( WP_Script_Modules $script_module
19861986
return $registered_property->getValue( $script_modules );
19871987
}
19881988

1989+
/**
1990+
* Returns the inline text of the first SCRIPT tag with the given id, or null if not found.
1991+
*
1992+
* @param string $markup The HTML markup to search.
1993+
* @param string $id The id attribute to match.
1994+
* @return string|null The inline script text, or null if no matching tag was found.
1995+
*/
1996+
private function get_inline_script_text( string $markup, string $id ): ?string {
1997+
$processor = new WP_HTML_Tag_Processor( $markup );
1998+
while ( $processor->next_tag( 'SCRIPT' ) ) {
1999+
if ( $id === $processor->get_attribute( 'id' ) ) {
2000+
return $processor->get_modifiable_text();
2001+
}
2002+
}
2003+
return null;
2004+
}
2005+
19892006
/**
19902007
* Data provider.
19912008
*
@@ -2741,10 +2758,8 @@ static function ( $relative, $src, $is_module ) use ( &$filtered ) {
27412758
$this->assertTrue( $filter_args['is_module'], 'Filter should receive $is_module=true for script modules.' );
27422759
}
27432760

2744-
$processor = new WP_HTML_Tag_Processor( $output );
2745-
$this->assertTrue( $processor->next_tag( 'SCRIPT' ), 'Output should contain a SCRIPT tag.' );
2746-
$script_text = $processor->get_modifiable_text();
2747-
$this->assertStringContainsString( 'wp-script-module-translation-data-test-module', $script_text, 'Translation inline script should be printed with the expected ID.' );
2761+
$script_text = $this->get_inline_script_text( $output, 'wp-script-module-translation-data-test-module' );
2762+
$this->assertNotNull( $script_text, 'Translation inline script should be printed with the expected ID.' );
27482763
$this->assertStringContainsString( 'wp.i18n.setLocaleData', $script_text, 'Output should call wp.i18n.setLocaleData().' );
27492764
$this->assertStringContainsString( 'Hola', $script_text, 'Output should contain the translated string.' );
27502765
}
@@ -2808,10 +2823,8 @@ static function ( $relative, $src, $is_module ) use ( &$filtered ) {
28082823
$this->assertTrue( $filter_args['is_module'], 'Filter should receive $is_module=true for script modules.' );
28092824
}
28102825

2811-
$processor = new WP_HTML_Tag_Processor( $output );
2812-
$this->assertTrue( $processor->next_tag( 'SCRIPT' ), 'Output should contain a SCRIPT tag.' );
2813-
$script_text = $processor->get_modifiable_text();
2814-
$this->assertStringContainsString( 'wp-script-module-translation-data-dep-module', $script_text, 'Dependency module translations should be printed.' );
2826+
$script_text = $this->get_inline_script_text( $output, 'wp-script-module-translation-data-dep-module' );
2827+
$this->assertNotNull( $script_text, 'Dependency module translations should be printed.' );
28152828
$this->assertStringContainsString( 'Mundo', $script_text, 'Output should contain the dependency translation.' );
28162829
}
28172830

0 commit comments

Comments
 (0)