#55250 closed defect (bug) (fixed)
Avoid sending empty script translations
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.0 | Priority: | normal |
| Severity: | normal | Version: | 5.0 |
| Component: | I18N | Keywords: | has-patch |
| Focuses: | Cc: |
Description
For every JS script enqueued, we generate an empty-ish translations script that doesn't add any translations:
<script id='react-js-translations'>
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "default", { "locale_data": { "messages": { "": {} } } } );
</script>
The only reason for that is to ensure that the wp.i18n library has a record for the domain in question (here "default"). But the wp.i18n library doesn't need that for __( 'foo', 'domain' ) to work, and it never needed.
There might have been some confusion about the Jed library, whose gettext methods really would throw an exception if the domain wasn't initialized, but 1) wp.i18n always caught the exception and returned the original string intact, and 2) it's been using tannin instead of Jed for close to 4 years.
This patch removes these redundant scripts. On a big page (like wp-admin/post.php for block editor on a site with many plugins), there would be around 90 of them.
Change History (10)
This ticket was mentioned in PR #2354 on WordPress/wordpress-develop by jsnajdr.
4 years ago
#1
swissspidy commented on PR #2354:
4 years ago
#2
Note that the two unit tests Tests_Dependencies_Scripts::test_wp_add_inline_script_before_after_concat_with_core_dependency and Tests_Dependencies_Scripts::test_wp_set_script_translations_when_translation_file_does_not_exist need updating.
The change looks reasonable to me.
cc @ocean90 in case he has some reservations.
4 years ago
#3
The unit tests are now fixed 🎉 I also found the exact error that led to adding the empty translations in this comment: https://core.trac.wordpress.org/ticket/45103#comment:38
At that time (Nov 13 2018) we were still using Jed and when a domain was not registered, the i18n was logging an error to console (nothing else). What's a funny coincidence is that the `i18n` migration from Jed to Tannin was merged to Gutenberg on literally the next day 🙂
4 years ago
#4
At that time (Nov 13 2018) we were still using Jed and when a domain was not registered, the
i18nwas logging an error to console (nothing else). What's a funny coincidence is that the `i18n` migration from Jed to Tannin was merged to Gutenberg on literally the next day 🙂
Thanks for digging into this! If there's no error anymore we should remove the unnecessary output.
#7
@
4 years ago
- Owner set to ocean90
- Resolution set to fixed
- Status changed from new to closed
In 52937:
#8
@
4 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
This change appears to have unintended side effects, breaking JS on a lot of w.org sites for any core-related JS functionality. Seems like the wp JS object doesn't get instantiated. See https://meta.trac.wordpress.org/ticket/6195.
#9
@
4 years ago
- Resolution set to fixed
- Status changed from reopened to closed
Marking as fixed again. The research about the real cause is happing in #meta6195 and Gutenberg PR #39497.
3 years ago
#10
This was merged in https://core.trac.wordpress.org/changeset/52937.
Fixes https://core.trac.wordpress.org/ticket/55250
For every JS script enqueued, we generate an empty-ish translations script that doesn't add any translations:
<script id='react-js-translations'> ( function( domain, translations ) { var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; localeData[""].domain = domain; wp.i18n.setLocaleData( localeData, domain ); } )( "default", { "locale_data": { "messages": { "": {} } } } ); </script>The only reason for that is to ensure that the
wp.i18nlibrary has a record for the domain in question (here"default"). But thewp.i18nlibrary doesn't need that for__( 'foo', 'domain' )to work, and it never needed.There might have been some confusion about the Jed library, whose
gettextmethods really would throw an exception if the domain wasn't initialized, but 1)wp.i18nalways caught the exception and returned the original string intact, and 2) it's been usingtannininstead of Jed for close to 4 years.This patch removes these redundant scripts. On a big page (like
wp-admin/post.phpfor block editor on a site with many plugins), there would be 90 of them.