From 385c29b49af5a667fa876d29c127eabd859dc696 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 8 Feb 2021 23:49:33 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@50259 602fd350-edb4-49c9-b593-d223f7449a82 --- package-lock.json | 14 ++++++++------ src/wp-includes/script-loader.php | 12 ++++++++++++ tests/phpunit/tests/dependencies/scripts.php | 3 +++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef2de62c43..fa9476cd38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3931,6 +3931,14 @@ "tar-fs": "^2.0.0", "unbzip2-stream": "^1.3.3", "ws": "^7.2.3" + }, + "dependencies": { + "devtools-protocol": { + "version": "0.0.818844", + "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.818844.tgz", + "integrity": "sha512-AD1hi7iVJ8OD0aMLQU5VK0XH9LDlA1+BcPIgrAxPfaibx2DbWucuyOhc4oyQCbnvDDO68nN6/LcKfqTP343Jjg==", + "dev": true + } } }, "rimraf": { @@ -8058,12 +8066,6 @@ "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", "dev": true }, - "devtools-protocol": { - "version": "0.0.818844", - "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.818844.tgz", - "integrity": "sha512-AD1hi7iVJ8OD0aMLQU5VK0XH9LDlA1+BcPIgrAxPfaibx2DbWucuyOhc4oyQCbnvDDO68nN6/LcKfqTP343Jjg==", - "dev": true - }, "diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index ce4c99e2ea..cde954418b 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -267,6 +267,18 @@ function wp_default_packages_scripts( $scripts ) { if ( in_array( 'wp-i18n', $dependencies, true ) ) { $scripts->set_translations( $handle ); } + + // Manually set the text direction localization after wp-i18n is + // printed. This ensures that wp.i18n.isRTL() returns true in RTL + // languages. We cannot use $scripts->set_translations( 'wp-i18n' ) to + // do this because WordPress prints a script's translations *before* + // printing the script, which means, in the case of wp-i18n, that + // wp.i18n.setLocaleData() is called before wp.i18n is defined. + if ( 'wp-i18n' === $handle ) { + $ltr = _x( 'ltr', 'text direction', 'default' ); + $script = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] } );", $ltr ); + $scripts->add_inline_script( $handle, $script, 'after' ); + } } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index cfc3540f84..2b6778954e 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -723,6 +723,9 @@ JS; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; + $expected .= "\n"; $expected .= "