Make WordPress Core

Changeset 50259


Ignore:
Timestamp:
02/08/2021 11:49:33 PM (4 years ago)
Author:
noisysocks
Message:

Fix wp.i18n.isRTL()

Fixes a bug causing wp.i18n.isRTL() to return false in RTL langauges by manually
loading the translated 'ltr' string for the i18n dependency. This ports over an
identical fix that was made in Gutenberg.

Fixes #52441.
Props @jonsurrell @youknowriad.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/package-lock.json

    r50235 r50259  
    39323932                        "unbzip2-stream": "^1.3.3",
    39333933                        "ws": "^7.2.3"
     3934                    },
     3935                    "dependencies": {
     3936                        "devtools-protocol": {
     3937                            "version": "0.0.818844",
     3938                            "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.818844.tgz",
     3939                            "integrity": "sha512-AD1hi7iVJ8OD0aMLQU5VK0XH9LDlA1+BcPIgrAxPfaibx2DbWucuyOhc4oyQCbnvDDO68nN6/LcKfqTP343Jjg==",
     3940                            "dev": true
     3941                        }
    39343942                    }
    39353943                },
     
    80578065            "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz",
    80588066            "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==",
    8059             "dev": true
    8060         },
    8061         "devtools-protocol": {
    8062             "version": "0.0.818844",
    8063             "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.818844.tgz",
    8064             "integrity": "sha512-AD1hi7iVJ8OD0aMLQU5VK0XH9LDlA1+BcPIgrAxPfaibx2DbWucuyOhc4oyQCbnvDDO68nN6/LcKfqTP343Jjg==",
    80658067            "dev": true
    80668068        },
  • trunk/src/wp-includes/script-loader.php

    r50155 r50259  
    267267        if ( in_array( 'wp-i18n', $dependencies, true ) ) {
    268268            $scripts->set_translations( $handle );
     269        }
     270
     271        // Manually set the text direction localization after wp-i18n is
     272        // printed. This ensures that wp.i18n.isRTL() returns true in RTL
     273        // languages. We cannot use $scripts->set_translations( 'wp-i18n' ) to
     274        // do this because WordPress prints a script's translations *before*
     275        // printing the script, which means, in the case of wp-i18n, that
     276        // wp.i18n.setLocaleData() is called before wp.i18n is defined.
     277        if ( 'wp-i18n' === $handle ) {
     278            $ltr    = _x( 'ltr', 'text direction', 'default' );
     279            $script = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] } );", $ltr );
     280            $scripts->add_inline_script( $handle, $script, 'after' );
    269281        }
    270282    }
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r50137 r50259  
    724724        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/hooks{$suffix}.js' id='wp-hooks-js'></script>\n";
    725725        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/i18n{$suffix}.js' id='wp-i18n-js'></script>\n";
     726        $expected .= "<script type='text/javascript' id='wp-i18n-js-after'>\n";
     727        $expected .= "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } );\n";
     728        $expected .= "</script>\n";
    726729        $expected .= "<script type='text/javascript' id='wp-a11y-js-translations'>\n";
    727730        $expected .= "( function( domain, translations ) {\n";
Note: See TracChangeset for help on using the changeset viewer.