<!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>[55010] trunk: I18N: Change how `WP_Textdomain_Registry` caches translation information.</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/55010">55010</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/55010","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-12-20 15:10:35 +0000 (Tue, 20 Dec 2022)</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: Change how `WP_Textdomain_Registry` caches translation information.
`WP_Textdomain_Registry` was introduced in <a href="https://core.trac.wordpress.org/changeset/53874">[53874]</a> and later adjusted in <a href="https://core.trac.wordpress.org/changeset/54682">[54682]</a> to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and `load_*_textdomain()` functions.
This change improves how the class stores information about all existing MO files on the site, addressing an issue where translations are not loaded after calling `switch_to_locale()`.
Props johnbillion, ocean90, SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/57116">#57116</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswplocaleswitcherphp">trunk/src/wp-includes/class-wp-locale-switcher.php</a></li>
<li><a href="#trunksrcwpincludesclasswptextdomainregistryphp">trunk/src/wp-includes/class-wp-textdomain-registry.php</a></li>
<li><a href="#trunktestsphpunittestsl10nwpLocaleSwitcherphp">trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php</a></li>
<li><a href="#trunktestsphpunittestsl10nwpTextdomainRegistryphp">trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswplocaleswitcherphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-locale-switcher.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-locale-switcher.php 2022-12-20 13:55:25 UTC (rev 55009)
+++ trunk/src/wp-includes/class-wp-locale-switcher.php 2022-12-20 15:10:35 UTC (rev 55010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,7 +36,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @var string[] An array of language codes (file names without the .mo extension).
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private $available_languages = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private $available_languages;
</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"> * Constructor.
</span></span></pre></div>
<a id="trunksrcwpincludesclasswptextdomainregistryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-textdomain-registry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-textdomain-registry.php 2022-12-20 13:55:25 UTC (rev 55009)
+++ trunk/src/wp-includes/class-wp-textdomain-registry.php 2022-12-20 15:10:35 UTC (rev 55010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -51,9 +51,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- protected $cached_mo_files;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $cached_mo_files = array();
</ins><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">+ * Holds a cached list of domains with translations to improve performance.
+ *
+ * @since 6.1.2
+ *
+ * @var string[]
+ */
+ protected $domains_with_translations = array();
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Returns the languages directory path for a specific domain and locale.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,7 +93,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool Whether any MO file paths are available for the domain.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function has( $domain ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return ! empty( $this->current[ $domain ] ) || empty( $this->all[ $domain ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return (
+ ! empty( $this->current[ $domain ] ) ||
+ empty( $this->all[ $domain ] ) ||
+ in_array( $domain, $this->domains_with_translations, true )
+ );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -109,6 +122,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Used by {@see load_plugin_textdomain()} and {@see load_theme_textdomain()}.
</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 6.1.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param string $domain Text domain.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $path Language directory path.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,6 +132,27 @@
</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">+ * Returns possible language directory paths for a given text domain.
+ *
+ * @since 6.1.2
+ *
+ * @param string $domain Text domain.
+ * @return string[] Array of language directory paths.
+ */
+ private function get_paths_for_domain( $domain ) {
+ $locations = array(
+ WP_LANG_DIR . '/plugins',
+ WP_LANG_DIR . '/themes',
+ );
+
+ if ( isset( $this->custom_paths[ $domain ] ) ) {
+ $locations[] = $this->custom_paths[ $domain ];
+ }
+
+ return $locations;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Gets the path to the language directory for the current locale.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Checks the plugins and themes language directories as well as any
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,37 +167,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|false Language directory path or false if there is none available.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function get_path_from_lang_dir( $domain, $locale ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locations = array(
- WP_LANG_DIR . '/plugins',
- WP_LANG_DIR . '/themes',
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $locations = $this->get_paths_for_domain( $domain );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $this->custom_paths[ $domain ] ) ) {
- $locations[] = $this->custom_paths[ $domain ];
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $found_location = false;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $mofile = "$domain-$locale.mo";
-
</del><span class="cx" style="display: block; padding: 0 10px"> foreach ( $locations as $location ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $this->cached_mo_files[ $location ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->set_cached_mo_files( $location );
</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">- $path = $location . '/' . $mofile;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $path = "$location/$domain-$locale.mo";
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( $path, $this->cached_mo_files[ $location ], true ) ) {
- $this->set( $domain, $locale, $location );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $this->cached_mo_files[ $location ] as $mo_path ) {
+ if (
+ ! in_array( $domain, $this->domains_with_translations, true ) &&
+ str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" )
+ ) {
+ $this->domains_with_translations[] = $domain;
+ }
</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 trailingslashit( $location );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $mo_path === $path ) {
+ $found_location = trailingslashit( $location );
+ }
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $found_location ) {
+ $this->set( $domain, $locale, $found_location );
+
+ return $found_location;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // If no path is found for the given locale and a custom path has been set
</span><span class="cx" style="display: block; padding: 0 10px"> // using load_plugin_textdomain/load_theme_textdomain, use that one.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'en_US' !== $locale && isset( $this->custom_paths[ $domain ] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $path = trailingslashit( $this->custom_paths[ $domain ] );
- $this->set( $domain, $locale, $path );
- return $path;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $fallback_location = trailingslashit( $this->custom_paths[ $domain ] );
+ $this->set( $domain, $locale, $fallback_location );
+ return $fallback_location;
</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"> $this->set( $domain, $locale, false );
</span></span></pre></div>
<a id="trunktestsphpunittestsl10nwpLocaleSwitcherphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php 2022-12-20 13:55:25 UTC (rev 55009)
+++ trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php 2022-12-20 15:10:35 UTC (rev 55010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,20 +24,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
</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_Textdomain_Registry $wp_textdomain_registry */
- global $wp_textdomain_registry;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_textdomain_registry, $wp_locale_switcher;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_textdomain_registry = new WP_Textdomain_Registry();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
+ $wp_locale_switcher = new WP_Locale_Switcher();
+ $wp_locale_switcher->init();
</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"> public function tear_down() {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] );
</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_Textdomain_Registry $wp_textdomain_registry */
- global $wp_textdomain_registry;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_textdomain_registry, $wp_locale_switcher;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_textdomain_registry = new WP_Textdomain_Registry();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
+ $wp_locale_switcher = new WP_Locale_Switcher();
+ $wp_locale_switcher->init();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -336,8 +342,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale_switcher = clone $wp_locale_switcher;
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Reset $wp_locale_switcher so it thinks es_ES is the original locale.
+ remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $wp_locale_switcher = new WP_Locale_Switcher();
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_locale_switcher->init();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -357,8 +363,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $language_header_after_restore = $l10n['default']->headers['Language']; // de_DE
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_locale_switcher = $locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $locale_switched_user_locale );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( $locale_switched_site_locale );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $site_locale, $site_locale_after_switch );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -388,8 +392,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale_switcher = clone $wp_locale_switcher;
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Reset $wp_locale_switcher so it thinks es_ES is the original locale.
+ remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $wp_locale_switcher = new WP_Locale_Switcher();
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_locale_switcher->init();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -409,8 +413,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $language_header_after_restore = $l10n['default']->headers['Language']; // de_DE
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_locale_switcher = $locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'locale', array( $this, 'filter_locale' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $locale_switched_user_locale );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -426,8 +428,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::load_default_textdomain
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_multiple_switches_to_site_locale_and_user_locale() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $site_locale = get_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = self::factory()->user->create(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -440,11 +440,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale_switcher = clone $wp_locale_switcher;
-
- $wp_locale_switcher = new WP_Locale_Switcher();
- $wp_locale_switcher->init();
-
</del><span class="cx" style="display: block; padding: 0 10px"> $user_locale = get_user_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> load_default_textdomain( $user_locale );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -458,8 +453,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> restore_current_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_locale_switcher = $locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'en_US', get_locale() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'This is a dummy plugin', $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -469,13 +462,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_switch_reloads_plugin_translations_outside_wp_lang_dir() {
</span><span class="cx" style="display: block; padding: 0 10px"> /** @var WP_Textdomain_Registry $wp_textdomain_registry */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_locale_switcher, $wp_textdomain_registry;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_textdomain_registry;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale_switcher = clone $wp_locale_switcher;
-
- $wp_locale_switcher = new WP_Locale_Switcher();
- $wp_locale_switcher->init();
-
</del><span class="cx" style="display: block; padding: 0 10px"> require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = custom_i18n_plugin_test();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -494,8 +482,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> restore_current_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_locale_switcher = $locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'This is a dummy plugin', $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( WP_PLUGIN_DIR . '/custom-internationalized-plugin/languages/', $registry_value );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'Das ist ein Dummy Plugin', $actual_de_de );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -503,17 +489,52 @@
</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 57116
+ */
+ public function test_switch_reloads_plugin_translations() {
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $wp_textdomain_registry;
+
+ $has_translations_1 = $wp_textdomain_registry->has( 'internationalized-plugin' );
+
+ require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
+
+ $actual = i18n_plugin_test();
+
+ switch_to_locale( 'es_ES' );
+
+ $lang_path_es_es = $wp_textdomain_registry->get( 'internationalized-plugin', determine_locale() );
+
+ switch_to_locale( 'de_DE' );
+
+ $actual_de_de = i18n_plugin_test();
+
+ $has_translations_3 = $wp_textdomain_registry->has( 'internationalized-plugin' );
+
+ restore_previous_locale();
+
+ $actual_es_es = i18n_plugin_test();
+
+ restore_current_locale();
+
+ $lang_path_en_us = $wp_textdomain_registry->get( 'internationalized-plugin', determine_locale() );
+
+ $this->assertSame( 'This is a dummy plugin', $actual );
+ $this->assertSame( 'Das ist ein Dummy Plugin', $actual_de_de );
+ $this->assertSame( 'Este es un plugin dummy', $actual_es_es );
+ $this->assertTrue( $has_translations_1 );
+ $this->assertTrue( $has_translations_3 );
+ $this->assertSame( WP_LANG_DIR . '/plugins/', $lang_path_es_es );
+ $this->assertFalse( $lang_path_en_us );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 39210
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_switch_reloads_theme_translations_outside_wp_lang_dir() {
</span><span class="cx" style="display: block; padding: 0 10px"> /** @var WP_Textdomain_Registry $wp_textdomain_registry */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_locale_switcher, $wp_textdomain_registry;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_textdomain_registry;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale_switcher = clone $wp_locale_switcher;
-
- $wp_locale_switcher = new WP_Locale_Switcher();
- $wp_locale_switcher->init();
-
</del><span class="cx" style="display: block; padding: 0 10px"> switch_theme( 'custom-internationalized-theme' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> require_once get_stylesheet_directory() . '/functions.php';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -534,8 +555,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> restore_current_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_locale_switcher = $locale_switcher;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( get_template_directory() . '/languages/', $registry_value );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'This is a dummy theme', $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'Das ist ein Dummy Theme', $actual_de_de );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -542,6 +561,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'Este es un tema dummy', $actual_es_es );
</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 57116
+ */
+ public function test_switch_to_locale_should_work() {
+ global $wp_textdomain_registry;
+ require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
+
+ $has_translations = $wp_textdomain_registry->has( 'internationalized-plugin' );
+ $path = $wp_textdomain_registry->get( 'internationalized-plugin', 'es_ES' );
+
+ $actual = i18n_plugin_test();
+
+ switch_to_locale( 'es_ES' );
+
+ $actual_es_es = i18n_plugin_test();
+
+ $this->assertTrue( $has_translations );
+ $this->assertNotEmpty( $path );
+ $this->assertSame( 'This is a dummy plugin', $actual );
+ $this->assertSame( 'Este es un plugin dummy', $actual_es_es );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function filter_locale() {
</span><span class="cx" style="display: block; padding: 0 10px"> return 'es_ES';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsl10nwpTextdomainRegistryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php 2022-12-20 13:55:25 UTC (rev 55009)
+++ trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php 2022-12-20 15:10:35 UTC (rev 55010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,7 +28,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $reflection_property = $reflection->getProperty( 'cached_mo_files' );
</span><span class="cx" style="display: block; padding: 0 10px"> $reflection_property->setAccessible( 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">- $this->assertNull(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEmpty(
</ins><span class="cx" style="display: block; padding: 0 10px"> $reflection_property->getValue( $this->instance ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'Cache not empty by default'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,28 +79,6 @@
</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">- * @covers ::get_path_from_lang_dir
- */
- public function test_get_does_not_check_themes_directory_for_plugin() {
- $reflection = new ReflectionClass( $this->instance );
- $reflection_property = $reflection->getProperty( 'cached_mo_files' );
- $reflection_property->setAccessible( true );
-
- $this->instance->get( 'internationalized-plugin', 'de_DE' );
-
- $this->assertArrayHasKey(
- WP_LANG_DIR . '/plugins',
- $reflection_property->getValue( $this->instance ),
- 'Default plugins path missing from cache'
- );
- $this->assertArrayNotHasKey(
- WP_LANG_DIR . '/themes',
- $reflection_property->getValue( $this->instance ),
- 'Default themes path should not be in cache'
- );
- }
-
- /**
</del><span class="cx" style="display: block; padding: 0 10px"> * @covers ::set
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre>
</div>
</div>
</body>
</html>