| 1 | <?php |
| 2 | /** |
| 3 | * Locale API: WP_Locale_Switcher class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage i18n |
| 7 | * @since 4.6.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Core class used for switching locales. |
| 12 | * |
| 13 | * @since 4.6.0 |
| 14 | */ |
| 15 | class WP_Locale_Switcher { |
| 16 | |
| 17 | /** |
| 18 | * Filter callbacks. |
| 19 | * |
| 20 | * @since 4.6.0 |
| 21 | * @var callback[] |
| 22 | */ |
| 23 | private $filters = array(); |
| 24 | |
| 25 | /** |
| 26 | * Locale stack. |
| 27 | * |
| 28 | * @since 4.6.0 |
| 29 | * @var string[] |
| 30 | */ |
| 31 | private $locales = array(); |
| 32 | |
| 33 | /** |
| 34 | * Original locale. |
| 35 | * |
| 36 | * @since 4.6.0 |
| 37 | * @var string |
| 38 | */ |
| 39 | private $original_locale; |
| 40 | |
| 41 | /** |
| 42 | * Translation objects. |
| 43 | * |
| 44 | * @since 4.6.0 |
| 45 | * @var NOOP_Translations[][] |
| 46 | */ |
| 47 | private $translations = array(); |
| 48 | |
| 49 | /** |
| 50 | * Constructor. Stores the original locale. |
| 51 | * |
| 52 | * @since 4.6.0 |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | $this->original_locale = get_locale(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Switches the translations according to the given locale. |
| 60 | * |
| 61 | * @since 4.6.0 |
| 62 | * |
| 63 | * @param string $locale The locale. |
| 64 | * @return bool True on success, false on failure. |
| 65 | */ |
| 66 | public function switch_to_locale( $locale ) { |
| 67 | $current_locale = get_locale(); |
| 68 | if ( $current_locale === $locale ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @global MO[] $l10n |
| 74 | */ |
| 75 | global $l10n; |
| 76 | |
| 77 | $textdomains = array_keys( $l10n ); |
| 78 | |
| 79 | if ( ! $this->has_translations_for_locale( $current_locale ) ) { |
| 80 | foreach ( $textdomains as $textdomain ) { |
| 81 | $this->translations[ $current_locale ][ $textdomain ] = get_translations_for_domain( $textdomain ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | $this->remove_filters(); |
| 86 | |
| 87 | $this->add_filter_for_locale( $locale ); |
| 88 | |
| 89 | if ( $this->has_translations_for_locale( $locale ) ) { |
| 90 | foreach ( $textdomains as $textdomain ) { |
| 91 | if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) { |
| 92 | $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ]; |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | foreach ( $l10n as $textdomain => $mo ) { |
| 97 | if ( 'default' === $textdomain ) { |
| 98 | load_default_textdomain(); |
| 99 | |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | unload_textdomain( $textdomain ); |
| 104 | |
| 105 | if ( $mofile = $mo->get_filename() ) { |
| 106 | load_textdomain( $textdomain, $mofile ); |
| 107 | } |
| 108 | |
| 109 | $this->translations[ $locale ][ $textdomain ] = get_translations_for_domain( $textdomain ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @global WP_Locale $wp_locale |
| 115 | */ |
| 116 | $GLOBALS['wp_locale'] = new WP_Locale(); |
| 117 | |
| 118 | $this->locales[] = $locale; |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Restores the translations according to the previous locale. |
| 125 | * |
| 126 | * @since 4.6.0 |
| 127 | * |
| 128 | * @return string|false Locale on success, false on error. |
| 129 | */ |
| 130 | public function restore_locale() { |
| 131 | if ( ! array_pop( $this->locales ) ) { |
| 132 | // The stack is empty, bail. |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | $this->remove_filters(); |
| 137 | |
| 138 | if ( $locale = end( $this->locales ) ) { |
| 139 | if ( isset( $this->filters[ $locale ] ) ) { |
| 140 | add_filter( 'locale', $this->filters[ $locale ] ); |
| 141 | } |
| 142 | } else { |
| 143 | // There's nothing left in the stack: go back to the original locale. |
| 144 | $locale = $this->original_locale; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @global MO[] $l10n |
| 149 | */ |
| 150 | global $l10n; |
| 151 | |
| 152 | foreach ( array_keys( $l10n ) as $textdomain ) { |
| 153 | if ( isset( $this->translations[ $locale ][ $textdomain ] ) ) { |
| 154 | $l10n[ $textdomain ] = $this->translations[ $locale ][ $textdomain ]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @global WP_Locale $wp_locale |
| 160 | */ |
| 161 | $GLOBALS['wp_locale'] = new WP_Locale(); |
| 162 | |
| 163 | return $locale; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Checks if there are cached translations for the given locale. |
| 168 | * |
| 169 | * @since 4.6.0 |
| 170 | * |
| 171 | * @param string $locale The locale. |
| 172 | * @return bool True if there are cached translations for the given locale, false otherwise. |
| 173 | */ |
| 174 | private function has_translations_for_locale( $locale ) { |
| 175 | return ! empty( $this->translations[ $locale ] ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Removes all filter callbacks added before. |
| 180 | * |
| 181 | * @since 4.6.0 |
| 182 | */ |
| 183 | private function remove_filters() { |
| 184 | foreach ( $this->filters as $filter ) { |
| 185 | remove_filter( 'locale', $filter ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Adds a filter callback returning the given locale. |
| 191 | * |
| 192 | * @since 4.6.0 |
| 193 | * |
| 194 | * @param string $locale The locale. |
| 195 | */ |
| 196 | private function add_filter_for_locale( $locale ) { |
| 197 | if ( ! isset( $this->filters[ $locale ] ) ) { |
| 198 | require_once ABSPATH . WPINC . '/class-wp-locale-storage.php'; |
| 199 | |
| 200 | // This SHOULD be a closure. |
| 201 | $this->filters[ $locale ] = array( new WP_Locale_Storage( $locale ), 'get' ); |
| 202 | } |
| 203 | |
| 204 | add_filter( 'locale', $this->filters[ $locale ] ); |
| 205 | } |
| 206 | } |