PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.7
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / translator / js.php

js.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.16.7, at inc/translator/js.php

272 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * wpLingua translate : Get translated JS
11 *
12 * This function translates JavaScript code containing JSON objects.
13 * It uses regex to find JavaScript variables and translates the JSON part.
14 *
15 * @param string $js The JavaScript code to translate.
16 * @param array $args Additional arguments for translation processing.
17 * @return string The translated JavaScript code.
18 */
19 function wplng_translate_js( $js, $args = array() ) {
20
21 // Return early if the provided JavaScript is empty
22 // or consists only of whitespace
23 // Or is the wp emoji script
24 if ( empty( trim( $js ) )
25 || wplng_str_contains( $js, '_wpemojiSettings' )
26 ) {
27 return $js;
28 }
29
30 $js = wplng_translate_js_json_in_var( $js, $args );
31 $js = wplng_translate_js_json_encoded_as_url( $js, $args );
32 $js = wplng_translate_js_json_in_function_call( $js, $args );
33
34 if ( wplng_str_is_script_i18n( $js ) ) {
35 $js = wplng_translate_js_json_in_i18n_script( $js, $args );
36 }
37
38 return $js; // Return the translated JavaScript
39 }
40
41
42 /**
43 * Translate JSON contained in 'var', 'let' or 'window._'
44 *
45 * @param string $js The JavaScript code to translate.
46 * @param array $args Additional arguments for translation processing.
47 * @return string The translated JavaScript code.
48 */
49 function wplng_translate_js_json_in_var( $js, $args = array() ) {
50
51 // Array to hold matched JSON objects
52 $json = array();
53
54 preg_match_all(
55 '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\}|\[(?:[^\[\]"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\])\s*;#Us',
56 $js,
57 $json
58 );
59
60 if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
61 foreach ( $json[2] as $key => $var_name ) {
62
63 $var_name = trim( $var_name );
64
65 if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
66 continue;
67 }
68
69 // Get the JSON string
70 $var_json = trim( $json[3][ $key ] );
71
72 // Prepare arguments for translation
73 wplng_args_setup( $args );
74 $args['parents'] = array( $var_name );
75
76 // Translate the JSON string
77 $json_translated = wplng_translate_json(
78 $var_json,
79 $args
80 );
81
82 // Replace the original JSON with the translated version if different
83 if ( $var_json !== $json_translated ) {
84 $js = str_replace(
85 $var_json,
86 $json_translated,
87 $js
88 );
89 }
90 }
91 }
92
93 return $js; // Return the translated JavaScript
94 }
95
96
97 /**
98 * Translate JSON in i18n script
99 *
100 * You can use wplng_parse_js_json_in_i18n_script( $js )
101 * to check if the string is a i18n script
102 *
103 * @param string $js The JavaScript code to translate.
104 * @param array $args Additional arguments for translation processing.
105 * @return string The translated JavaScript code.
106 */
107 function wplng_translate_js_json_in_i18n_script( $js, $args = array() ) {
108
109 $json = array();
110
111 preg_match_all(
112 '#\(\s?["\'](.*)["\'],\s?(.*)\s?\);#Ui',
113 $js,
114 $json
115 );
116
117 if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
118 foreach ( $json[1] as $key => $var_name ) {
119
120 $var_name = trim( $var_name );
121
122 if ( empty( $var_name ) || empty( $json[2][ $key ] ) ) {
123 continue;
124 }
125
126 $var_json = trim( $json[2][ $key ] );
127
128 // Prepare arguments for translation
129 wplng_args_setup( $args );
130 $args['parents'] = array( 'i18n_script', $var_name );
131
132 // Translate the JSON string
133 $json_translated = wplng_translate_json(
134 $var_json,
135 $args
136 );
137
138 // Replace the original JSON with the translated version if different
139 if ( $var_json !== $json_translated ) {
140 $js = str_replace(
141 $var_json,
142 $json_translated,
143 $js
144 );
145 }
146 }
147 }
148
149 return $js; // Return the translated JavaScript
150 }
151
152
153 /**
154 * Translate JSON encoded as URL
155 *
156 * @param string $js The JavaScript code to translate.
157 * @param array $args Additional arguments for translation processing.
158 * @return string The translated JavaScript code.
159 */
160 function wplng_translate_js_json_encoded_as_url( $js, $args = array() ) {
161
162 $json = array();
163
164 preg_match_all(
165 '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?JSON\.parse\(\sdecodeURIComponent\(\s\'(.*)\'\s\)\s\)#Ui',
166 $js,
167 $json
168 );
169
170 if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
171 foreach ( $json[2] as $key => $var_name ) {
172
173 $var_name = trim( $var_name );
174
175 if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
176 continue;
177 }
178
179 $encoded_json = trim( $json[3][ $key ] );
180 $var_json = urldecode( $encoded_json );
181
182 // Prepare arguments for translation
183 wplng_args_setup( $args );
184 $args['parents'] = array( 'encoded_as_url', $var_name );
185
186 // Translate the JSON string
187 $json_translated = wplng_translate_json(
188 $var_json,
189 $args
190 );
191
192 $json_translated = rawurlencode( $json_translated );
193
194 // Replace the original JSON with the translated version if different
195 if ( $encoded_json !== $json_translated ) {
196 $js = str_replace(
197 $encoded_json,
198 $json_translated,
199 $js
200 );
201 }
202 }
203 }
204
205 return $js; // Return the translated JavaScript
206 }
207
208
209 /**
210 * Translate JSON passed as argument to function calls like jQuery.datepicker.setDefaults({...})
211 *
212 * @param string $js The JavaScript code to translate.
213 * @param array $args Additional arguments for translation processing.
214 * @return string The translated JavaScript code.
215 */
216 function wplng_translate_js_json_in_function_call( $js, $args = array() ) {
217
218 $json = array();
219
220 // Whitelist of function calls that contain translatable JSON
221 $allowed_functions = wplng_data_json_in_js_functions();
222
223 if ( empty( $allowed_functions ) ) {
224 return $js;
225 }
226
227 preg_match_all(
228 '#([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\s*\(\s*(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?2))*\})\s*\)#Us',
229 $js,
230 $json
231 );
232
233 if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
234 foreach ( $json[1] as $key => $func_name ) {
235
236 $func_name = trim( $func_name );
237
238 // Skip if not in whitelist
239 if ( ! in_array( $func_name, $allowed_functions, true ) ) {
240 continue;
241 }
242
243 if ( empty( $json[2][ $key ] ) ) {
244 continue;
245 }
246
247 $var_json = trim( $json[2][ $key ] );
248
249 // Prepare arguments for translation
250 wplng_args_setup( $args );
251 $args['parents'] = array( $func_name );
252
253 // Translate the JSON string
254 $json_translated = wplng_translate_json(
255 $var_json,
256 $args
257 );
258
259 // Replace the original JSON with the translated version if different
260 if ( $var_json !== $json_translated ) {
261 $js = str_replace(
262 $var_json,
263 $json_translated,
264 $js
265 );
266 }
267 }
268 }
269
270 return $js; // Return the translated JavaScript
271 }
272