[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-locale-switcher.php (source)

   1  <?php
   2  /**
   3   * Locale API: WP_Locale_Switcher class
   4   *
   5   * @package WordPress
   6   * @subpackage i18n
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class used for switching locales.
  12   *
  13   * @since 4.7.0
  14   */
  15  class WP_Locale_Switcher {
  16      /**
  17       * Locale stack.
  18       *
  19       * @since 4.7.0
  20       * @var string[]
  21       */
  22      private $locales = array();
  23  
  24      /**
  25       * Original locale.
  26       *
  27       * @since 4.7.0
  28       * @var string
  29       */
  30      private $original_locale;
  31  
  32      /**
  33       * Holds all available languages.
  34       *
  35       * @since 4.7.0
  36       * @var array An array of language codes (file names without the .mo extension).
  37       */
  38      private $available_languages = array();
  39  
  40      /**
  41       * Constructor.
  42       *
  43       * Stores the original locale as well as a list of all available languages.
  44       *
  45       * @since 4.7.0
  46       */
  47  	public function __construct() {
  48          $this->original_locale     = determine_locale();
  49          $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() );
  50      }
  51  
  52      /**
  53       * Initializes the locale switcher.
  54       *
  55       * Hooks into the {@see 'locale'} filter to change the locale on the fly.
  56       *
  57       * @since 4.7.0
  58       */
  59  	public function init() {
  60          add_filter( 'locale', array( $this, 'filter_locale' ) );
  61      }
  62  
  63      /**
  64       * Switches the translations according to the given locale.
  65       *
  66       * @since 4.7.0
  67       *
  68       * @param string $locale The locale to switch to.
  69       * @return bool True on success, false on failure.
  70       */
  71  	public function switch_to_locale( $locale ) {
  72          $current_locale = determine_locale();
  73          if ( $current_locale === $locale ) {
  74              return false;
  75          }
  76  
  77          if ( ! in_array( $locale, $this->available_languages, true ) ) {
  78              return false;
  79          }
  80  
  81          $this->locales[] = $locale;
  82  
  83          $this->change_locale( $locale );
  84  
  85          /**
  86           * Fires when the locale is switched.
  87           *
  88           * @since 4.7.0
  89           *
  90           * @param string $locale The new locale.
  91           */
  92          do_action( 'switch_locale', $locale );
  93  
  94          return true;
  95      }
  96  
  97      /**
  98       * Restores the translations according to the previous locale.
  99       *
 100       * @since 4.7.0
 101       *
 102       * @return string|false Locale on success, false on failure.
 103       */
 104  	public function restore_previous_locale() {
 105          $previous_locale = array_pop( $this->locales );
 106  
 107          if ( null === $previous_locale ) {
 108              // The stack is empty, bail.
 109              return false;
 110          }
 111  
 112          $locale = end( $this->locales );
 113  
 114          if ( ! $locale ) {
 115              // There's nothing left in the stack: go back to the original locale.
 116              $locale = $this->original_locale;
 117          }
 118  
 119          $this->change_locale( $locale );
 120  
 121          /**
 122           * Fires when the locale is restored to the previous one.
 123           *
 124           * @since 4.7.0
 125           *
 126           * @param string $locale          The new locale.
 127           * @param string $previous_locale The previous locale.
 128           */
 129          do_action( 'restore_previous_locale', $locale, $previous_locale );
 130  
 131          return $locale;
 132      }
 133  
 134      /**
 135       * Restores the translations according to the original locale.
 136       *
 137       * @since 4.7.0
 138       *
 139       * @return string|false Locale on success, false on failure.
 140       */
 141  	public function restore_current_locale() {
 142          if ( empty( $this->locales ) ) {
 143              return false;
 144          }
 145  
 146          $this->locales = array( $this->original_locale );
 147  
 148          return $this->restore_previous_locale();
 149      }
 150  
 151      /**
 152       * Whether switch_to_locale() is in effect.
 153       *
 154       * @since 4.7.0
 155       *
 156       * @return bool True if the locale has been switched, false otherwise.
 157       */
 158  	public function is_switched() {
 159          return ! empty( $this->locales );
 160      }
 161  
 162      /**
 163       * Filters the locale of the WordPress installation.
 164       *
 165       * @since 4.7.0
 166       *
 167       * @param string $locale The locale of the WordPress installation.
 168       * @return string The locale currently being switched to.
 169       */
 170  	public function filter_locale( $locale ) {
 171          $switched_locale = end( $this->locales );
 172  
 173          if ( $switched_locale ) {
 174              return $switched_locale;
 175          }
 176  
 177          return $locale;
 178      }
 179  
 180      /**
 181       * Load translations for a given locale.
 182       *
 183       * When switching to a locale, translations for this locale must be loaded from scratch.
 184       *
 185       * @since 4.7.0
 186       *
 187       * @global Mo[] $l10n An array of all currently loaded text domains.
 188       *
 189       * @param string $locale The locale to load translations for.
 190       */
 191  	private function load_translations( $locale ) {
 192          global $l10n;
 193  
 194          $domains = $l10n ? array_keys( $l10n ) : array();
 195  
 196          load_default_textdomain( $locale );
 197  
 198          foreach ( $domains as $domain ) {
 199              if ( 'default' === $domain ) {
 200                  continue;
 201              }
 202  
 203              unload_textdomain( $domain );
 204              get_translations_for_domain( $domain );
 205          }
 206      }
 207  
 208      /**
 209       * Changes the site's locale to the given one.
 210       *
 211       * Loads the translations, changes the global `$wp_locale` object and updates
 212       * all post type labels.
 213       *
 214       * @since 4.7.0
 215       *
 216       * @global WP_Locale $wp_locale WordPress date and time locale object.
 217       *
 218       * @param string $locale The locale to change to.
 219       */
 220  	private function change_locale( $locale ) {
 221          // Reset translation availability information.
 222          _get_path_to_translation( null, true );
 223  
 224          $this->load_translations( $locale );
 225  
 226          $GLOBALS['wp_locale'] = new WP_Locale();
 227  
 228          /**
 229           * Fires when the locale is switched to or restored.
 230           *
 231           * @since 4.7.0
 232           *
 233           * @param string $locale The new locale.
 234           */
 235          do_action( 'change_locale', $locale );
 236      }
 237  }


Generated: Fri Apr 26 01:00:03 2024 Cross-referenced by PHPXref 0.7.1