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/l10n.php

    r54938 r55161  
    16661666    global $wp_locale_switcher;
    16671667
     1668    if ( ! $wp_locale_switcher ) {
     1669        return false;
     1670    }
     1671
    16681672    return $wp_locale_switcher->switch_to_locale( $locale );
     1673}
     1674
     1675/**
     1676 * Switches the translations according to the given user's locale.
     1677 *
     1678 * @since 6.2.0
     1679 *
     1680 * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
     1681 *
     1682 * @param int $user_id User ID.
     1683 * @return bool True on success, false on failure.
     1684 */
     1685function switch_to_user_locale( $user_id ) {
     1686    /* @var WP_Locale_Switcher $wp_locale_switcher */
     1687    global $wp_locale_switcher;
     1688
     1689    if ( ! $wp_locale_switcher ) {
     1690        return false;
     1691    }
     1692
     1693    return $wp_locale_switcher->switch_to_user_locale( $user_id );
    16691694}
    16701695
     
    16821707    global $wp_locale_switcher;
    16831708
     1709    if ( ! $wp_locale_switcher ) {
     1710        return false;
     1711    }
     1712
    16841713    return $wp_locale_switcher->restore_previous_locale();
    16851714}
     
    16971726    /* @var WP_Locale_Switcher $wp_locale_switcher */
    16981727    global $wp_locale_switcher;
     1728
     1729    if ( ! $wp_locale_switcher ) {
     1730        return false;
     1731    }
    16991732
    17001733    return $wp_locale_switcher->restore_current_locale();
Note: See TracChangeset for help on using the changeset viewer.