[wp-trac] [WordPress Trac] #39210: switch_to_locale() unloads all plugin and theme translations
WordPress Trac
noreply at wordpress.org
Fri Nov 22 12:10:03 UTC 2019
#39210: switch_to_locale() unloads all plugin and theme translations
-------------------------------------------------+-------------------------
Reporter: gchtr | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Future
| Release
Component: I18N | Version: 4.7
Severity: normal | Resolution:
Keywords: has-patch needs-unit-tests dev- | Focuses:
feedback |
-------------------------------------------------+-------------------------
Comment (by msykes):
As eluded to above, I've found the problem to be directly correlated with
`_load_textdomain_just_in_time()` and how it checks the `$l10n_unloaded`
which gets manipulated during `unload_textdomain()` during switching
languages.
I'm experiencing this issue but in a slightly different context. My issue
is when using switch_to_locale(), restore_previous_locale() and then
switch_to_locale() again. The first time round works for me, but the
second time the text isn't translated.
My use case is sending emails for bookings in different languages of the
person making the booking.
As a simple test, install Events Manager from WordPress.org and make sure
Spanish is installed in the wp-content/languages folder. Then add this:
{{{#!php
<?php
add_action('init', function(){
global $locale;
$domain = 'events-manager';
$test_string = 'Quantity';
$test_output = 'Current "'.$test_string.'" Translation in
<code>%s</code> : %s<br>';
echo sprintf($test_output, $locale, __($test_string, $domain));
switch_to_locale('es_ES');
echo sprintf($test_output, 'es_ES', __($test_string, $domain));
restore_previous_locale();
echo sprintf($test_output, $locale, __($test_string, $domain));
// uncomment the following line to fii the problem
//global $l10n_unloaded;
//if( !empty($l10n_unloaded[$domain]) )
unset($l10n_unloaded[$domain]);
switch_to_locale('es_ES');
echo sprintf($test_output, 'es_ES', __($test_string, $domain));
die();
});
}}}
The last line gets translated only if I uncomment those lines near the
bottom.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39210#comment:19>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list