Make WordPress Core


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

    r38928 r38961  
    131131require_once( ABSPATH . WPINC . '/l10n.php' );
    132132require_once( ABSPATH . WPINC . '/class-wp-locale.php' );
     133require_once( ABSPATH . WPINC . '/class-wp-locale-switcher.php' );
    133134
    134135// Run the installer if WordPress is not installed.
     
    401402$GLOBALS['wp_locale'] = new WP_Locale();
    402403
     404/**
     405 *  WordPress Locale Switcher object for switching locales.
     406 *
     407 * @since 4.7.0
     408 *
     409 * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
     410 */
     411$GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher();
     412$GLOBALS['wp_locale_switcher']->init();
     413
    403414// Load the functions for the active theme, for both parent and child theme if applicable.
    404415if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) {
Note: See TracChangeset for help on using the changeset viewer.