From a10598acd06ac3e07045cbd107b9ed86279aae16 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 16 Dec 2018 22:23:34 +0000 Subject: [PATCH] I18N: Set translations for all WordPress packages scripts. Calls `wp_set_script_translations` for all packages scripts that have translations. Also correctly sets the domain on the translations. Props omarreiss, pento, ocean90. Merges [43878] to trunk. Fixes #45161. git-svn-id: https://develop.svn.wordpress.org/trunk@44239 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-scripts.php | 3 ++- src/wp-includes/script-loader.php | 19 ++++++++++++++++++- tests/phpunit/tests/dependencies/scripts.php | 20 +++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php index 42374815d0..e843233cbc 100644 --- a/src/wp-includes/class.wp-scripts.php +++ b/src/wp-includes/class.wp-scripts.php @@ -546,7 +546,8 @@ class WP_Scripts extends WP_Dependencies { } $output = '(function( translations ){' . - 'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' . + 'translations.locale_data.messages[""].domain = "' . $domain . '";' . + 'wp.i18n.setLocaleData( translations.locale_data.messages, "' . $domain . '" );' . '})(' . $json_translations . ');'; if ( $echo ) { diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 5699810ef4..6dd77f46b2 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -319,7 +319,7 @@ function wp_default_packages_scripts( &$scripts ) { 'html-entities' => array( 'wp-polyfill' ), 'i18n' => array( 'wp-polyfill' ), 'is-shallow-equal' => array( 'wp-polyfill' ), - 'keycodes' => array( 'lodash', 'wp-polyfill' ), + 'keycodes' => array( 'lodash', 'wp-polyfill', 'wp-i18n' ), 'list-reusable-blocks' => array( 'lodash', 'wp-api-fetch', @@ -361,11 +361,28 @@ function wp_default_packages_scripts( &$scripts ) { 'wordcount' => array( 'wp-polyfill' ), ); + $package_translations = array( + 'api-fetch' => 'default', + 'blocks' => 'default', + 'block-library' => 'default', + 'components' => 'default', + 'edit-post' => 'default', + 'editor' => 'default', + 'format-library' => 'default', + 'keycodes' => 'default', + 'list-reusable-blocks' => 'default', + 'nux' => 'default', + ); + foreach ( $packages_dependencies as $package => $dependencies ) { $handle = 'wp-' . $package; $path = "/wp-includes/js/dist/$package$suffix.js"; $scripts->add( $handle, $path, $dependencies, false, 1 ); + + if ( isset( $package_translations[ $package ] ) ) { + $scripts->set_translations( $handle, $package_translations[ $package ] ); + } } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 5f03e9acc2..7e52adf91f 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -783,8 +783,9 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; + 'translations.locale_data.messages[""].domain = "default";' . + 'wp.i18n.setLocaleData( translations.locale_data.messages, "default" );' . + '})(' . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n\n"; $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -800,7 +801,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; $expected .= "\n"; @@ -817,7 +819,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; $expected .= "\n"; @@ -834,7 +837,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; $expected .= "\n"; @@ -886,7 +890,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; $expected .= "\n"; @@ -905,7 +910,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $expected = ""; $expected .= "\n\n"; $expected .= "\n"; $expected .= "\n";