WordPress.org

Make WordPress Core

Opened 3 weeks ago

Last modified 4 days ago

#50749 new defect (bug)

wp_set_script_translations() ignores failure when called before script is registered.

Reported by: tellyworth Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4
Component: I18N Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

wp_set_script_translations() only works if it's called after the script has already been registered. The doc page confirms this: https://developer.wordpress.org/reference/functions/wp_set_script_translations/. The function does trigger _doing_it_wrong when called prior to wp_register_scripts, but fails silently when called before the specific script has been registered.

In investigating js translation issues in the plugin directory, we discovered that it is very common for developers to incorrectly call wp_set_script_translations() too early. As a result, js translations are not correctly loaded for many plugins.

This would be easily fixed by having wp_set_script_translations() trigger _doing_it_wrong when $wp_scripts->set_translations() returns false.

Change History (3)

#1 @swissspidy
3 weeks ago

This would be easily fixed by having wp_set_script_translations() trigger _doing_it_wrong when $wp_scripts->set_translations() returns false.

Note that $wp_scripts->set_translations() will also return false if the domain is not a string. That would be an easy checck to add to wp_set_script_translations as well.

This ticket was mentioned in PR #466 on WordPress/wordpress-develop by donmhico.


4 days ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

Trigger _doing_it_wrong() when passed $domain is not a string and if $wp_scripts->set_translations() returns false.

Trac ticket: https://core.trac.wordpress.org/ticket/50749

#3 @donmhico
4 days ago

@tellyworth would be great if you can check if my patch corrects the issue? Also the $message passed to _doing_it_wrong could be improved. Thank you!

Note: See TracTickets for help on using tickets.