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/user.php

    r38905 r38961  
    18021802        $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    18031803
     1804        $switched_locale = false;
     1805        if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
     1806            $switched_locale = switch_to_locale( get_user_locale( $user_id ) );
     1807        }
     1808
    18041809        if ( ! empty( $send_password_change_email ) ) {
    1805 
    18061810            /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    18071811            $pass_change_text = __( 'Hi ###USERNAME###,
     
    19111915            wp_mail( $email_change_email['to'], sprintf( $email_change_email['subject'], $blog_name ), $email_change_email['message'], $email_change_email['headers'] );
    19121916        }
     1917
     1918        if ( $switched_locale ) {
     1919            restore_previous_locale();
     1920        }
    19131921    }
    19141922
Note: See TracChangeset for help on using the changeset viewer.