Completed
Pull Request — master (#1190)
by Darren
08:58
created
core/services/assets/I18nRegistry.php 1 patch
Indentation   +235 added lines, -235 removed lines patch added patch discarded remove patch
@@ -15,239 +15,239 @@
 block discarded – undo
15 15
  */
16 16
 class I18nRegistry
17 17
 {
18
-    /**
19
-     * @var DomainInterface
20
-     */
21
-    private $domain;
22
-
23
-    /**
24
-     * Will hold all registered i18n scripts.  Prevents script handles from being registered more than once.
25
-     *
26
-     * @var array
27
-     */
28
-    private $registered_i18n = array();
29
-
30
-
31
-    /**
32
-     * Used to hold queued translations for the chunks loading in a view.
33
-     *
34
-     * @var array
35
-     */
36
-    private $queued_handle_translations = array();
37
-
38
-    /**
39
-     * Used to track script handles queued for adding translation strings as inline data in the dom.
40
-     *
41
-     * @var array
42
-     */
43
-    private $queued_scripts = array();
44
-
45
-
46
-    /**
47
-     * Obtained from the generated json file from the all javascript using wp.i18n with a map of script handle names to
48
-     * translation strings.
49
-     *
50
-     * @var array
51
-     */
52
-    private $i18n_map;
53
-
54
-
55
-    /**
56
-     * I18nRegistry constructor.
57
-     *
58
-     * @param array() $i18n_map  An array of script handle names and the strings translated for those handles.  If not
59
-     *                            provided, the class will look for map in root of plugin with filename of
60
-     *                            'translation-map.json'.
61
-     * @param DomainInterface $domain
62
-     */
63
-    public function __construct(array $i18n_map = array(), DomainInterface $domain)
64
-    {
65
-        $this->domain = $domain;
66
-        $this->setI18nMap($i18n_map);
67
-        add_filter('print_scripts_array', array($this, 'queueI18n'));
68
-    }
69
-
70
-
71
-    /**
72
-     * Used to register a script that has i18n strings for its $handle
73
-     *
74
-     * @param string $handle The script handle reference.
75
-     * @param string $domain The i18n domain for the strings.
76
-     */
77
-    public function registerScriptI18n($handle, $domain = 'event_espresso')
78
-    {
79
-        if(! isset($this->registered_i18n[$handle])) {
80
-            $this->registered_i18n[ $handle ] = 1;
81
-            $this->queued_scripts[ $handle ] = $domain;
82
-        }
83
-    }
84
-
85
-
86
-
87
-    /**
88
-     * Callback on print_scripts_array to listen for scripts enqueued and handle setting up the localized data.
89
-     *
90
-     * @param array $handles Array of registered script handles.
91
-     * @return array
92
-     */
93
-    public function queueI18n(array $handles)
94
-    {
95
-        if (empty($this->queued_scripts)) {
96
-            return $handles;
97
-        }
98
-        foreach ($handles as $handle) {
99
-            $this->queueI18nTranslationsForHandle($handle);
100
-        }
101
-        if ($this->queued_handle_translations) {
102
-            foreach ($this->queued_handle_translations as $handle => $translations_for_domain) {
103
-                $this->registerInlineScript(
104
-                    $handle,
105
-                    $translations_for_domain['translations'],
106
-                    $translations_for_domain['domain']
107
-                );
108
-                unset($this->queued_handle_translations[$handle]);
109
-            }
110
-        }
111
-        return $handles;
112
-    }
113
-
114
-
115
-    /**
116
-     * Registers inline script with translations for given handle and domain.
117
-     *
118
-     * @param string $handle       Handle used to register javascript file containing translations.
119
-     * @param array  $translations Array of string translations.
120
-     * @param string $domain       Domain for translations.  If left empty then strings are registered with the default
121
-     *                             domain for the javascript.
122
-     */
123
-    protected function registerInlineScript($handle, array $translations, $domain)
124
-    {
125
-        $script = $domain ?
126
-            'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ', "' . $domain . '" );' :
127
-            'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ' );';
128
-        wp_add_inline_script($handle, $script, 'before');
129
-    }
130
-
131
-
132
-    /**
133
-     * Queues up the translation strings for the given handle.
134
-     *
135
-     * @param string $handle The script handle being queued up.
136
-     */
137
-    private function queueI18nTranslationsForHandle($handle)
138
-    {
139
-        if (isset($this->queued_scripts[$handle])) {
140
-            $domain = $this->queued_scripts[$handle];
141
-            $translations = $this->getJedLocaleDataForDomainAndChunk($handle, $domain);
142
-            if (count($translations) > 0) {
143
-                $this->queued_handle_translations[$handle] = array(
144
-                    'domain'       => $domain,
145
-                    'translations' => $translations,
146
-                );
147
-            }
148
-            unset($this->queued_scripts[$handle]);
149
-        }
150
-    }
151
-
152
-
153
-    /**
154
-     * Sets the internal i18n_map property.
155
-     * If $chunk_map is empty or not an array, will attempt to load a chunk map from a default named map.
156
-     *
157
-     * @param array $i18n_map  If provided, an array of translation strings indexed by script handle names they
158
-     *                         correspond to.
159
-     */
160
-    private function setI18nMap(array $i18n_map)
161
-    {
162
-        if (empty($i18n_map)) {
163
-            $i18n_map = file_exists($this->domain->pluginPath() . 'translation-map.json')
164
-                ? json_decode(
165
-                        file_get_contents($this->domain->pluginPath() . 'translation-map.json'),
166
-                        true
167
-                    )
168
-                : array();
169
-        }
170
-        $this->i18n_map = $i18n_map;
171
-    }
172
-
173
-
174
-    /**
175
-     * Get the jed locale data for a given $handle and domain
176
-     *
177
-     * @param string $handle The name for the script handle we want strings returned for.
178
-     * @param string $domain The i18n domain.
179
-     * @return array
180
-     */
181
-    protected function getJedLocaleDataForDomainAndChunk($handle, $domain)
182
-    {
183
-        $translations = $this->getJedLocaleData($domain);
184
-        // get index for adding back after extracting strings for this $chunk.
185
-        $index = $translations[''];
186
-        $translations = $this->getLocaleDataMatchingMap(
187
-            $this->getOriginalStringsForHandleFromMap($handle),
188
-            $translations
189
-        );
190
-        $translations[''] = $index;
191
-        return $translations;
192
-    }
193
-
194
-
195
-    /**
196
-     * Get locale data for given strings from given translations
197
-     *
198
-     * @param array $string_set   This is the subset of strings (msgIds) we want to extract from the translations array.
199
-     * @param array $translations Translation data to extra strings from.
200
-     * @return array
201
-     */
202
-    protected function getLocaleDataMatchingMap(array $string_set, array $translations)
203
-    {
204
-        if (empty($string_set)) {
205
-            return array();
206
-        }
207
-        // some strings with quotes in them will break on the array_flip, so making sure quotes in the string are
208
-        // slashed also filter falsey values.
209
-        $string_set = array_unique(array_filter(wp_slash($string_set)));
210
-        return array_intersect_key($translations, array_flip($string_set));
211
-    }
212
-
213
-
214
-    /**
215
-     * Get original strings to translate for the given chunk from the map
216
-     *
217
-     * @param string $handle The script handle name to get strings from the map for.
218
-     * @return array
219
-     */
220
-    protected function getOriginalStringsForHandleFromMap($handle)
221
-    {
222
-        return isset($this->i18n_map[$handle]) ? $this->i18n_map[$handle] : array();
223
-    }
224
-
225
-
226
-    /**
227
-     * Returns Jed-formatted localization data.
228
-     *
229
-     * @param  string $domain Translation domain.
230
-     * @return array
231
-     */
232
-    private function getJedLocaleData($domain)
233
-    {
234
-        $translations = get_translations_for_domain($domain);
235
-
236
-        $locale = array(
237
-            '' => array(
238
-                'domain' => $domain,
239
-                'lang'   => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale()
240
-            ),
241
-        );
242
-
243
-        if (! empty($translations->headers['Plural-Forms'])) {
244
-            $locale['']['plural_forms'] = $translations->headers['Plural-Forms'];
245
-        }
246
-
247
-        foreach ($translations->entries as $msgid => $entry) {
248
-            $locale[$msgid] = $entry->translations;
249
-        }
250
-
251
-        return $locale;
252
-    }
18
+	/**
19
+	 * @var DomainInterface
20
+	 */
21
+	private $domain;
22
+
23
+	/**
24
+	 * Will hold all registered i18n scripts.  Prevents script handles from being registered more than once.
25
+	 *
26
+	 * @var array
27
+	 */
28
+	private $registered_i18n = array();
29
+
30
+
31
+	/**
32
+	 * Used to hold queued translations for the chunks loading in a view.
33
+	 *
34
+	 * @var array
35
+	 */
36
+	private $queued_handle_translations = array();
37
+
38
+	/**
39
+	 * Used to track script handles queued for adding translation strings as inline data in the dom.
40
+	 *
41
+	 * @var array
42
+	 */
43
+	private $queued_scripts = array();
44
+
45
+
46
+	/**
47
+	 * Obtained from the generated json file from the all javascript using wp.i18n with a map of script handle names to
48
+	 * translation strings.
49
+	 *
50
+	 * @var array
51
+	 */
52
+	private $i18n_map;
53
+
54
+
55
+	/**
56
+	 * I18nRegistry constructor.
57
+	 *
58
+	 * @param array() $i18n_map  An array of script handle names and the strings translated for those handles.  If not
59
+	 *                            provided, the class will look for map in root of plugin with filename of
60
+	 *                            'translation-map.json'.
61
+	 * @param DomainInterface $domain
62
+	 */
63
+	public function __construct(array $i18n_map = array(), DomainInterface $domain)
64
+	{
65
+		$this->domain = $domain;
66
+		$this->setI18nMap($i18n_map);
67
+		add_filter('print_scripts_array', array($this, 'queueI18n'));
68
+	}
69
+
70
+
71
+	/**
72
+	 * Used to register a script that has i18n strings for its $handle
73
+	 *
74
+	 * @param string $handle The script handle reference.
75
+	 * @param string $domain The i18n domain for the strings.
76
+	 */
77
+	public function registerScriptI18n($handle, $domain = 'event_espresso')
78
+	{
79
+		if(! isset($this->registered_i18n[$handle])) {
80
+			$this->registered_i18n[ $handle ] = 1;
81
+			$this->queued_scripts[ $handle ] = $domain;
82
+		}
83
+	}
84
+
85
+
86
+
87
+	/**
88
+	 * Callback on print_scripts_array to listen for scripts enqueued and handle setting up the localized data.
89
+	 *
90
+	 * @param array $handles Array of registered script handles.
91
+	 * @return array
92
+	 */
93
+	public function queueI18n(array $handles)
94
+	{
95
+		if (empty($this->queued_scripts)) {
96
+			return $handles;
97
+		}
98
+		foreach ($handles as $handle) {
99
+			$this->queueI18nTranslationsForHandle($handle);
100
+		}
101
+		if ($this->queued_handle_translations) {
102
+			foreach ($this->queued_handle_translations as $handle => $translations_for_domain) {
103
+				$this->registerInlineScript(
104
+					$handle,
105
+					$translations_for_domain['translations'],
106
+					$translations_for_domain['domain']
107
+				);
108
+				unset($this->queued_handle_translations[$handle]);
109
+			}
110
+		}
111
+		return $handles;
112
+	}
113
+
114
+
115
+	/**
116
+	 * Registers inline script with translations for given handle and domain.
117
+	 *
118
+	 * @param string $handle       Handle used to register javascript file containing translations.
119
+	 * @param array  $translations Array of string translations.
120
+	 * @param string $domain       Domain for translations.  If left empty then strings are registered with the default
121
+	 *                             domain for the javascript.
122
+	 */
123
+	protected function registerInlineScript($handle, array $translations, $domain)
124
+	{
125
+		$script = $domain ?
126
+			'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ', "' . $domain . '" );' :
127
+			'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ' );';
128
+		wp_add_inline_script($handle, $script, 'before');
129
+	}
130
+
131
+
132
+	/**
133
+	 * Queues up the translation strings for the given handle.
134
+	 *
135
+	 * @param string $handle The script handle being queued up.
136
+	 */
137
+	private function queueI18nTranslationsForHandle($handle)
138
+	{
139
+		if (isset($this->queued_scripts[$handle])) {
140
+			$domain = $this->queued_scripts[$handle];
141
+			$translations = $this->getJedLocaleDataForDomainAndChunk($handle, $domain);
142
+			if (count($translations) > 0) {
143
+				$this->queued_handle_translations[$handle] = array(
144
+					'domain'       => $domain,
145
+					'translations' => $translations,
146
+				);
147
+			}
148
+			unset($this->queued_scripts[$handle]);
149
+		}
150
+	}
151
+
152
+
153
+	/**
154
+	 * Sets the internal i18n_map property.
155
+	 * If $chunk_map is empty or not an array, will attempt to load a chunk map from a default named map.
156
+	 *
157
+	 * @param array $i18n_map  If provided, an array of translation strings indexed by script handle names they
158
+	 *                         correspond to.
159
+	 */
160
+	private function setI18nMap(array $i18n_map)
161
+	{
162
+		if (empty($i18n_map)) {
163
+			$i18n_map = file_exists($this->domain->pluginPath() . 'translation-map.json')
164
+				? json_decode(
165
+						file_get_contents($this->domain->pluginPath() . 'translation-map.json'),
166
+						true
167
+					)
168
+				: array();
169
+		}
170
+		$this->i18n_map = $i18n_map;
171
+	}
172
+
173
+
174
+	/**
175
+	 * Get the jed locale data for a given $handle and domain
176
+	 *
177
+	 * @param string $handle The name for the script handle we want strings returned for.
178
+	 * @param string $domain The i18n domain.
179
+	 * @return array
180
+	 */
181
+	protected function getJedLocaleDataForDomainAndChunk($handle, $domain)
182
+	{
183
+		$translations = $this->getJedLocaleData($domain);
184
+		// get index for adding back after extracting strings for this $chunk.
185
+		$index = $translations[''];
186
+		$translations = $this->getLocaleDataMatchingMap(
187
+			$this->getOriginalStringsForHandleFromMap($handle),
188
+			$translations
189
+		);
190
+		$translations[''] = $index;
191
+		return $translations;
192
+	}
193
+
194
+
195
+	/**
196
+	 * Get locale data for given strings from given translations
197
+	 *
198
+	 * @param array $string_set   This is the subset of strings (msgIds) we want to extract from the translations array.
199
+	 * @param array $translations Translation data to extra strings from.
200
+	 * @return array
201
+	 */
202
+	protected function getLocaleDataMatchingMap(array $string_set, array $translations)
203
+	{
204
+		if (empty($string_set)) {
205
+			return array();
206
+		}
207
+		// some strings with quotes in them will break on the array_flip, so making sure quotes in the string are
208
+		// slashed also filter falsey values.
209
+		$string_set = array_unique(array_filter(wp_slash($string_set)));
210
+		return array_intersect_key($translations, array_flip($string_set));
211
+	}
212
+
213
+
214
+	/**
215
+	 * Get original strings to translate for the given chunk from the map
216
+	 *
217
+	 * @param string $handle The script handle name to get strings from the map for.
218
+	 * @return array
219
+	 */
220
+	protected function getOriginalStringsForHandleFromMap($handle)
221
+	{
222
+		return isset($this->i18n_map[$handle]) ? $this->i18n_map[$handle] : array();
223
+	}
224
+
225
+
226
+	/**
227
+	 * Returns Jed-formatted localization data.
228
+	 *
229
+	 * @param  string $domain Translation domain.
230
+	 * @return array
231
+	 */
232
+	private function getJedLocaleData($domain)
233
+	{
234
+		$translations = get_translations_for_domain($domain);
235
+
236
+		$locale = array(
237
+			'' => array(
238
+				'domain' => $domain,
239
+				'lang'   => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale()
240
+			),
241
+		);
242
+
243
+		if (! empty($translations->headers['Plural-Forms'])) {
244
+			$locale['']['plural_forms'] = $translations->headers['Plural-Forms'];
245
+		}
246
+
247
+		foreach ($translations->entries as $msgid => $entry) {
248
+			$locale[$msgid] = $entry->translations;
249
+		}
250
+
251
+		return $locale;
252
+	}
253 253
 }
254 254
\ No newline at end of file
Please login to merge, or discard this patch.