Skip to content

Commit

Permalink
I18N: Fix warnings in JavaScript translations.
Browse files Browse the repository at this point in the history
Fixes an undefined index PHP warning in the load_script_textdomain function and a non-interrupting JS error when registering a domain which has no translations.

Props omarreiss, ocean90, georgestephanis.
Fixes #45256.


git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43898 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
herregroen committed Nov 15, 2018
1 parent 4b791ec commit 07736dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/wp-includes/class.wp-scripts.php
Expand Up @@ -528,7 +528,8 @@ public function print_translations( $handle, $echo = true ) {
$json_translations = load_script_textdomain( $handle, $domain, $path ); $json_translations = load_script_textdomain( $handle, $domain, $path );


if ( ! $json_translations ) { if ( ! $json_translations ) {
return false; // Register empty locale data object to ensure the domain still exists.
$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
} }


$output = '(function( translations ){' . $output = '(function( translations ){' .
Expand Down
20 changes: 8 additions & 12 deletions src/wp-includes/l10n.php
Expand Up @@ -923,18 +923,14 @@ function load_script_textdomain( $handle, $domain, $path = null ) {


$relative = array_slice( $relative, 2 ); $relative = array_slice( $relative, 2 );
$relative = implode( '/', $relative ); $relative = implode( '/', $relative );
} else if ( } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
! isset( $site_url['path'] ) && if ( ! isset( $site_url['path'] ) ) {
( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) $relative = trim( $src_url['path'], '/' );
) { } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
$relative = trim( $src_url['path'], '/' ); // Make the src relative to the WP root.
} else if ( $relative = substr( $src, strlen( $site_url['path'] ) );
( strpos( $src_url['path'], $site_url['path'] ) === 0 ) && $relative = trim( $relative, '/' );
( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) }
) {
// Make the src relative to the WP root.
$relative = substr( $src, strlen( $site_url['path'] ) );
$relative = trim( $relative, '/' );
} }


// If the source is not from WP. // If the source is not from WP.
Expand Down
6 changes: 5 additions & 1 deletion tests/phpunit/tests/dependencies/scripts.php
Expand Up @@ -863,7 +863,11 @@ public function test_wp_set_script_translations_when_translation_file_does_not_e
wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' ); wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' );


$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n"; $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"admin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
"})({ \"locale_data\": { \"messages\": { \"\": {} } } });\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";


$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
} }
Expand Down

0 comments on commit 07736dc

Please sign in to comment.