Make WordPress Core

Changeset 52937


Ignore:
Timestamp:
03/15/2022 03:57:57 PM (15 months ago)
Author:
ocean90
Message:

I18N, Script Loader: Don't register empty locale data objects.

For wp.i18n the library Jed was initially used which was throwing an error if a domain was not registered but used in a translate function. Later, the library was replaced by Tannin which no longer requires the domain to be registered and thus we can avoid printing an empty-ish translations script that doesn't add any translations.

Props jsnajdr.
Fixes #55250.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r52610 r52937  
    600600
    601601        if ( ! $json_translations ) {
    602             // Register empty locale data object to ensure the domain still exists.
    603             $json_translations = '{ "locale_data": { "messages": { "": {} } } }';
     602            return false;
    604603        }
    605604
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r52010 r52937  
    741741        $expected .= "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } );\n";
    742742        $expected .= "</script>\n";
    743         $expected .= "<script type='text/javascript' id='wp-a11y-js-translations'>\n";
    744         $expected .= "( function( domain, translations ) {\n";
    745         $expected .= "  var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;\n";
    746         $expected .= "  localeData[\"\"].domain = domain;\n";
    747         $expected .= "  wp.i18n.setLocaleData( localeData, domain );\n";
    748         $expected .= "} )( \"default\", { \"locale_data\": { \"messages\": { \"\": {} } } } );\n";
    749         $expected .= "</script>\n";
    750743        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/a11y{$suffix}.js' id='wp-a11y-js'></script>\n";
    751744        $expected .= "<script type='text/javascript' src='http://example2.com' id='test-example2-js'></script>\n";
     
    995988    /**
    996989     * @ticket 45103
     990     * @ticket 55250
    997991     */
    998992    public function test_wp_set_script_translations_when_translation_file_does_not_exist() {
     
    1002996
    1003997        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    1004         $expected .= str_replace(
    1005             array(
    1006                 '__DOMAIN__',
    1007                 '__HANDLE__',
    1008                 '__JSON_TRANSLATIONS__',
    1009             ),
    1010             array(
    1011                 'admin',
    1012                 'test-example',
    1013                 '{ "locale_data": { "messages": { "": {} } } }',
    1014             ),
    1015             $this->wp_scripts_print_translations_output
    1016         );
    1017998        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='test-example-js'></script>\n";
    1018999
Note: See TracChangeset for help on using the changeset viewer.