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.
Fixes #45161.


git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43878 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
herregroen 2018-11-08 11:33:46 +00:00
parent 4bfa0886eb
commit 7138832595
4 changed files with 33 additions and 16 deletions

View File

@ -115,13 +115,6 @@ if ( 'auto-draft' === $post->post_status ) {
);
}
// Prepare Jed locale data.
$locale_data = wp_get_jed_locale_data( 'default' );
wp_add_inline_script(
'wp-i18n',
'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ' );'
);
// Preload server-registered block schemas.
wp_add_inline_script(
'wp-blocks',

View File

@ -532,7 +532,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 ) {

View File

@ -320,7 +320,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',
@ -363,11 +363,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 ] );
}
}
}

View File

@ -777,8 +777,9 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\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</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@ -794,7 +795,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"internationalized-plugin\" );" .
"translations.locale_data.messages[\"\"].domain = \"internationalized-plugin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-plugin\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
@ -811,7 +813,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"internationalized-theme\" );" .
"translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
@ -828,7 +831,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"admin\" );" .
"translations.locale_data.messages[\"\"].domain = \"admin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
@ -876,7 +880,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
"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</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
@ -895,7 +900,8 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
"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</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";