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.
Built from https://develop.svn.wordpress.org/trunk@38961


git-svn-id: http://core.svn.wordpress.org/trunk@38904 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling
2016-10-26 15:36:31 +00:00
parent 91b518a716
commit 7819e2b4ba
13 changed files with 446 additions and 11 deletions

View File

@@ -272,6 +272,8 @@ function update_option_new_admin_email( $old_value, $value ) {
);
update_option( 'adminhash', $new_admin_email );
$switched_locale = switch_to_locale( get_user_locale() );
/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
$email_text = __( 'Howdy ###USERNAME###,
@@ -315,6 +317,10 @@ All at ###SITENAME###
$content = str_replace( '###SITEURL###', network_home_url(), $content );
wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
if ( $switched_locale ) {
restore_previous_locale();
}
}
/**
@@ -353,6 +359,8 @@ function send_confirmation_on_profile_email() {
);
update_user_meta( $current_user->ID, '_new_email', $new_user_email );
$switched_locale = switch_to_locale( get_user_locale() );
/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
$email_text = __( 'Howdy ###USERNAME###,
@@ -395,6 +403,10 @@ All at ###SITENAME###
wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
$_POST['email'] = $current_user->user_email;
if ( $switched_locale ) {
restore_previous_locale();
}
}
}

View File

@@ -42,6 +42,8 @@ if ( isset( $_POST['action'] ) && $_POST['action'] == 'deleteblog' && isset( $_P
$url_delete = esc_url( admin_url( 'ms-delete-site.php?h=' . $hash ) );
$switched_locale = switch_to_locale( get_locale() );
/* translators: Do not translate USERNAME, URL_DELETE, SITE_NAME: those are placeholders. */
$content = __( "Howdy ###USERNAME###,
@@ -73,6 +75,10 @@ Webmaster
$content = str_replace( '###SITE_NAME###', get_network()->site_name, $content );
wp_mail( get_option( 'admin_email' ), "[ " . wp_specialchars_decode( get_option( 'blogname' ) ) . " ] ".__( 'Delete My Site' ), $content );
if ( $switched_locale ) {
restore_previous_locale();
}
?>
<p><?php _e( 'Thank you. Please check your email for a link to confirm your action. Your site will not be deleted until this link is clicked.' ) ?></p>

View File

@@ -87,6 +87,8 @@ if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
*/
do_action( 'invite_user', $user_id, $role, $newuser_key );
$switched_locale = switch_to_locale( get_user_locale( $user_details ) );
/* translators: 1: Site name, 2: site URL, 3: role, 4: activation URL */
$message = __( 'Hi,
@@ -96,6 +98,11 @@ You\'ve been invited to join \'%1$s\' at
Please click the following link to confirm the invite:
%4$s' );
wp_mail( $new_user_email, sprintf( __( '[%s] Joining confirmation' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), sprintf( $message, get_option( 'blogname' ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ), home_url( "/newbloguser/$newuser_key/" ) ) );
if ( $switched_locale ) {
restore_previous_locale();
}
$redirect = add_query_arg( array('update' => 'add'), 'user-new.php' );
}
}