Make WordPress Core


Ignore:
Timestamp:
01/30/2023 10:25:53 AM (23 months ago)
Author:
swissspidy
Message:

I18N: Introduce switch_to_user_locale().

This new function makes it easier to switch to a specific user’s locale by reducing duplicate code and storing the user’s ID as additional context for plugins to consume. Existing usage of switch_to_locale() in core has been replaced with switch_to_user_locale() where appropriate.

Also, this change ensures WP_Locale_Switcher properly filters determine_locale so that anyyone using the determine_locale() function will get the correct locale information when switching is in effect.

Props costdev.
Fixes #57123.
See #26511.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r55015 r55161  
    25792579    $switched_locale = false;
    25802580    if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
    2581         $switched_locale = switch_to_locale( get_user_locale( $user_id ) );
     2581        $switched_locale = switch_to_user_locale( $user_id );
    25822582    }
    25832583
     
    31403140    $locale = get_user_locale( $user_data );
    31413141
    3142     $switched_locale = switch_to_locale( $locale );
     3142    $switched_locale = switch_to_user_locale( $user_data->ID );
    31433143
    31443144    if ( is_multisite() ) {
     
    42454245    // Localize message content for user; fallback to site default for visitors.
    42464246    if ( ! empty( $request->user_id ) ) {
    4247         $locale = get_user_locale( $request->user_id );
     4247        $switched_locale = switch_to_user_locale( $request->user_id );
    42484248    } else {
    4249         $locale = get_locale();
    4250     }
    4251 
    4252     $switched_locale = switch_to_locale( $locale );
     4249        $switched_locale = switch_to_locale( get_locale() );
     4250    }
    42534251
    42544252    /**
     
    46564654    // Localize message content for user; fallback to site default for visitors.
    46574655    if ( ! empty( $request->user_id ) ) {
    4658         $locale = get_user_locale( $request->user_id );
     4656        $switched_locale = switch_to_user_locale( $request->user_id );
    46594657    } else {
    4660         $locale = get_locale();
    4661     }
    4662 
    4663     $switched_locale = switch_to_locale( $locale );
     4658        $switched_locale = switch_to_locale( get_locale() );
     4659    }
    46644660
    46654661    $email_data = array(
Note: See TracChangeset for help on using the changeset viewer.