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

    r38723 r38961  
    8888            do_action( 'invite_user', $user_id, $role, $newuser_key );
    8989
     90            $switched_locale = switch_to_locale( get_user_locale( $user_details ) );
     91
    9092            /* translators: 1: Site name, 2: site URL, 3: role, 4: activation URL */
    9193            $message = __( 'Hi,
     
    9799%4$s' );
    98100            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/" ) ) );
     101
     102            if ( $switched_locale ) {
     103                restore_previous_locale();
     104            }
     105
    99106            $redirect = add_query_arg( array('update' => 'add'), 'user-new.php' );
    100107        }
Note: See TracChangeset for help on using the changeset viewer.