Make WordPress Core


Ignore:
Timestamp:
10/26/2016 03:35:58 PM (9 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-includes/ms-functions.php

    r38943 r38961  
    801801    $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
    802802    $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
     803
     804    $user = get_user_by( 'login', $user );
     805    $switched_locale = switch_to_locale( get_user_locale( $user ) );
     806
    803807    $message = sprintf(
    804808        /**
     
    850854    );
    851855    wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     856
     857    if ( $switched_locale ) {
     858        restore_previous_locale();
     859    }
     860
    852861    return true;
    853862}
     
    887896    if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
    888897        return false;
     898
     899    $user = get_user_by( 'login', $user );
     900    $switched_locale = switch_to_locale( get_user_locale( $user ) );
    889901
    890902    // Send email with activation link.
     
    935947    );
    936948    wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     949
     950    if ( $switched_locale ) {
     951        restore_previous_locale();
     952    }
     953
    937954    return true;
    938955}
     
    14491466        return false;
    14501467
     1468    $user = get_userdata( $user_id );
     1469
     1470    $switched_locale = switch_to_locale( get_user_locale( $user ) );
     1471
    14511472    $welcome_email = get_site_option( 'welcome_email' );
    14521473    if ( $welcome_email == false ) {
     
    14691490
    14701491    $url = get_blogaddress_by_id($blog_id);
    1471     $user = get_userdata( $user_id );
    14721492
    14731493    $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
     
    15131533    $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_network->site_name, wp_unslash( $title ) ) );
    15141534    wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     1535
     1536    if ( $switched_locale ) {
     1537        restore_previous_locale();
     1538    }
     1539
    15151540    return true;
    15161541}
     
    15511576
    15521577    $user = get_userdata( $user_id );
     1578
     1579    $switched_locale = switch_to_locale( get_user_locale( $user ) );
    15531580
    15541581    /**
     
    15911618    $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_network->site_name, $user->user_login) );
    15921619    wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     1620
     1621    if ( $switched_locale ) {
     1622        restore_previous_locale();
     1623    }
     1624
    15931625    return true;
    15941626}
Note: See TracChangeset for help on using the changeset viewer.