WP_Locale_Switcher

Core class used for switching locales.

Defined (1)

The class is defined in the following location(s).

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