/wp-includes/class-wp-locale-switcher.php

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