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.

git-svn-id: https://develop.svn.wordpress.org/trunk@38961 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90)
2016-10-26 15:35:58 +00:00
parent a6a15b1819
commit 9e3f6d338b
15 changed files with 867 additions and 16 deletions
+33 -1
View File
@@ -800,6 +800,10 @@ function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_ema
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
$user = get_user_by( 'login', $user );
$switched_locale = switch_to_locale( get_user_locale( $user ) );
$message = sprintf(
/**
* Filters the message content of the new blog notification email.
@@ -849,6 +853,11 @@ function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_ema
esc_url( 'http://' . $domain . $path )
);
wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}
@@ -887,6 +896,9 @@ function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array(
if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
return false;
$user = get_user_by( 'login', $user );
$switched_locale = switch_to_locale( get_user_locale( $user ) );
// Send email with activation link.
$admin_email = get_site_option( 'admin_email' );
if ( $admin_email == '' )
@@ -934,6 +946,11 @@ function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array(
$user
);
wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}
@@ -1448,6 +1465,10 @@ function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta
if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
return false;
$user = get_userdata( $user_id );
$switched_locale = switch_to_locale( get_user_locale( $user ) );
$welcome_email = get_site_option( 'welcome_email' );
if ( $welcome_email == false ) {
/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
@@ -1468,7 +1489,6 @@ We hope you enjoy your new site. Thanks!
}
$url = get_blogaddress_by_id($blog_id);
$user = get_userdata( $user_id );
$welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
$welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
@@ -1512,6 +1532,11 @@ We hope you enjoy your new site. Thanks!
*/
$subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_network->site_name, wp_unslash( $title ) ) );
wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}
@@ -1551,6 +1576,8 @@ function wpmu_welcome_user_notification( $user_id, $password, $meta = array() )
$user = get_userdata( $user_id );
$switched_locale = switch_to_locale( get_user_locale( $user ) );
/**
* Filters the content of the welcome email after user activation.
*
@@ -1590,6 +1617,11 @@ function wpmu_welcome_user_notification( $user_id, $password, $meta = array() )
*/
$subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_network->site_name, $user->user_login) );
wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}