<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43859] branches/5.0: I18N: Improve JavaScript translation support.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43859">43859</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43859","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>herregroen</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-11-01 13:50:59 +0000 (Thu, 01 Nov 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>I18N: Improve JavaScript translation support.
Improves upon <a href="https://core.trac.wordpress.org/changeset/43825">[43825]</a> by adding unit tests to cover additional use-cases and changes loading translations to only occur when outputting the script to allow setting translations on dependencies.
Props atimmer, omarreiss, nerrad, swissspidy, ocean90.
Fixes <a href="https://core.trac.wordpress.org/ticket/45103">#45103</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches50srcwpincludesclasswpdependencyphp">branches/5.0/src/wp-includes/class-wp-dependency.php</a></li>
<li><a href="#branches50srcwpincludesclasswpscriptsphp">branches/5.0/src/wp-includes/class.wp-scripts.php</a></li>
<li><a href="#branches50srcwpincludesfunctionswpscriptsphp">branches/5.0/src/wp-includes/functions.wp-scripts.php</a></li>
<li><a href="#branches50srcwpincludesl10nphp">branches/5.0/src/wp-includes/l10n.php</a></li>
<li><a href="#branches50testsphpunittestsdependenciesscriptsphp">branches/5.0/tests/phpunit/tests/dependencies/scripts.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#branches50testsphpunitdatalanguagesadminen_USscripthandlejson">branches/5.0/tests/phpunit/data/languages/admin-en_US-script-handle.json</a></li>
<li><a href="#branches50testsphpunitdatalanguagesen_US813e104eb47e13dd4cc5af844c618754json">branches/5.0/tests/phpunit/data/languages/en_US-813e104eb47e13dd4cc5af844c618754.json</a></li>
<li><a href="#branches50testsphpunitdatalanguagespluginsinternationalizedpluginen_US2f86cb96a0233e7cb3b6f03ad573be0bjson">branches/5.0/tests/phpunit/data/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json</a></li>
<li><a href="#branches50testsphpunitdatalanguagesthemesinternationalizedthemeen_US2f86cb96a0233e7cb3b6f03ad573be0bjson">branches/5.0/tests/phpunit/data/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#branches50testsphpunitdatalanguagesdefaulten_US813e104eb47e13dd4cc5af844c618754json">branches/5.0/tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches50srcwpincludesclasswpdependencyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-includes/class-wp-dependency.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/class-wp-dependency.php 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/src/wp-includes/class-wp-dependency.php 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,6 +68,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $extra = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Translation textdomain set for this dependency.
+ *
+ * @since 5.0.0
+ * @var string
+ */
+ public $textdomain;
+
+ /**
+ * Translation path set for this dependency.
+ *
+ * @since 5.0.0
+ * @var string
+ */
+ public $translations_path;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Setup dependencies.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -94,4 +110,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function set_translations( $domain, $path = null ) {
+ if ( !is_string($domain) )
+ return false;
+ $this->textdomain = $domain;
+ $this->translations_path = $path;
+ return true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="branches50srcwpincludesclasswpscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-includes/class.wp-scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/class.wp-scripts.php 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/src/wp-includes/class.wp-scripts.php 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -325,6 +325,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $translations = $this->print_translations( $handle, false );
+ if ( $translations ) {
+ $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $src = $this->base_url . $src;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -338,7 +343,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $src )
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the HTML script tag of an enqueued script.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -478,7 +483,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Register a translation textdomain.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sets a translation textdomain.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -493,6 +498,33 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** @var \_WP_Dependency $obj */
+ $obj = $this->registered[ $handle ];
+
+ if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
+ $obj->deps[] = 'wp-i18n';
+ }
+ return $obj->set_translations( $domain, $path );
+ }
+
+ /**
+ * Prints translations set for a specific handle.
+ *
+ * @since 5.0.0
+ *
+ * @param string $handle Name of the script to add the inline script to. Must be lowercase.
+ * @param bool $echo Optional. Whether to echo the script instead of just returning it.
+ * Default true.
+ * @return string|false Script on success, false otherwise.
+ */
+ public function print_translations( $handle, $echo = true ) {
+ if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
+ return false;
+ }
+
+ $domain = $this->registered[ $handle ]->textdomain;
+ $path = $this->registered[ $handle ]->translations_path;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $json_translations = load_script_textdomain( $handle, $domain, $path );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $json_translations ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -499,17 +531,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /** @var \_WP_Dependency $obj */
- $obj = $this->registered[ $handle ];
- $obj->deps[] = 'wp-i18n';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $output = '(function( translations ){' .
+ 'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
+ '})(' . $json_translations . ');';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->add_inline_script(
- $handle,
- '(function( translations ){' .
- 'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
- '})(' . $json_translations . ');',
- 'before'
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $echo ) {
+ printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
+ }
+
+ return $output;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="branches50srcwpincludesfunctionswpscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-includes/functions.wp-scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/functions.wp-scripts.php 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/src/wp-includes/functions.wp-scripts.php 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -193,12 +193,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Register translated strings for a script.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sets translated strings for a script.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Works only if the script has already been added.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see WP_Scripts::set_translations()
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @link https://core.trac.wordpress.org/ticket/45103
</del><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -216,10 +215,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! wp_script_is( $handle, 'enqueued' ) ) {
- _doing_it_wrong( __FUNCTION__, __( 'Script translations may only be set if the script is enqueued.' ), '5.0.0' );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> return $wp_scripts->set_translations( $handle, $domain, $path );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="branches50srcwpincludesl10nphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-includes/l10n.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/l10n.php 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/src/wp-includes/l10n.php 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -876,6 +876,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @link https://core.trac.wordpress.org/ticket/45103
</span><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param string $handle Name of the script to register a translation domain to.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $domain The textdomain.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $path Optional. The full file path to the directory containing translation files.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -886,10 +888,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function load_script_textdomain( $handle, $domain, $path = null ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_scripts;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $path = untrailingslashit( $path );
</ins><span class="cx" style="display: block; padding: 0 10px"> $locale = is_admin() ? get_locale() : get_user_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If a path was given and the handle file exists simply return it.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $handle_filename = $domain . '-' . $locale . '-' . $handle . '.json';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale;
+ $handle_filename = $file_base . '-' . $handle . '.json';
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return file_get_contents( $path . '/' . $handle_filename );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -908,7 +912,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If the host is the same or it's a relative URL.
</span><span class="cx" style="display: block; padding: 0 10px"> if (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- strpos( $content_url['path'], $src_url['path'] ) === 0 &&
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ strpos( $src_url['path'], $content_url['path'] ) === 0 &&
</ins><span class="cx" style="display: block; padding: 0 10px"> ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
</span><span class="cx" style="display: block; padding: 0 10px"> ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Make the src relative the specific plugin or theme.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -925,7 +929,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $relative = trim( $src_url['path'], '/' );
</span><span class="cx" style="display: block; padding: 0 10px"> } else if (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- ( strpos( $site_url['path'], $src_url['path'] ) === 0 ) &&
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) &&
</ins><span class="cx" style="display: block; padding: 0 10px"> ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] )
</span><span class="cx" style="display: block; padding: 0 10px"> ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Make the src relative to the WP root.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -943,7 +947,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $relative = substr( $relative, 0, -7 ) . '.js';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $md5_filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return file_get_contents( $path . '/' . $md5_filename );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="branches50testsphpunitdatalanguagesadminen_USscripthandlejson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/tests/phpunit/data/languages/admin-en_US-script-handle.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/data/languages/admin-en_US-script-handle.json (rev 0)
+++ branches/5.0/tests/phpunit/data/languages/admin-en_US-script-handle.json 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,17 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+ "translation-revision-data": "+0000",
+ "generator": "GlotPress/2.3.0-alpha",
+ "domain": "messages",
+ "locale_data": {
+ "messages": {
+ "": {
+ "domain": "messages",
+ "plural-forms": "n != 1",
+ "lang": "en-gb"
+ },
+ "This file is a translation for script-handle.": [
+ "This file is a translation for script-handle."
+ ]
+ }
+ }
+}
</ins></span></pre></div>
<a id="branches50testsphpunitdatalanguagesdefaulten_US813e104eb47e13dd4cc5af844c618754json"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: branches/5.0/tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,30 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-{
- "translation-revision-data": "+0000",
- "generator": "GlotPress/2.3.0-alpha",
- "domain": "messages",
- "locale_data": {
- "messages": {
- "": {
- "domain": "messages",
- "plural-forms": "n != 1",
- "lang": "en-gb"
- },
- "This file is too big. Files must be less than %d KB in size.": [
- "This file is too big. Files must be less than %d KB in size."
- ],
- "%d Theme Update": [
- "%d Theme Update",
- "%d Theme Updates"
- ],
- "password strength\u0004Medium": [
- "Medium"
- ],
- "taxonomy singular name\u0004Category": [
- "Category"
- ],
- "post type general name\u0004Pages": [
- "Pages"
- ]
- }
- }
-}
</del></span></pre></div>
<a id="branches50testsphpunitdatalanguagesen_US813e104eb47e13dd4cc5af844c618754jsonfromrev43858branches50testsphpunitdatalanguagesdefaulten_US813e104eb47e13dd4cc5af844c618754json"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/5.0/tests/phpunit/data/languages/en_US-813e104eb47e13dd4cc5af844c618754.json (from rev 43858, branches/5.0/tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/data/languages/en_US-813e104eb47e13dd4cc5af844c618754.json (rev 0)
+++ branches/5.0/tests/phpunit/data/languages/en_US-813e104eb47e13dd4cc5af844c618754.json 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,30 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+ "translation-revision-data": "+0000",
+ "generator": "GlotPress/2.3.0-alpha",
+ "domain": "messages",
+ "locale_data": {
+ "messages": {
+ "": {
+ "domain": "messages",
+ "plural-forms": "n != 1",
+ "lang": "en-gb"
+ },
+ "This file is too big. Files must be less than %d KB in size.": [
+ "This file is too big. Files must be less than %d KB in size."
+ ],
+ "%d Theme Update": [
+ "%d Theme Update",
+ "%d Theme Updates"
+ ],
+ "password strength\u0004Medium": [
+ "Medium"
+ ],
+ "taxonomy singular name\u0004Category": [
+ "Category"
+ ],
+ "post type general name\u0004Pages": [
+ "Pages"
+ ]
+ }
+ }
+}
</ins></span></pre></div>
<a id="branches50testsphpunitdatalanguagespluginsinternationalizedpluginen_US2f86cb96a0233e7cb3b6f03ad573be0bjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/tests/phpunit/data/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/data/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json (rev 0)
+++ branches/5.0/tests/phpunit/data/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,17 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+ "translation-revision-data": "+0000",
+ "generator": "GlotPress/2.3.0-alpha",
+ "domain": "messages",
+ "locale_data": {
+ "messages": {
+ "": {
+ "domain": "messages",
+ "plural-forms": "n != 1",
+ "lang": "en-gb"
+ },
+ "This is a dummy plugin.": [
+ "This is a dummy plugin."
+ ]
+ }
+ }
+}
</ins></span></pre></div>
<a id="branches50testsphpunitdatalanguagesthemesinternationalizedthemeen_US2f86cb96a0233e7cb3b6f03ad573be0bjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/tests/phpunit/data/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/data/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json (rev 0)
+++ branches/5.0/tests/phpunit/data/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,17 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+ "translation-revision-data": "+0000",
+ "generator": "GlotPress/2.3.0-alpha",
+ "domain": "messages",
+ "locale_data": {
+ "messages": {
+ "": {
+ "domain": "messages",
+ "plural-forms": "n != 1",
+ "lang": "en-gb"
+ },
+ "This is a dummy theme.": [
+ "This is a dummy theme."
+ ]
+ }
+ }
+}
</ins></span></pre></div>
<a id="branches50testsphpunittestsdependenciesscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/tests/phpunit/tests/dependencies/scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/tests/dependencies/scripts.php 2018-11-01 05:19:48 UTC (rev 43858)
+++ branches/5.0/tests/phpunit/tests/dependencies/scripts.php 2018-11-01 13:50:59 UTC (rev 43859)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -770,15 +770,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 45103
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_wp_set_script_translation() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_wp_set_script_translations() {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages/' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
</span><span class="cx" style="display: block; padding: 0 10px"> $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
</span><span class="cx" style="display: block; padding: 0 10px"> "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "})(" . file_get_contents( DIR_TESTDATA . '/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json') . ");\n</script>\n";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
</ins><span class="cx" style="display: block; padding: 0 10px"> $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -785,6 +785,125 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_for_plugin() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ 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 = "<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\" );" .
+ "})(" . 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";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_for_theme() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ 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 = "<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\" );" .
+ "})(" . 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";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_with_handle_file() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null );
+ wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' );
+
+ $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\" );" .
+ "})(" . 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";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_i18n_dependency() {
+ global $wp_scripts;
+
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
+ wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages/' );
+
+ $script = $wp_scripts->registered[ 'test-example' ];
+
+ $this->assertContains( 'wp-i18n', $script->deps );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_when_translation_file_does_not_exist() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null );
+ 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 .= "\n<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_after_register() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null );
+ wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
+
+ wp_enqueue_script( 'test-example' );
+
+ $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";
+ $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
+ * @ticket 45103
+ */
+ public function test_wp_set_script_translations_dependency() {
+ wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
+ wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null );
+ wp_set_script_translations( 'test-dependency', 'default', DIR_TESTDATA . '/languages' );
+
+ wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
+
+ $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";
+ $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";
+
+ $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Testing `wp_enqueue_code_editor` with file path.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 41871
</span></span></pre>
</div>
</div>
</body>
</html>