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-includes/load.php

    r38899 r38961  
    842842 * @access private
    843843 *
    844  * @global string    $text_direction
    845  * @global WP_Locale $wp_locale      The WordPress date and time locale object.
     844 * @global string             $text_direction
     845 * @global WP_Locale          $wp_locale      The WordPress date and time locale object.
     846 * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
    846847 *
    847848 * @staticvar bool $loaded
    848849 */
    849850function wp_load_translations_early() {
    850     global $text_direction, $wp_locale;
     851    global $text_direction, $wp_locale, $wp_locale_switcher;
    851852
    852853    static $loaded = false;
     
    865866    require_once ABSPATH . WPINC . '/l10n.php';
    866867    require_once ABSPATH . WPINC . '/class-wp-locale.php';
     868    require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';
    867869
    868870    // General libraries
     
    916918
    917919    $wp_locale = new WP_Locale();
     920    $wp_locale_switcher = new WP_Locale_Switcher();
     921    $wp_locale_switcher->init();
    918922}
    919923
Note: See TracChangeset for help on using the changeset viewer.