WordPress.org

Make WordPress Core


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

    r38955 r38961  
    11791179    return $wp_locale->is_rtl();
    11801180}
     1181
     1182/**
     1183 * Switches the translations according to the given locale.
     1184 *
     1185 * @since 4.7.0
     1186 *
     1187 * @global WP_Locale_Switcher $wp_locale_switcher
     1188 *
     1189 * @param string $locale The locale.
     1190 * @return bool True on success, false on failure.
     1191 */
     1192function switch_to_locale( $locale ) {
     1193    /* @var WP_Locale_Switcher $wp_locale_switcher */
     1194    global $wp_locale_switcher;
     1195
     1196    return $wp_locale_switcher->switch_to_locale( $locale );
     1197}
     1198
     1199/**
     1200 * Restores the translations according to the previous locale.
     1201 *
     1202 * @since 4.7.0
     1203 *
     1204 * @global WP_Locale_Switcher $wp_locale_switcher
     1205 *
     1206 * @return string|false Locale on success, false on error.
     1207 */
     1208function restore_previous_locale() {
     1209    /* @var WP_Locale_Switcher $wp_locale_switcher */
     1210    global $wp_locale_switcher;
     1211
     1212    return $wp_locale_switcher->restore_previous_locale();
     1213}
     1214
     1215/**
     1216 * Restores the translations according to the original locale.
     1217 *
     1218 * @since 4.7.0
     1219 *
     1220 * @global WP_Locale_Switcher $wp_locale_switcher
     1221 *
     1222 * @return string|false Locale on success, false on error.
     1223 */
     1224function restore_current_locale() {
     1225    /* @var WP_Locale_Switcher $wp_locale_switcher */
     1226    global $wp_locale_switcher;
     1227
     1228    return $wp_locale_switcher->restore_current_locale();
     1229}
     1230
     1231/**
     1232 * Whether switch_to_locale() is in effect.
     1233 *
     1234 * @since 4.7.0
     1235 *
     1236 * @global WP_Locale_Switcher $wp_locale_switcher
     1237 *
     1238 * @return bool True if the locale has been switched, false otherwise.
     1239 */
     1240function is_locale_switched() {
     1241    /* @var WP_Locale_Switcher $wp_locale_switcher */
     1242    global $wp_locale_switcher;
     1243
     1244    return $wp_locale_switcher->is_switched();
     1245}
Note: See TracChangeset for help on using the changeset viewer.