Make WordPress Core


Ignore:
Timestamp:
10/26/2016 03:35:58 PM (10 years ago)
Author:
ocean90
Message:

I18N: Introduce a locale-switching function.

With the introduction of user-specific languages in [38705] it's necessary to be able to switch translations on the fly. For example emails should be sent in the language of the recipient and not the one of the current user.

This introduces a new WP_Locale_Switcher class which is used for switching locales and translations. It holds the stack of locales whenever switch_to_locale( $locale ) is called. With restore_previous_locale() you can restore the previous locale. restore_current_locale() empties the stack and sets the locale back to the initial value.

switch_to_locale() is added to most of core's email functions, either with the value of get_locale() (site language) or get_user_locale() (user language with fallback to site language).

Props yoavf, tfrommen, swissspidy, pbearne, ocean90.
See #29783.
Fixes #26511.

File:
1 edited

Legend:

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

    r38876 r38961  
    273273    update_option( 'adminhash', $new_admin_email );
    274274
     275    $switched_locale = switch_to_locale( get_user_locale() );
     276
    275277    /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    276278    $email_text = __( 'Howdy ###USERNAME###,
     
    316318
    317319    wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
     320
     321    if ( $switched_locale ) {
     322        restore_previous_locale();
     323    }
    318324}
    319325
     
    353359        );
    354360        update_user_meta( $current_user->ID, '_new_email', $new_user_email );
     361
     362        $switched_locale = switch_to_locale( get_user_locale() );
    355363
    356364        /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
     
    396404        wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
    397405        $_POST['email'] = $current_user->user_email;
     406
     407        if ( $switched_locale ) {
     408            restore_previous_locale();
     409        }
    398410    }
    399411}
Note: See TracChangeset for help on using the changeset viewer.