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/pomo/mo.php

    r35714 r38961  
    1717
    1818    /**
     19     * Loaded MO file.
     20     *
     21     * @var string
     22     */
     23    private $filename = '';
     24
     25    /**
     26     * Returns the loaded MO file.
     27     *
     28     * @return string The loaded MO file.
     29     */
     30    public function get_filename() {
     31        return $this->filename;
     32    }
     33
     34    /**
    1935     * Fills up with the entries from MO file $filename
    2036     *
     
    2238     */
    2339    function import_from_file($filename) {
    24         $reader = new POMO_FileReader($filename);
    25         if (!$reader->is_resource())
    26             return false;
    27         return $this->import_from_reader($reader);
     40        $reader = new POMO_FileReader( $filename );
     41
     42        if ( ! $reader->is_resource() ) {
     43            return false;
     44        }
     45
     46        $this->filename = (string) $filename;
     47
     48        return $this->import_from_reader( $reader );
    2849    }
    2950
Note: See TracChangeset for help on using the changeset viewer.