From 690b0cbf835f93477a30d863384428e69c75463d Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Sun, 6 Jan 2019 16:22:46 +0000 Subject: [PATCH] I18N/Script Loader: Support text domains other than "messages". The inline JavaScript added by `WP_Scripts::print_translations()` should check whether `locale_data.$text_domain` exists and fall back to `locale_data.messages` otherwise. Props swissspidy. See #45441. git-svn-id: https://develop.svn.wordpress.org/trunk@44403 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-scripts.php | 11 +- tests/phpunit/tests/dependencies/scripts.php | 134 ++++++++++++++----- 2 files changed, 105 insertions(+), 40 deletions(-) diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php index 26fbd6da9b..531bb3a165 100644 --- a/src/wp-includes/class.wp-scripts.php +++ b/src/wp-includes/class.wp-scripts.php @@ -546,10 +546,13 @@ class WP_Scripts extends WP_Dependencies { $json_translations = '{ "locale_data": { "messages": { "": {} } } }'; } - $output = '(function( translations ){' . - 'translations.locale_data.messages[""].domain = "' . $domain . '";' . - 'wp.i18n.setLocaleData( translations.locale_data.messages, "' . $domain . '" );' . - '})(' . $json_translations . ');'; + $output = <<\n%s\n\n", $output ); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 7e52adf91f..b1e8e6c8f8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -4,7 +4,9 @@ * @group scripts */ class Tests_Dependencies_Scripts extends WP_UnitTestCase { - var $old_wp_scripts; + protected $old_wp_scripts; + + protected $wp_scripts_print_translations_output; function setUp() { parent::setUp(); @@ -13,6 +15,17 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { remove_action( 'wp_default_scripts', 'wp_default_packages' ); $GLOBALS['wp_scripts'] = new WP_Scripts(); $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); + + $this->wp_scripts_print_translations_output = << +( function( domain, translations ) { + var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; + localeData[""].domain = domain; + wp.i18n.setLocaleData( localeData, domain ); +} )( "__DOMAIN__", __JSON_TRANSLATIONS__ ); + +JS; + $this->wp_scripts_print_translations_output .= "\n"; } function tearDown() { @@ -781,11 +794,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null ); wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'default', + file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -799,11 +819,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null ); wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'internationalized-plugin', + file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -817,11 +844,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null ); wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'internationalized-theme', + file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -835,11 +869,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null ); wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'admin', + file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -868,11 +909,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null ); wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'admin', + '{ "locale_data": { "messages": { "": {} } } }', + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -888,11 +936,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'test-example' ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'default', + file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -908,11 +963,18 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null ); - $expected = ""; - $expected .= "\n\n"; + $expected = "\n"; + $expected .= str_replace( + array( + '__DOMAIN__', + '__JSON_TRANSLATIONS__', + ), + array( + 'default', + file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), + ), + $this->wp_scripts_print_translations_output + ); $expected .= "\n"; $expected .= "\n";