Make WordPress Core

Opened 4 years ago

Last modified 7 weeks ago

#50749 new defect (bug)

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

Reported by: tellyworth's profile tellyworth Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 5.4
Component: I18N Keywords: has-unit-tests needs-patch
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 (6)

#1 @swissspidy
4 years 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 years ago
#2

  • 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 years 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!

#4 @swissspidy
2 years ago

  • Milestone changed from Awaiting Review to Future Release

#5 @swissspidy
10 months ago

Worth noting that wp_add_inline_script() doesn't emit a warning either.

Instead of silently failing we could also just make it work.

See #41990

#6 @swissspidy
7 weeks ago

  • Keywords needs-patch added; has-patch removed
Note: See TracTickets for help on using tickets.