[wp-trac] [WordPress Trac] #39210: switch_to_locale() unloads all plugin and theme translations

WordPress Trac noreply at wordpress.org
Fri Dec 9 16:07:09 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        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When using switch_to_locale() in the backend, all translations loaded into
 the `$l10n` global will be unloaded, except for the default one. This
 makes plugin and theme translations unavailable after using
 switch_to_locale().

 In the `load_translations` method of `WP_Locale_Switcher` there are two
 functions called right after each other for each of the currently loaded
 domains:

 {{{#!php
 unload_textdomain( $domain );
 get_translations_for_domain( $domain );
 }}}

 `unload_textdomain` loads all unloaded functions into the `$l10n_unloaded`
 global. Later when `_load_textdomain_just_in_time()` is called in
 `get_translations_for_domain()`, all domains set in `$10n_unloaded` will
 be short-circuited in the following statement

 {{{#!php
 // Short-circuit if domain is 'default' which is reserved for core.
 if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
     return false;
 }
 }}}

 This results in only the new translations for the domain `default` being
 loaded. All plugin and theme translations will be lost. But even when `||
 isset( $l10n_unloaded[ $domain ]` is commented out, it doesn’t work,
 because WordPress then tries to load a language file from the `WP_LANG`
 folder, and not from either a theme or plugin directory.

 In #26511 it was already mentioned by @rmccue that this might affect
 emails to be sent in the wrong language:
 https://core.trac.wordpress.org/ticket/26511#trac-
 change-8-1430202811399151.

 I run into this problem when I want to send notification emails to
 subscribed email adresses whenever I publish a post.

 = Test case =

 I created a minimal theme with a translations for `en_EN` and `de_DE` that
 displays some debug output in the backend and the frontend.

 Preparatory steps:

 1. Set site language to German (de_DE).
 2. Set admin user language to English (en_EN). If the language dropdown
 does not appear, temporarily set the site language to German so the
 translation is downloaded and then select the users language again.
 3. Install the test theme.

 Now an admin notice should appear in the backend that shows debug output.
 There are string translations saved before and after `switch_to_locale`.
 Whenever `Untranslated default string` shows up, then a translation
 couldn’t be loaded.

 ---

 Now if this is not intended behavior, I don’t really know how I would
 approach fixing this.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39210>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list