Make WordPress Core


Ignore:
Timestamp:
10/26/2016 03:35:58 PM (8 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/pluggable.php

    r38470 r38961  
    212212    if ( ! ( $phpmailer instanceof PHPMailer ) ) {
    213213        require_once ABSPATH . WPINC . '/class-phpmailer.php';
    214         require_once ABSPATH . WPINC . '/class-smtp.php'; 
     214        require_once ABSPATH . WPINC . '/class-smtp.php';
    215215        $phpmailer = new PHPMailer( true );
    216216    }
     
    14181418        $emails = array_flip( $emails );
    14191419    }
     1420
     1421    $switched_locale = switch_to_locale( get_locale() );
    14201422
    14211423    $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
     
    15231525    }
    15241526
     1527    if ( $switched_locale ) {
     1528        restore_previous_locale();
     1529    }
     1530
    15251531    return true;
    15261532}
     
    15691575            $emails[] = $user->user_email;
    15701576    }
     1577
     1578    $switched_locale = switch_to_locale( get_locale() );
    15711579
    15721580    $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
     
    16651673    }
    16661674
     1675    if ( $switched_locale ) {
     1676        restore_previous_locale();
     1677    }
     1678
    16671679    return true;
    16681680}
     
    17241736
    17251737    if ( 'user' !== $notify ) {
     1738        $switched_locale = switch_to_locale( get_locale() );
    17261739        $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
    17271740        $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
     
    17291742
    17301743        @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message );
     1744
     1745        if ( $switched_locale ) {
     1746            restore_previous_locale();
     1747        }
    17311748    }
    17321749
     
    17491766    $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
    17501767
     1768    $switched_locale = switch_to_locale( get_user_locale( $user ) );
     1769
    17511770    $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
    17521771    $message .= __('To set your password, visit the following address:') . "\r\n\r\n";
     
    17561775
    17571776    wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message);
     1777
     1778    if ( $switched_locale ) {
     1779        restore_previous_locale();
     1780    }
    17581781}
    17591782endif;
Note: See TracChangeset for help on using the changeset viewer.