PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0
Elementor Website Builder – more than just a page builder v3.8.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / force-locale.php

force-locale.php in Elementor Website Builder – more than just a page builder 3.8.0, at core/utils/force-locale.php

100 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Utils;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Force translation to use a specific locale.
10 *
11 * A hacky class to force any translation functions in the call-stack between the
12 * `force()` & `reset()` methods to use a specific locale.
13 */
14 class Force_Locale {
15
16 /**
17 * @var string Locale to force (e.g. `he_IL`).
18 */
19 private $new_locale;
20
21 /**
22 * @var string Original locale before forcing.
23 */
24 private $original_locale;
25
26 /**
27 * @var \Closure Filter reference `pre_determine_locale`.
28 */
29 private $filter;
30
31 public function __construct( $new_locale, $original_locale = null ) {
32 $this->new_locale = $new_locale;
33
34 $this->original_locale = $original_locale ? $original_locale : determine_locale();
35
36 $this->filter = function() use ( $new_locale ) {
37 return $new_locale;
38 };
39 }
40
41 /**
42 * Force the translations to use a specific locale.
43 *
44 * @return void
45 */
46 public function force() {
47 switch_to_locale( $this->new_locale );
48
49 /**
50 * Reset l10n in order to clear the translations cache.
51 *
52 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1324
53 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1222
54 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L821
55 */
56 $this->reset_l10n();
57
58 /**
59 * Force the translations of `$new_locale` to be loaded.
60 *
61 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1294
62 */
63 add_filter( 'pre_determine_locale', $this->filter );
64 }
65
66 /**
67 * Restore the original locale and cleanup filters, etc.
68 *
69 * @return void
70 */
71 public function restore() {
72 $this->reset_l10n();
73
74 switch_to_locale( $this->original_locale );
75
76 remove_filter( 'pre_determine_locale', $this->filter );
77 }
78
79 /**
80 * Reset the l10n global variables.
81 *
82 * @return void
83 */
84 private function reset_l10n() {
85 global $l10n, $l10n_unloaded;
86
87 if ( is_array( $l10n ) ) {
88 foreach ( $l10n as $domain => $l10n_data ) {
89 unset( $l10n[ $domain ] );
90 }
91 }
92
93 if ( is_array( $l10n_unloaded ) ) {
94 foreach ( $l10n_unloaded as $domain => $l10n_unloaded_data ) {
95 unset( $l10n_unloaded[ $domain ] );
96 }
97 }
98 }
99 }
100