[wp-trac] [WordPress Trac] #49263: Switching blog doesn't switch locale

WordPress Trac noreply at wordpress.org
Tue Jan 21 19:34:15 UTC 2020


#49263: Switching blog doesn't switch locale
-------------------------+-----------------------------
 Reporter:  iandunn      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  I18N         |    Version:
 Severity:  normal       |   Keywords:
  Focuses:  multisite    |
-------------------------+-----------------------------
 #26511 introduced `switch_to_locale()`, but didn't take into account the
 Multisite use case that @rmccue mentioned in ticket:26511#comment:8.

 It seems reasonable to expect that switching to a site with a different
 locale would switch the locale.

 You can see that it currently doesn't by setting up a network with the
 following conditions and code, and then loading each site.

 * The locale of site `1` is `en_US`
 * The locale of site `2` is `es_MX`
 * The locale of site `3` is `fr_FR`

 {{{#!php
 add_action( 'admin_init', function() {
         echo get_locale() . '<br>'; // loaded site
         _e( 'Howdy, %s' );
         echo '<hr>';

         switch_to_blog( 2 ); // spanish
         echo get_locale() . '<br>';
         _e( 'Howdy, %s' );
         restore_current_blog();
         echo '<hr>';

         switch_to_blog( 3 ); // french
         echo get_locale() . '<br>';
         _e( 'Howdy, %s' );
         restore_current_blog();
         echo '<hr>';

         switch_to_blog( 1 ); // english
         echo get_locale() . '<br>';
         _e( 'Howdy, %s' );
         restore_current_blog();
         echo '<hr>';

         echo get_locale() . '<br>'; // back to loaded site
         _e( 'Howdy, %s' );

         wp_die();
 } );
 }}}

 The strings are always translated using the loaded site's locale, rather
 than the switched site.

 A rudimentary way to see the desired effect would be something like this:

 {{{#!php
 function switch_to_blog_locale() {
         $locale = get_option( 'WPLANG', 'en_US' ); // bypass get_locale()
 b/c early return is stuck on the starting site.

         switch_to_locale( $locale );
 }
 add_action( 'switch_blog', 'switch_to_blog_locale' );
 }}}

 ...although that doesn't take user locales into account, doesn't restore
 previous locales, etc.


 Related: #44844

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


More information about the wp-trac mailing list