[wp-trac] [WordPress Trac] #44844: switch_to_locale does not handle plugins/themes properly in multisite

WordPress Trac noreply at wordpress.org
Sun Aug 26 22:17:17 UTC 2018


#44844: switch_to_locale does not handle plugins/themes properly in multisite
--------------------------+-----------------------------
 Reporter:  pcfreak30     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  I18N          |    Version:  4.9.8
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Running the following test script should enable switching of languages in
 a multisite.

 {{{#!php
 require_once( dirname( __FILE__ ) . '/wp-load.php' );

 switch_to_blog(2);
 switch_to_locale('sv_SE');
 var_dump(__('my text','my-domain'));

 }}}

 However do to complexities I am not familiar with in unload_textdomain and
 get_translations_for_domain it does not work. Breaking it down
 unload_textdomain flags it as unloaded which _load_textdomain_just_in_time
 via get_translations_for_domain checks and aborts. the only way I have
 been able to do this is via some hacky filtering and mangling language
 globals.

 Example:

 {{{#!php
 switch_to_blog( $object['blog_id'] );
                         unset( $GLOBALS['locale'] );
                         $locale = get_locale();
                         unset( $GLOBALS['locale'] );
                         restore_current_blog();
                         add_filter( 'locale', [ $this, 'one_time_false' ]
 );
                         add_filter( 'override_unload_textdomain', [ $this,
 'unload_textdomain' ], 10, 2 );

                         switch_to_locale( $locale );
                         remove_filter( 'locale', [ $this, 'one_time_false'
 ] );
                         remove_filter( 'override_unload_textdomain', [
 $this, 'unload_textdomain' ] );
                         switch_to_blog( $object['blog_id'] );

 }}}

 $this->unload_textdomain being:

 {{{#!php
 <?php
 public function unload_textdomain( $value, $domain ) {
                 if ( isset( $GLOBALS['l10n'][ $domain ] ) ) {
                         unset( $GLOBALS['l10n'][ $domain ] );
                 }

                 return $value;
         }
 }}}

 and $this->one_time_false being:

 {{{#!php
 <?php
 public function one_time_false() {
                 remove_filter( 'locale', [ $this, 'one_time_false' ] );

                 return false;
         }
 }}}


 Not sure how this should be refactored but this so far should really be
 unnecessary to do.

 Thanks.

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


More information about the wp-trac mailing list