PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.4
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.4
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.16.4, at inc/admin/option-page-dictionary.php

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