[wp-trac] [WordPress Trac] #39210: switch_to_locale() unloads all plugin and theme translations
WordPress Trac
noreply at wordpress.org
Tue Dec 13 08:44:33 UTC 2016
#39210: switch_to_locale() unloads all plugin and theme translations
--------------------------+------------------------------
Reporter: gchtr | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: I18N | Version: 4.7
Severity: normal | Resolution:
Keywords: needs-patch | Focuses:
--------------------------+------------------------------
Comment (by gchtr):
Thanks for the explanation @ocean90! I see that for performance reasons,
it makes total sense to do it like that.
I now use the following workaround, which works fine:
{{{#!php
<?php
add_action( 'change_locale', 'force_load_theme_textdomain' );
// Switch to site locale
$site_locale = get_locale();
$locale_switched = switch_to_locale( $site_locale );
}}}
{{{#!php
<?php
/**
* Hotfix for WordPress 4.7
*
* WordPress removes translations when switch_to_locale is used in
backend.
* Until this error is resolved, we circumvent it by adding a filter
* to force the site locale for load_theme_textdomain() and then load the
missing
* text domain.
*
* @see https://core.trac.wordpress.org/ticket/39210
*/
function force_load_theme_textdomain() {
add_filter( 'theme_locale', 'force_site_locale_once' );
load_theme_textdomain( 'axa-blog', get_template_directory() .
'/languages' );
}
/**
* Forces site locale once.
*
* Since 4.7 WordPress always returns the user locale in the backend.
* This filters tells WordPress to use the site locale once when
* load_theme_textdomain() is used.
*
* @return string $locale The locale to use.
*/
function force_site_locale_once() {
// Remove filter to use it only once
remove_filter( 'theme_locale', 'force_site_locale_once' );
// Return site locale
return get_locale();
}
}}}
I have to use another filter before using `load_theme_textdomain` to
force-load the site’s text domain and not the user’s, which is the default
for the backend.
Now is it really a bug or a matter of documentation (the special case of
using switch_to_locale() in the backend)? Am I on an edge case here? Or
should this be made more convenient for developers?
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39210#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list