3 * Locale API: WP_Locale_Switcher class
11 * Core class used for switching locales.
15 class WP_Locale_Switcher {
23 private $locales = array();
32 private $original_locale;
35 * Holds all available languages.
39 * @var array An array of language codes (file names without the .mo extension).
41 private $available_languages = array();
46 * Stores the original locale as well as a list of all available languages.
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() );
56 * Initializes the locale switcher.
58 * Hooks into the {@see 'locale'} filter to change the locale on the fly.
60 public function init() {
61 add_filter( 'locale', array( $this, 'filter_locale' ) );
65 * Switches the translations according to the given locale.
69 * @param string $locale The locale to switch to.
70 * @return bool True on success, false on failure.
72 public function switch_to_locale( $locale ) {
73 $current_locale = is_admin() ? get_user_locale() : get_locale();
74 if ( $current_locale === $locale ) {
78 if ( ! in_array( $locale, $this->available_languages, true ) ) {
82 $this->locales[] = $locale;
84 $this->change_locale( $locale );
87 * Fires when the locale is switched.
91 * @param string $locale The new locale.
93 do_action( 'switch_locale', $locale );
99 * Restores the translations according to the previous locale.
103 * @return string|false Locale on success, false on failure.
105 public function restore_previous_locale() {
106 $previous_locale = array_pop( $this->locales );
108 if ( null === $previous_locale ) {
109 // The stack is empty, bail.
113 $locale = end( $this->locales );
116 // There's nothing left in the stack: go back to the original locale.
117 $locale = $this->original_locale;
120 $this->change_locale( $locale );
123 * Fires when the locale is restored to the previous one.
127 * @param string $locale The new locale.
128 * @param string $previous_locale The previous locale.
130 do_action( 'restore_previous_locale', $locale, $previous_locale );
136 * Restores the translations according to the original locale.
140 * @return string|false Locale on success, false on failure.
142 public function restore_current_locale() {
143 if ( empty( $this->locales ) ) {
147 $this->locales = array( $this->original_locale );
149 return $this->restore_previous_locale();
153 * Whether switch_to_locale() is in effect.
157 * @return bool True if the locale has been switched, false otherwise.
159 public function is_switched() {
160 return ! empty( $this->locales );
164 * Filters the WordPress install's locale.
168 * @param string $locale The WordPress install's locale.
169 * @return string The locale currently being switched to.
171 public function filter_locale( $locale ) {
172 $switched_locale = end( $this->locales );
174 if ( $switched_locale ) {
175 return $switched_locale;
182 * Load translations for a given locale.
184 * When switching to a locale, translations for this locale must be loaded from scratch.
189 * @global Mo[] $l10n An array of all currently loaded text domains.
191 * @param string $locale The locale to load translations for.
193 private function load_translations( $locale ) {
196 $domains = $l10n ? array_keys( $l10n ) : array();
198 load_default_textdomain( $locale );
200 foreach ( $domains as $domain ) {
201 if ( 'default' === $domain ) {
205 unload_textdomain( $domain );
206 get_translations_for_domain( $domain );
211 * Changes the site's locale to the given one.
213 * Loads the translations, changes the global `$wp_locale` object and updates
214 * all post type labels.
219 * @global WP_Locale $wp_locale The WordPress date and time locale object.
221 * @param string $locale The locale to change to.
223 private function change_locale( $locale ) {
224 // Reset translation availability information.
225 _get_path_to_translation( null, true );
227 $this->load_translations( $locale );
229 $GLOBALS['wp_locale'] = new WP_Locale();
232 * Fires when the locale is switched to or restored.
236 * @param string $locale The new locale.
238 do_action( 'change_locale', $locale );