Repository navigation
I18N: Print translations for the script modules that scripts depend on #14260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3af54e5
c2f21ff
8951e00
6a7649e
3caf006
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,18 @@ class WP_Script_Modules { | |
| */ | ||
| private $modules_with_missing_dependencies = array(); | ||
|
|
||
| /** | ||
| * List of handles for scripts encountered which have missing script module dependencies. | ||
| * | ||
| * A handle is added to this list when it is discovered to have script module dependencies that are not | ||
| * registered. At this time, a warning is emitted with {@see _doing_it_wrong()}. The handle is then added to this | ||
| * list, so that duplicate warnings don't occur. | ||
| * | ||
| * @since 7.2.0 | ||
| * @var string[] | ||
| */ | ||
| private $scripts_with_missing_module_dependencies = array(); | ||
|
|
||
| /** | ||
| * Registers the script module if no script module with that script module | ||
| * identifier has already been registered. | ||
|
|
@@ -367,22 +379,36 @@ public function set_translations( string $id, string $domain = 'default', string | |
| } | ||
|
|
||
| /** | ||
| * Prints translations for all enqueued script modules. | ||
| * Prints translations for the enqueued script modules and for the script | ||
| * modules in the import map. | ||
| * | ||
| * Outputs inline `<script>` tags that call `wp.i18n.setLocaleData()` with | ||
| * the translated strings for each script module. This must run before | ||
| * the script modules execute. | ||
| * | ||
| * The translations are printed after the scripts in the footer. A script | ||
| * that imports a script module while it executes may evaluate the script | ||
| * module before its translations are printed. To avoid that, the script | ||
| * can use the 'defer' loading strategy, or import the script module once | ||
| * the document has been parsed. | ||
| * | ||
| * The translations of the enqueued script modules are printed first, | ||
| * followed by the translations of the script modules in the import map. | ||
| * When two script modules translate the same string of a text domain | ||
| * differently, the translation printed last is the one that is used. | ||
| * | ||
| * Auto-detects the text domain and translation path for each module from | ||
| * its source URL. Modules whose text domain or path differs from the | ||
| * defaults can opt into a specific domain/path via | ||
| * {@see WP_Script_Modules::set_translations()}. | ||
| * | ||
| * @since 7.0.0 | ||
| * @since 7.2.0 Translations of the script module dependencies ('module_dependencies') of scripts, and of their | ||
| * dependencies, are now printed. The translations of the enqueued script modules are now printed | ||
| * first, instead of after the translations of their dependencies. | ||
| */ | ||
| public function print_script_module_translations(): void { | ||
| // Collect all module IDs that will be on the page (enqueued + their dependencies). | ||
| $module_ids = $this->get_sorted_dependencies( $this->queue ); | ||
| $module_ids = $this->get_loadable_script_module_ids(); | ||
|
|
||
| $set_locale_data_js_function = <<<'JS' | ||
| ( domain, translations ) => { | ||
|
|
@@ -456,6 +482,10 @@ public function add_hooks() { | |
| * priority 10 via _wp_footer_scripts), but before the script modules | ||
| * execute. Script modules with type="module" are deferred by default, | ||
| * so inline translation scripts at priority 11 will execute before them. | ||
| * | ||
| * A script that is not deferred and imports a script module while it | ||
| * executes may evaluate that script module before these translations | ||
| * are printed. | ||
| */ | ||
| add_action( 'wp_footer', array( $this, 'print_script_module_translations' ), 21 ); | ||
| add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_translations' ), 11 ); | ||
|
|
@@ -634,7 +664,7 @@ public function print_import_map() { | |
| * Returns the import map array. | ||
| * | ||
| * @since 6.5.0 | ||
| * @since 7.0.0 Script module dependencies ('module_dependencies') of classic scripts are now included. | ||
| * @since 7.0.0 Script module dependencies ('module_dependencies') of scripts are now included. | ||
| * | ||
| * @global WP_Scripts $wp_scripts | ||
| * | ||
|
|
@@ -646,8 +676,8 @@ private function get_import_map(): array { | |
|
|
||
| $imports = array(); | ||
|
|
||
| // Identify script modules that are dependencies of classic scripts. | ||
| $classic_script_module_dependencies = array(); | ||
| // Identify script modules that are dependencies of scripts. | ||
| $script_module_dependencies = array(); | ||
| if ( $wp_scripts instanceof WP_Scripts ) { | ||
| $handles = array_merge( | ||
| $wp_scripts->queue, | ||
|
|
@@ -680,11 +710,14 @@ private function get_import_map(): array { | |
| if ( ! isset( $this->registered[ $id ] ) ) { | ||
| $missing_module_dependencies[] = $id; | ||
| } else { | ||
| $classic_script_module_dependencies[] = $id; | ||
| $script_module_dependencies[] = $id; | ||
| } | ||
| } | ||
|
|
||
| if ( count( $missing_module_dependencies ) > 0 ) { | ||
| if ( | ||
| count( $missing_module_dependencies ) > 0 && | ||
| ! in_array( $handle, $this->scripts_with_missing_module_dependencies, true ) | ||
| ) { | ||
| _doing_it_wrong( | ||
| 'WP_Scripts::add_data', | ||
| sprintf( | ||
|
|
@@ -696,6 +729,7 @@ private function get_import_map(): array { | |
| ), | ||
| '7.0.0' | ||
| ); | ||
| $this->scripts_with_missing_module_dependencies[] = $handle; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -710,8 +744,8 @@ private function get_import_map(): array { | |
| // Note: the script modules in $this->queue are not included in the importmap because they get printed as scripts. | ||
| $ids = array_unique( | ||
| array_merge( | ||
| $classic_script_module_dependencies, | ||
| array_keys( $this->get_dependencies( array_merge( $this->queue, $classic_script_module_dependencies ) ) ) | ||
| $script_module_dependencies, | ||
| array_keys( $this->get_dependencies( array_merge( $this->queue, $script_module_dependencies ) ) ) | ||
| ) | ||
| ); | ||
| foreach ( $ids as $id ) { | ||
|
|
@@ -723,6 +757,26 @@ private function get_import_map(): array { | |
| return array( 'imports' => $imports ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the IDs of the enqueued script modules and of the script modules in the import map. | ||
| * | ||
| * The script modules in the import map are the dependencies of the enqueued script modules, the script module | ||
| * dependencies ('module_dependencies') of scripts, and the dependencies of those. The enqueued script modules | ||
| * come first, followed by the script modules in the import map. | ||
| * | ||
| * @since 7.2.0 | ||
| * | ||
| * @return string[] List of script module IDs. | ||
| */ | ||
| private function get_loadable_script_module_ids(): array { | ||
| return array_unique( | ||
| array_merge( | ||
| $this->queue, | ||
| array_keys( $this->get_import_map()['imports'] ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a previous issue, it can be a follow-up optimization. I've realized
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the notice is now triggered once per script, so it does not repeat. |
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the list of script modules marked for enqueue. | ||
| * | ||
|
|
@@ -1014,21 +1068,13 @@ private function get_src( string $id ): string { | |
| * form `wp-script-module-data-{$module_id}`. | ||
| */ | ||
| public function print_script_module_data(): void { | ||
| $modules = array(); | ||
| foreach ( array_unique( $this->queue ) as $id ) { | ||
| if ( '@wordpress/a11y' === $id ) { | ||
| $this->a11y_available = true; | ||
| } | ||
| $modules[ $id ] = true; | ||
| } | ||
| foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) { | ||
| if ( '@wordpress/a11y' === $id ) { | ||
| $this->a11y_available = true; | ||
| } | ||
| $modules[ $id ] = true; | ||
| $module_ids = $this->get_loadable_script_module_ids(); | ||
|
|
||
| if ( in_array( '@wordpress/a11y', $module_ids, true ) ) { | ||
| $this->a11y_available = true; | ||
| } | ||
|
|
||
| foreach ( array_keys( $modules ) as $module_id ) { | ||
| foreach ( $module_ids as $module_id ) { | ||
| /** | ||
| * Filters data associated with a given Script Module. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides changing which modules get translations (the fix we need: queue + import map), this also changes the order they are printed (before: sorted, dependencies first / after: queue + import map).
Since every module calls
wp.i18n.setLocaleData()on the same domain, the last one printed wins when two modules translate the same string differently. For example:Considering
my-dialog's language-pack JSON has"Close": ["Fechar"], this is the before/after result:setLocaleData__( 'Close' )returnsmy-dialog,my-panelmy-panelmy-panel,my-dialogmy-dialogI don't think this happens with language packs: a string with the same context has one translation per project, so every module's JSON agrees. It takes a per-module override like the one above, or a custom path set via
set_translations(). So I'd be fine with documenting the new order in the docblock.Alternatively, we could considering adding back sorting (but differently). I see in 8951e00 that removing
get_sorted_dependencies()was intentional.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the main criterion here is that classic scripts and script modules should use the same algorithm for the order in which they are printed. And classic scripts are always sorted.
The commit message on 8951e00 says:
Yes, but that's the same behavior that classic scripts have. The sorting code eliminates unregistered dependencies. Also also apparently triggers an infinite loop for circular dependencies?!
If script modules behave differently, that's an accidental behavior that should not be preserved, but fixed. If there are really crashing circular dependencies, that's a topic for another patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, I documented the new order in the docblock of print_script_module_translations, including that the translation printed last is the one used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jsnajdr, I agree that is where we should get to. I did not sort in this PR because print_script_module_data never sorted. Sorting the shared list would stop printing the data of script modules that have an unregistered dependency, which is printed today, and it would not finish for circular dependencies between script modules that scripts import, which work today. I think it is better to fix get_sorted_dependencies first and then sort all the printers in a follow-up.