PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.15.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.15.1
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 / admin / option-page-dictionary.php

option-page-dictionary.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.15.1, at inc/admin/option-page-dictionary.php

619 lines 19.1 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 * Print HTML Option page : wpLingua Dictionary
11 *
12 * @return void
13 */
14 function wplng_option_page_dictionary() {
15
16 $entries_json = wplng_dictionary_get_entries_json();
17 $wplng_dictionary_updated_data = get_transient( 'wplng_dictionary_updated_data' );
18
19 delete_transient( 'wplng_dictionary_updated_data' );
20
21 ?>
22
23 <h1 class="wplng-option-page-title"><span class="dashicons dashicons-translation"></span> <?php esc_html_e( 'wpLingua - Dictionary rules', 'wplingua' ); ?></h1>
24
25 <div class="wrap">
26 <hr class="wp-header-end">
27 <form id="wplng-option-dictionary-form" method="post" action="options.php">
28 <?php
29 settings_fields( 'wplng_dictionary' );
30 do_settings_sections( 'wplng_dictionary' );
31 ?>
32 <table class="form-table wplng-form-table">
33
34 <?php
35
36 $style_section_entries_all = '';
37
38 if ( false !== $wplng_dictionary_updated_data ) {
39 $style_section_entries_all = 'display: none;';
40 wplng_option_page_dictionary_update_translations_html(
41 $wplng_dictionary_updated_data
42 );
43 }
44 ?>
45
46 <tr class="wplng-beta-hidden" style="display: none;">
47 <th scope="row"><span class="dashicons dashicons-printer"></span> <?php esc_html_e( 'Debug', 'wplingua' ); ?></th>
48 <td>
49 <fieldset>
50 <textarea name="wplng_dictionary_entries" id="wplng_dictionary_entries"><?php echo esc_textarea( $entries_json ); ?></textarea>
51 </fieldset>
52 </td>
53 </tr>
54
55 <tr id="wplng-section-entries-all" style="<?php echo esc_attr( $style_section_entries_all ); ?>">
56 <th scope="row"><span class="dashicons dashicons-book"></span> <?php esc_html_e( 'Dictionary entries', 'wplingua' ); ?></th>
57 <td>
58 <fieldset>
59
60 <p><strong><?php esc_html_e( 'Translation rules by dictionary: ', 'wplingua' ); ?></strong></p>
61
62 <p><?php esc_html_e( 'The dictionary allows you to define translation rules that apply when generating machine translations. You can specify words or sets of words that should never be translated, or define how they should be translated for each language.', 'wplingua' ); ?></p>
63
64 <hr>
65
66 <?php wplng_option_page_dictionary_entries_html(); ?>
67
68 <a href="javascript:void(0);" class="button button-primary" id="wplng-new-rule-button">
69 <?php esc_html_e( 'Add a dictionary entry', 'wplingua' ); ?>
70 </a>
71
72 </fieldset>
73 </td>
74 </tr>
75
76 <tr id="wplng-section-entry-new" style="display: none;">
77 <th scope="row"><span class="dashicons dashicons-welcome-add-page"></span> <?php esc_html_e( 'Add an entry', 'wplingua' ); ?></th>
78 <td class="wplng-flex-container">
79 <div id="wplng-dictionary-entry-new">
80 <?php wplng_option_page_dictionary_new_entry_html(); ?>
81 </div>
82 </td>
83 </tr>
84
85 <tr id="wplng-section-entry-edit" style="display: none;">
86 <th scope="row"><span class="dashicons dashicons-welcome-write-blog"></span> <?php esc_html_e( 'Edit the entry', 'wplingua' ); ?></th>
87 <td class="wplng-flex-container">
88 <div id="wplng-dictionary-entry-edit">
89 <?php wplng_option_page_dictionary_edit_entry_html(); ?>
90 </div>
91 </td>
92 </tr>
93 </table>
94
95 <?php submit_button(); ?>
96
97 </form>
98 </div>
99 <?php
100 }
101
102
103 /**
104 * Print HTML subsection of Option page : wpLingua Dictionary - Update translations
105 *
106 * Displays the list of translations impacted by the latest dictionary rule
107 * changes, along with the controls used to launch, ignore, or track the
108 * progress of the AJAX update queue.
109 *
110 * @param array $translations_to_update List of items with 'post_id', 'source' and 'impacted_languages' ('all' or an array of language IDs).
111 * @return void
112 */
113 function wplng_option_page_dictionary_update_translations_html( $translations_to_update ) {
114
115 /**
116 * HTML Buttons
117 */
118
119 $html_buttons = '<div class="wplng-flex-row wplng-dictionary-update-subsection-launch">';
120
121 $html_buttons .= '<div class="wplng-flex-item">';
122 $html_buttons .= '<a';
123 $html_buttons .= ' href="javascript:void(0);"';
124 $html_buttons .= ' class="button wplng-dictionary-update-button-ignore"';
125 $html_buttons .= '>';
126 $html_buttons .= esc_html__( 'Ignore', 'wplingua' );
127 $html_buttons .= '</a>';
128 $html_buttons .= '</div>'; // End .wplng-flex-item
129
130 $html_buttons .= '<div class="wplng-flex-item">';
131 $html_buttons .= '<a';
132 $html_buttons .= ' href="javascript:void(0);"';
133 $html_buttons .= ' class="button button-primary wplng-dictionary-update-button-start"';
134 $html_buttons .= '>';
135 $html_buttons .= esc_html__( 'Update translations', 'wplingua' );
136 $html_buttons .= '</a>';
137 $html_buttons .= '</div>'; // End .wplng-flex-item
138
139 $html_buttons .= '</div>'; // End .wplng-flex-row
140
141 /**
142 * HTML Section
143 */
144
145 $html = '<tr id="wplng-section-dictionary-update-translations">';
146
147 $html .= '<th scope="row">';
148 $html .= '<span class="dashicons dashicons-update"></span> ';
149 $html .= esc_html( 'Update translations', 'wplingua' );
150 $html .= '</th>'; // End .row
151
152 $html .= '<td class="wplng-flex-container">';
153 $html .= '<fieldset>';
154 $html .= '<label>';
155 $html .= '<strong>' . esc_html( 'Translations Affected by Dictionary Changes', 'wplingua' ) . '</strong>';
156 $html .= '</label>';
157 $html .= '<p>';
158 $html .= esc_html( 'Some translations have been identified as needing to be updated following the latest changes to the dictionary rules.', 'wplingua' );
159 $html .= '</p>';
160
161 $html .= '<hr>';
162
163 /**
164 * Subsection info before process
165 */
166
167 $html .= '<div class="wplng-dictionary-update-subsection-info">';
168 $html .= '<p>';
169 $html .= esc_html(
170 sprintf(
171 __( 'Number of translations to update: %d', 'wplingua' ),
172 count( $translations_to_update )
173 )
174 );
175 $html .= '</p>';
176 $html .= '</div>'; // End .wplng-dictionary-update-subsection-info
177
178 /**
179 * Subsection info in process
180 */
181
182 $progression_info = '<span class="wplng-count-processed">0</span>';
183 $progression_info .= ' / ' . count( $translations_to_update );
184 // $progression_info .= ' - <span class="wplng-count-percent">0 %</span>';
185
186 $html .= '<div class="wplng-dictionary-update-subsection-info-progress" style="display: none;">';
187 $html .= '<p>';
188 $html .= esc_html__( 'Translations are currently being updated.', 'wplingua' );
189 $html .= '</p>';
190 $html .= '<p>';
191 $html .= sprintf(
192 __( 'Number of translations updated: %1$s', 'wplingua' ),
193 $progression_info
194 );
195 $html .= '</p>';
196 $html .= '</div>'; // End .wplng-dictionary-update-subsection-info
197
198 /**
199 * Subsection info before process
200 */
201
202 $html .= '<div class="wplng-dictionary-update-subsection-info-end" style="display: none;">';
203 $html .= '<p>';
204 $html .= esc_html__( 'End of translations update.', 'wplingua' );
205 $html .= '</p>';
206 $html .= '<a';
207 $html .= ' href="javascript:void(0);"';
208 $html .= ' class="button button-primary wplng-dictionary-update-button-end"';
209 $html .= '>';
210 $html .= esc_html__( 'Back to the dictionary', 'wplingua' );
211 $html .= '</a>';
212 $html .= '</div>'; // End .wplng-dictionary-update-subsection-info
213
214 $html .= $html_buttons;
215
216 $html .= '<hr>';
217 $html .= '<p>';
218 $html .= '<strong>' . esc_html( 'Translations to update: ', 'wplingua' ) . '</strong>';
219 $html .= '</p>';
220
221 foreach ( $translations_to_update as $key => $translation_to_update ) {
222
223 if ( ! isset( $translation_to_update['post_id'] )
224 || ! isset( $translation_to_update['source'] )
225 || ! isset( $translation_to_update['impacted_languages'] )
226 || ( $translation_to_update['impacted_languages'] !== 'all'
227 && ! is_array( $translation_to_update['impacted_languages'] )
228 )
229 ) {
230 continue;
231 }
232
233 // Write 'all' as a plain string so jQuery's .data() returns the string 'all'
234 // (jQuery only auto-parses values starting with '{' or '[').
235 // Arrays are JSON-encoded so jQuery parses them back into JS arrays.
236 $impacted_languages_attr = 'all';
237 if ( $translation_to_update['impacted_languages'] !== 'all' ) {
238 $impacted_languages_attr = wp_json_encode( $translation_to_update['impacted_languages'] );
239 }
240
241 $check = wplng_encryption_encrypt( $translation_to_update['post_id'] . '-' . $impacted_languages_attr);
242
243 $html .= '<div';
244 $html .= ' class="wplng-dictionary-text-to-update-entry"';
245 $html .= ' data-post-id="' . esc_attr( $translation_to_update['post_id'] ) . '"';
246 $html .= ' data-impacted-languages="' . esc_attr( $impacted_languages_attr ) . '"';
247 $html .= ' data-check="' . esc_attr( $check ) . '"';
248 $html .= '>';
249
250 // $html .= '<span class="wplng-dictionary-update-state dashicons dashicons-yes-alt"></span>';
251 $html .= '<span class="wplng-dictionary-update-state dashicons"></span>';
252
253 $html .= '<strong>';
254 $html .= ' | ' . esc_html__( 'ID: ', 'wplingua' ) . esc_html( $translation_to_update['post_id'] );
255 $html .= '</strong>';
256
257 $html .= '<hr>';
258
259 // Truncate source at the last word boundary before 200 characters.
260 $source_display = $translation_to_update['source'];
261 if ( mb_strlen( $source_display ) > 200 ) {
262 $source_display = mb_substr( $source_display, 0, 200 );
263 $last_space = mb_strrpos( $source_display, ' ' );
264 if ( false !== $last_space ) {
265 $source_display = mb_substr( $source_display, 0, $last_space );
266 }
267 $source_display .= '';
268 }
269 $html .= '<p class="wplng-dictionary-text-to-update">';
270 $html .= esc_html( $source_display );
271 $html .= '</p>';
272
273 $html .= '</div>'; // End .wplng-dictionary-text-to-update-entry
274 }
275
276 $html .= '</fieldset>';
277 $html .= '</td>';
278 $html .= '</tr>';
279
280 echo $html;
281 }
282
283
284 /**
285 * Print HTML subsection of Option page : wpLingua Dictionary - Entries
286 *
287 * @return void
288 */
289 function wplng_option_page_dictionary_entries_html() {
290
291 $dictionary_entries = wplng_dictionary_get_entries();
292
293 if ( empty( $dictionary_entries ) ) {
294 return '';
295 }
296
297 $language_website = wplng_get_language_website();
298 $language_website_html = '<img';
299 $language_website_html .= ' src="' . esc_url( $language_website['flag'] ) . '"';
300 $language_website_html .= ' alt="' . esc_attr( $language_website['name'] ) . '"';
301 $language_website_html .= ' class="wplng-flag"';
302 $language_website_html .= '>';
303
304 $html = '<label><strong>';
305 $html .= esc_html__( 'All dictionary entries: ', 'wplingua' );
306 $html .= '</strong></label>';
307 $html .= '<br>';
308 $html .= '<div id="wplng-dictionary-entries">';
309
310 foreach ( $dictionary_entries as $rule_number => $entry ) {
311
312 $html .= '<div';
313 $html .= ' class="wplng-dictionary-entry"';
314 $html .= ' wplng-rule="' . esc_attr( $rule_number ) . '"';
315 $html .= '>';
316
317 $html .= '<div class="wplng-rule-header">';
318
319 $html .= '<div class="wplng-rule-name">';
320 $html .= esc_html__( 'Rule N°', 'wplingua' );
321 $html .= esc_html( $rule_number + 1 );
322 $html .= '</div>'; // ENd .wplng-rule-name
323
324 $html .= '<div class="wplng-rule-action">';
325 $html .= '<a';
326 $html .= ' href="javascript:void(0);"';
327 $html .= ' class="wplng-rule-link-edit"';
328 $html .= ' wplng-rule="' . esc_attr( $rule_number ) . '"';
329 $html .= '>';
330 $html .= esc_html__( 'Edit', 'wplingua' );
331 $html .= '</a> ';
332 $html .= '<a';
333 $html .= ' href="javascript:void(0);"';
334 $html .= ' class="wplng-rule-link-remove"';
335 $html .= ' wplng-rule="' . esc_attr( $rule_number ) . '"';
336 $html .= '>';
337 $html .= esc_html__( 'Remove', 'wplingua' );
338 $html .= '</a>';
339 $html .= '</div>'; // .wplng-rule-action
340
341 $html .= '</div>'; // End .wplng-rule-header
342
343 $html .= '<hr>';
344
345 if ( isset( $entry['rules'] ) ) {
346
347 $html .= '<strong>';
348 $html .= $language_website_html;
349 $html .= esc_html__( 'Always translate: ', 'wplingua' );
350 $html .= '</strong>';
351 $html .= esc_html( $entry['source'] );
352
353 foreach ( $entry['rules'] as $language_id => $rule ) {
354
355 $language = wplng_get_language_by_id( $language_id );
356
357 $html .= '<hr>';
358 $html .= '<strong>';
359 $html .= '<img';
360 $html .= ' src="' . esc_url( $language['flag'] ) . '"';
361 $html .= ' alt="' . esc_attr( $language['name'] ) . '"';
362 $html .= ' class="wplng-flag"';
363 $html .= '>';
364 $html .= esc_html( $language['name'] );
365 $html .= esc_html__( ' - By: ', 'wplingua' );
366 $html .= '</strong>';
367 $html .= esc_html( $rule );
368 }
369 } else {
370 $html .= '<strong>';
371 $html .= esc_html__( 'Never translate: ', 'wplingua' );
372 $html .= '</strong>';
373 $html .= esc_html( $entry['source'] );
374 }
375
376 $html .= '</div>'; // End .wplng-dictionary-entry
377 }
378
379 $html .= '</div>'; // End #wplng-dictionary-entries
380
381 echo $html;
382 }
383
384
385 /**
386 * Print HTML subsection of Option page : wpLingua Dictionary - New entry
387 *
388 * @return void
389 */
390 function wplng_option_page_dictionary_new_entry_html() {
391
392 $html = '';
393
394 /**
395 * Input : Source
396 */
397
398 $language_website = wplng_get_language_website();
399 $language_website_html = '<img';
400 $language_website_html .= ' src="' . esc_url( $language_website['flag'] ) . '"';
401 $language_website_html .= ' alt="' . esc_attr( $language_website['name'] ) . '"';
402 $language_website_html .= ' class="wplng-flag"';
403 $language_website_html .= '>';
404
405 $html .= '<fieldset>';
406 $html .= '<label for="wplng-new-source">';
407 $html .= '<strong>';
408 $html .= $language_website_html;
409 $html .= esc_html__( 'Source text: ', 'wplingua' );
410 $html .= '</strong>';
411 $html .= '</label>';
412 $html .= '<br>';
413 $html .= '<textarea';
414 $html .= ' name="wplng-new-source"';
415 $html .= ' id="wplng-new-source"';
416 $html .= ' class="wplng-adaptive-textarea"';
417 $html .= ' maxlength="256"';
418 $html .= '>';
419 $html .= '</textarea>';
420 $html .= '</fieldset>';
421
422 /**
423 * Input : Never translate
424 */
425
426 $html .= '<fieldset>';
427 $html .= '<input';
428 $html .= ' type="checkbox"';
429 $html .= ' id="wplng-new-never-translate"';
430 $html .= ' name="wplng-new-never-translate"';
431 $html .= '>';
432 $html .= '<label for="wplng-new-never-translate"> ';
433 $html .= esc_html__( 'Never translate', 'wplingua' );
434 $html .= '</label>';
435 $html .= '</fieldset>';
436
437 $language_target = wplng_get_languages_target();
438
439 $html .= '<div id="wplng-new-rules">';
440 foreach ( $language_target as $language ) {
441
442 $name = 'wplng-new-always-translate-' . $language['id'];
443
444 $html .= '<div class="wplng-new-rule" wplng-rule="' . esc_html( $language['id'] ) . '">';
445 $html .= '<hr>';
446 $html .= '<fieldset>';
447 $html .= '<label for="' . esc_attr( $name ) . '">';
448 $html .= '<strong>';
449 $html .= '<img';
450 $html .= ' src="' . esc_url( $language['flag'] ) . '"';
451 $html .= ' alt="' . esc_attr( $language['name'] ) . '"';
452 $html .= ' class="wplng-flag"';
453 $html .= '>';
454 $html .= esc_html( $language['name'] );
455 $html .= esc_html__( ' - Always translate by: ', 'wplingua' );
456 $html .= '</strong>';
457 $html .= '</label>';
458
459 $html .= '<br>';
460
461 $html .= '<textarea';
462 $html .= ' name="' . esc_attr( $name ) . '"';
463 $html .= ' id="' . esc_attr( $name ) . '"';
464 $html .= ' class="wplng-adaptive-textarea"';
465 $html .= ' maxlength="256"';
466 $html .= '>';
467 $html .= '</textarea>';
468
469 $html .= '</fieldset>';
470 $html .= '</div>';
471
472 }
473 $html .= '</div>'; // End #wplng-new-rules
474
475 $html .= '<div id="wplng-new-action-section" class="wplng-flex-row">';
476
477 $html .= '<div class="wplng-flex-item">';
478 $html .= '<a';
479 $html .= ' href="javascript:void(0);"';
480 $html .= ' id="wplng-new-cancel-button"';
481 $html .= ' class="button "';
482 $html .= '>';
483 $html .= esc_html__( 'Cancel', 'wplingua' );
484 $html .= '</a>';
485 $html .= '</div>'; // End .wplng-flex-item
486
487 $html .= '<div class="wplng-flex-item">';
488 $html .= '<a';
489 $html .= ' href="javascript:void(0);"';
490 $html .= ' id="wplng-new-add-button"';
491 $html .= ' class="button button-primary"';
492 $html .= '>';
493 $html .= esc_html__( 'Save new entry', 'wplingua' );
494 $html .= '</a>';
495 $html .= '</div>'; // End .wplng-flex-item
496
497 $html .= '</div>'; // End #wplng-new-action-section
498
499 echo $html;
500 }
501
502
503 /**
504 * Print HTML subsection of Option page : wpLingua Dictionary - Edit entry
505 *
506 * @return void
507 */
508 function wplng_option_page_dictionary_edit_entry_html() {
509
510 $html = '';
511
512 /**
513 * Input : Source
514 */
515
516 $language_website = wplng_get_language_website();
517 $language_website_html = '<img';
518 $language_website_html .= ' src="' . esc_url( $language_website['flag'] ) . '"';
519 $language_website_html .= ' alt="' . esc_attr( $language_website['name'] ) . '"';
520 $language_website_html .= ' class="wplng-flag"';
521 $language_website_html .= '>';
522
523 $html .= '<fieldset>';
524 $html .= '<label for="wplng-edit-source">';
525 $html .= '<strong>';
526 $html .= $language_website_html;
527 $html .= esc_html__( 'Source text: ', 'wplingua' );
528 $html .= '</strong>';
529 $html .= '</label>';
530 $html .= '<br>';
531 $html .= '<textarea';
532 $html .= ' name="wplng-edit-source"';
533 $html .= ' id="wplng-edit-source"';
534 $html .= ' class="wplng-adaptive-textarea"';
535 $html .= ' maxlength="256"';
536 $html .= '>';
537 $html .= '</textarea>';
538 $html .= '</fieldset>';
539
540 /**
541 * Input : Never translate
542 */
543
544 $html .= '<fieldset>';
545 $html .= '<input';
546 $html .= ' type="checkbox"';
547 $html .= ' id="wplng-edit-never-translate"';
548 $html .= ' name="wplng-edit-never-translate"';
549 $html .= '>';
550 $html .= '<label for="wplng-edit-never-translate"> ';
551 $html .= esc_html__( 'Never translate', 'wplingua' );
552 $html .= '</label>';
553 $html .= '</fieldset>';
554
555 $language_target = wplng_get_languages_target();
556
557 $html .= '<div id="wplng-edit-rules">';
558 foreach ( $language_target as $language ) {
559
560 $name = 'wplng-edit-always-translate-' . $language['id'];
561
562 $html .= '<div class="wplng-edit-rule" wplng-rule="' . esc_html( $language['id'] ) . '">';
563 $html .= '<hr>';
564 $html .= '<fieldset>';
565 $html .= '<label for="' . esc_attr( $name ) . '">';
566 $html .= '<strong>';
567 $html .= '<img';
568 $html .= ' src="' . esc_url( $language['flag'] ) . '"';
569 $html .= ' alt="' . esc_attr( $language['name'] ) . '"';
570 $html .= ' class="wplng-flag"';
571 $html .= '>';
572 $html .= esc_html( $language['name'] );
573 $html .= esc_html__( ' - Always translate by: ', 'wplingua' );
574 $html .= '</strong>';
575 $html .= '</label>';
576
577 $html .= '<br>';
578
579 $html .= '<textarea';
580 $html .= ' name="' . esc_attr( $name ) . '"';
581 $html .= ' id="' . esc_attr( $name ) . '"';
582 $html .= ' class="wplng-adaptive-textarea"';
583 $html .= ' maxlength="256"';
584 $html .= '>';
585 $html .= '</textarea>';
586
587 $html .= '</fieldset>';
588 $html .= '</div>';
589
590 }
591 $html .= '</div>';
592
593 $html .= '<div id="wplng-edit-action-section" class="wplng-flex-row">';
594
595 $html .= '<div class="wplng-flex-item">';
596 $html .= '<a';
597 $html .= ' href="javascript:void(0);"';
598 $html .= ' id="wplng-edit-cancel-button"';
599 $html .= ' class="button "';
600 $html .= '>';
601 $html .= esc_html__( 'Cancel', 'wplingua' );
602 $html .= '</a>';
603 $html .= '</div>'; // End .wplng-flex-item
604
605 $html .= '<div class="wplng-flex-item">';
606 $html .= '<a';
607 $html .= ' href="javascript:void(0);"';
608 $html .= ' id="wplng-edit-save-button"';
609 $html .= ' class="button button-primary"';
610 $html .= '>';
611 $html .= esc_html__( 'Save edited entry', 'wplingua' );
612 $html .= '</a>';
613 $html .= '</div>'; // End .wplng-flex-item
614
615 $html .= '</div>'; // End #wplng-edit-action-section
616
617 echo $html;
618 }
619