forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
edit_languages.php
1528 lines (1420 loc) · 55 KB
/
edit_languages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* \file
* \brief Manage languages
*
* Call: edit_languages.php?....
* ... refresh=[langid] ... reparse all texts in lang
* ... del=[langid] ... do delete
* ... op=Save ... do insert new
* ... op=Change ... do update
* ... new=1 ... display new lang. screen
* ... chg=[langid] ... display edit screen
*
* PHP version 8.1
*
* @category User_Interface
* @package Lwt
* @author LWT Project <[email protected]>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/edit-languages.html
* @since 1.0.3
* @since 2.4.0 Refactored with functional paradigm
*/
require_once 'inc/session_utility.php';
require_once 'inc/langdefs.php';
require_once 'inc/classes/Language.php';
/**
* Prepare a JavaScript code that checks for duplicate names in languages.
*
* @return void
*/
function edit_languages_alert_duplicate()
{
?>
<script type="text/javascript">
//<![CDATA[
var LANGUAGES = <?php echo json_encode(get_languages()); ?>;
/// Check if langname exists and its lang# != curr
function check_dupl_lang(curr) {
const l = $('#LgName').val();
if (l in LANGUAGES) {
if (curr != LANGUAGES[l]) {
alert('Language "' + l +
'" already exists. Please change the language name!');
$('#LgName').focus();
return false;
}
}
return true;
}
//]]>
</script>
<?php
}
/**
* Refresh sentence and text items from a specific language.
*
* @param int $lid Language ID
*
* @return string Number of sentences and textitems refreshed
*
* @global string $tbpref Database table prefix
*/
function edit_languages_refresh($lid): string
{
global $tbpref;
$message2 = runsql(
"DELETE FROM {$tbpref}sentences where SeLgID = $lid",
"Sentences deleted"
);
$message3 = runsql(
"DELETE FROM {$tbpref}textitems2 where Ti2LgID = $lid",
"Text items deleted"
);
adjust_autoincr('sentences', 'SeID');
$sql = "select TxID, TxText from " . $tbpref . "texts
where TxLgID = " . $lid . "
order by TxID";
$res = do_mysqli_query($sql);
while ($record = mysqli_fetch_assoc($res)) {
$txtid = (int)$record["TxID"];
$txttxt = $record["TxText"];
splitCheckText($txttxt, $lid, $txtid);
}
mysqli_free_result($res);
$message = $message2 .
" / " . $message3 .
" / Sentences added: " . get_first_value(
"SELECT count(*) as value
FROM {$tbpref}sentences
where SeLgID = $lid"
) .
" / Text items added: " . get_first_value(
"SELECT count(*) as value
FROM {$tbpref}textitems2
where Ti2LgID = $lid"
);
return $message;
}
/**
* Delete a language.
*
* @param int $lid Language ID
*
* @return string Info on the number of languages deleted
*
* @global string $tbpref Database table prefix
*/
function edit_languages_delete($lid): string
{
global $tbpref;
$anztexts = get_first_value(
"SELECT count(TxID) as value
FROM {$tbpref}texts
where TxLgID = $lid"
);
$anzarchtexts = get_first_value(
"SELECT count(AtID) as value
FROM {$tbpref}archivedtexts
where AtLgID = $lid"
);
$anzwords = get_first_value(
"SELECT count(WoID) as value
FROM {$tbpref}words
where WoLgID = $lid"
);
$anzfeeds = get_first_value(
"SELECT count(NfID) as value
FROM {$tbpref}newsfeeds
where NfLgID = $lid"
);
if ($anztexts > 0 || $anzarchtexts > 0 || $anzwords > 0 || $anzfeeds > 0) {
$message = 'You must first delete texts, archived texts, newsfeeds and words with this language!';
} else {
$message = runsql(
"DELETE FROM {$tbpref}languages
WHERE LgID = $lid",
"Deleted"
);
}
return $message;
}
/**
* Save a new language to the database.
*
* @return string Success or error message
*
* @global string $tbpref Database table prefix
*/
function edit_languages_op_save(): string
{
global $tbpref;
$val = get_first_value(
"SELECT MIN(LgID) AS value FROM {$tbpref}languages WHERE LgName=''"
);
if (!isset($val)) {
$message = runsql(
"INSERT INTO {$tbpref}languages (
LgName, LgDict1URI, LgDict2URI, LgGoogleTranslateURI,
LgExportTemplate, LgTextSize, LgCharacterSubstitutions,
LgRegexpSplitSentences, LgExceptionsSplitSentences,
LgRegexpWordCharacters, LgRemoveSpaces, LgSplitEachChar,
LgRightToLeft, LgTTSVoiceAPI, LgShowRomanization
) VALUES(" .
convert_string_to_sqlsyntax($_REQUEST["LgName"]) . ', ' .
convert_string_to_sqlsyntax($_REQUEST["LgDict1URI"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgDict2URI"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgGoogleTranslateURI"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgExportTemplate"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgTextSize"]) . ', '.
convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgCharacterSubstitutions"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgRegexpSplitSentences"]) . ', '.
convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgExceptionsSplitSentences"]) . ', '.
convert_string_to_sqlsyntax($_REQUEST["LgRegexpWordCharacters"]) . ', '.
((int)isset($_REQUEST["LgRemoveSpaces"])) . ', '.
((int)isset($_REQUEST["LgSplitEachChar"])) . ', '.
((int)isset($_REQUEST["LgRightToLeft"])) . ', ' .
convert_string_to_sqlsyntax_nonull($_REQUEST["LgTTSVoiceAPI"]) . ', ' .
((int)isset($_REQUEST["LgShowRomanization"])) .
')',
'Saved'
);
} else {
$message = runsql(
"UPDATE {$tbpref}languages SET " .
'LgName = ' . convert_string_to_sqlsyntax($_REQUEST["LgName"]) . ', ' .
'LgDict1URI = ' . convert_string_to_sqlsyntax($_REQUEST["LgDict1URI"]) . ', ' .
'LgDict2URI = ' . convert_string_to_sqlsyntax($_REQUEST["LgDict2URI"]) . ', ' .
'LgGoogleTranslateURI = ' . convert_string_to_sqlsyntax($_REQUEST["LgGoogleTranslateURI"]) . ', ' .
'LgExportTemplate = ' . convert_string_to_sqlsyntax($_REQUEST["LgExportTemplate"]) . ', ' .
'LgTextSize = ' . convert_string_to_sqlsyntax($_REQUEST["LgTextSize"]) . ', ' .
'LgCharacterSubstitutions = ' . convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgCharacterSubstitutions"]) . ', ' .
'LgRegexpSplitSentences = ' . convert_string_to_sqlsyntax($_REQUEST["LgRegexpSplitSentences"]) . ', ' .
'LgExceptionsSplitSentences = ' . convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgExceptionsSplitSentences"]) . ', ' .
'LgRegexpWordCharacters = ' . convert_string_to_sqlsyntax($_REQUEST["LgRegexpWordCharacters"]) . ', ' .
'LgRemoveSpaces = ' . ((int)isset($_REQUEST["LgRemoveSpaces"])) . ', ' .
'LgSplitEachChar = ' . ((int)isset($_REQUEST["LgSplitEachChar"])) . ', ' .
'LgRightToLeft = ' . ((int)isset($_REQUEST["LgRightToLeft"])) . ', ' .
"LgTTSVoiceAPI = " . convert_string_to_sqlsyntax_nonull($_REQUEST["LgTTSVoiceAPI"]) . ', ' .
"LgShowRomanization = " . ((int)isset($_REQUEST["LgShowRomanization"])) .
" WHERE LgID = $val",
'Saved'
);
}
return $message;
}
/**
* Edit an existing text in the database.
*
* @param int $lid Language ID
*
* @return string Number of texts updated and items reparsed.
*
* @global string $tbpref Database table prefix
*/
function edit_languages_op_change($lid): string
{
global $tbpref;
// Get old values
$sql = "SELECT * FROM {$tbpref}languages where LgID = $lid";
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
if ($record == false) {
my_die("Cannot access language data: $sql");
}
$needReParse = (
convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgCharacterSubstitutions"])
!= convert_string_to_sqlsyntax_notrim_nonull($record['LgCharacterSubstitutions'])
) || (
convert_string_to_sqlsyntax($_REQUEST["LgRegexpSplitSentences"]) !=
convert_string_to_sqlsyntax($record['LgRegexpSplitSentences'])
) || (
convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgExceptionsSplitSentences"])
!= convert_string_to_sqlsyntax_notrim_nonull($record['LgExceptionsSplitSentences'])
) || (
convert_string_to_sqlsyntax($_REQUEST["LgRegexpWordCharacters"]) !=
convert_string_to_sqlsyntax($record['LgRegexpWordCharacters'])
) || ((isset($_REQUEST["LgRemoveSpaces"]) ? 1 : 0) != $record['LgRemoveSpaces']) ||
((isset($_REQUEST["LgSplitEachChar"]) ? 1 : 0) != $record['LgSplitEachChar']);
mysqli_free_result($res);
$message = runsql(
"UPDATE {$tbpref}languages SET " .
'LgName = ' . convert_string_to_sqlsyntax($_REQUEST["LgName"]) . ', ' .
'LgDict1URI = ' . convert_string_to_sqlsyntax($_REQUEST["LgDict1URI"]) . ', ' .
'LgDict2URI = ' . convert_string_to_sqlsyntax($_REQUEST["LgDict2URI"]) . ', ' .
'LgGoogleTranslateURI = ' . convert_string_to_sqlsyntax($_REQUEST["LgGoogleTranslateURI"]) . ', ' .
'LgExportTemplate = ' . convert_string_to_sqlsyntax($_REQUEST["LgExportTemplate"]) . ', ' .
'LgTextSize = ' . convert_string_to_sqlsyntax($_REQUEST["LgTextSize"]) . ', ' .
'LgCharacterSubstitutions = ' . convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgCharacterSubstitutions"]) . ', ' .
'LgRegexpSplitSentences = ' . convert_string_to_sqlsyntax($_REQUEST["LgRegexpSplitSentences"]) . ', ' .
'LgExceptionsSplitSentences = ' . convert_string_to_sqlsyntax_notrim_nonull($_REQUEST["LgExceptionsSplitSentences"]) . ', ' .
'LgRegexpWordCharacters = ' . convert_string_to_sqlsyntax($_REQUEST["LgRegexpWordCharacters"]) . ', ' .
'LgRemoveSpaces = ' . ((int)isset($_REQUEST["LgRemoveSpaces"])) . ', ' .
'LgSplitEachChar = ' . ((int)isset($_REQUEST["LgSplitEachChar"])) . ', ' .
'LgRightToLeft = ' . ((int)isset($_REQUEST["LgRightToLeft"])) . ', ' .
'LgTTSVoiceAPI = ' . convert_string_to_sqlsyntax_nonull($_REQUEST["LgTTSVoiceAPI"]) . ', ' .
'LgShowRomanization = ' . ((int)isset($_REQUEST["LgShowRomanization"])) .
" WHERE LgID = $lid",
'Updated'
);
if ($needReParse) {
runsql(
"DELETE FROM {$tbpref}sentences where SeLgID = $lid",
"Sentences deleted"
);
runsql(
"DELETE FROM {$tbpref}textitems2 where Ti2LgID = $lid",
"Text items deleted"
);
adjust_autoincr('sentences', 'SeID');
runsql(
"UPDATE {$tbpref}words SET WoWordCount = 0 WHERE WoLgID = $lid",
''
);
init_word_count();
$sql = "SELECT TxID, TxText FROM {$tbpref}texts
WHERE TxLgID = $lid ORDER BY TxID";
$res = do_mysqli_query($sql);
$cntrp = 0;
while ($record = mysqli_fetch_assoc($res)) {
$txtid = (int)$record["TxID"];
$txttxt = $record["TxText"];
splitCheckText($txttxt, $lid, $txtid);
$cntrp++;
}
mysqli_free_result($res);
$message .= " / Reparsed texts: " . $cntrp;
} else {
$message .= " / Reparsing not needed";
}
return $message;
}
/**
* Load a language object based in language ID.
*
* @param int $lgid Language ID, if 0 load empty data.
*
* @return Language Created object
*
* @global string $tbpref
*/
function load_language($lgid)
{
global $tbpref;
$language = new Language();
$language->id = $lgid;
if ($lgid == 0) {
// Special case: set all to empty
$language->dict1uri = "";
$language->dict2uri = "";
$language->translator = "";
$language->exporttemplate = "";
$language->textsize = 100;
$language->charactersubst = "";
$language->regexpsplitsent = "";
$language->exceptionsplitsent = "";
$language->regexpwordchar = "";
$language->removespaces = null;
$language->spliteachchar = null;
$language->rightoleft = null;
$language->ttsvoiceapi = "";
$language->showromanization = true;
} else {
// Load data from database
$sql = "SELECT * FROM {$tbpref}languages WHERE LgID = $lgid";
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
$language->name = $record["LgName"];
$language->dict1uri = $record["LgDict1URI"];
$language->dict2uri = $record["LgDict2URI"];
$language->translator = $record["LgGoogleTranslateURI"];
$language->exporttemplate = $record["LgExportTemplate"];
$language->textsize = (int) $record["LgTextSize"];
$language->charactersubst = $record["LgCharacterSubstitutions"];
$language->regexpsplitsent = $record["LgRegexpSplitSentences"];
$language->exceptionsplitsent = $record["LgExceptionsSplitSentences"];
$language->regexpwordchar = $record["LgRegexpWordCharacters"];
$language->removespaces = (bool) $record["LgRemoveSpaces"];
$language->spliteachchar = (bool) $record["LgSplitEachChar"];
$language->rightoleft = (bool) $record["LgRightToLeft"];
$language->ttsvoiceapi = $record["LgTTSVoiceAPI"];
$language->showromanization = (bool) $record["LgShowRomanization"];
mysqli_free_result($res);
}
return $language;
}
function edit_languages_displayThirdPartyVoiceAPI(): void
{
?>
<h2>Third-Party Voice API</h2>
<p>
You can customize the voice API using an external service.
You have to use the following JSON format.
</p>
<pre
style="background-color: #f0f0f0; padding: 10px; border: 1px solid #ccc;"
><code lang="json"
>{
"input": ...,
"options": ...
}</code></pre>
<p>
LWT will insert text in <code>lwt_term</code> (required),
you can specify the language with <code>lwt_lang</code> (optional).<br />
If you need help, suggestions or want to see some demo, please go to
<a href="https://github.com/HugoFara/lwt/discussions/174">discussion 174</a>.
</p>
<?php
}
/**
* Create the form for a language.
*
* @param Language $language Language object
*/
function edit_language_form($language): void
{
$sourceLg = '';
$targetLg = '';
$currentnativelanguage = getSetting('currentnativelanguage');
if (array_key_exists($currentnativelanguage, LWT_LANGUAGES_ARRAY)) {
$targetLg = LWT_LANGUAGES_ARRAY[$currentnativelanguage][1];
}
if ($language->name) {
if (array_key_exists($language->name, LWT_LANGUAGES_ARRAY)) {
$sourceLg = LWT_LANGUAGES_ARRAY[$language->name][1];
}
$lgFromDict = langFromDict($language->translator);
if ($lgFromDict != '' && $lgFromDict != $sourceLg) {
$sourceLg = $lgFromDict;
}
$targetFromDict = targetLangFromDict($language->translator);
if ($targetFromDict != '' && $targetFromDict != $targetLg) {
$targetLg = $targetFromDict;
}
}
?>
<script type="text/javascript">
const edit_languages_js = {
reloadDictURLs: function(sourceLg='auto', targetLg='en') {
let base_url = window.location.href;
base_url = base_url.substring(0, base_url.lastIndexOf('/'));
GGTRANSLATE = 'https://translate.google.com/?' + $.param({
ie: "UTF-8",
sl: sourceLg,
tl: targetLg,
text: 'lwt_term'
});
LIBRETRANSLATE = 'http://localhost:5000/?' + $.param({
lwt_translator: 'libretranslate',
source: sourceLg,
target: targetLg,
q: "lwt_term"
});
GGL = base_url + '/ggl.php/?' + $.param({
sl: sourceLg, tl: targetLg, text: 'lwt_term'
});
},
/**
* Check for specific language option based on language name
*/
checkLanguageChanged: function(value) {
if (value == "Japanese") {
$(document.forms.lg_form.LgRegexpAlt).css("display", "block");
} else {
$(document.forms.lg_form.LgRegexpAlt).css("display", "none");
}
},
/**
* Handles any change on multi-words translate mode.
*/
multiWordsTranslateChange: function(value) {
let result;
let uses_key = false;
let base_url = window.location.href;
base_url = base_url.replace('/edit_languages.php', '/');
switch (value) {
case "google_translate":
result = GGTRANSLATE;
break;
case "libretranslate":
result = LIBRETRANSLATE;
uses_key = true;
break;
case "ggl":
result = GGL;
break;
case "glosbe":
result = base_url + "glosbe.php";
break;
}
if (result) {
document.forms.lg_form.LgGoogleTranslateURI.value = result;
}
$('#LgTranslatorKeyWrapper')
.css("display", uses_key ? "inherit" : "none");
},
displayLibreTranslateError: function(error) {
$('#translator_status')
.html('<a href="https://libretranslate.com/">LibreTranslate</a> server seems to be unreachable.' +
'You can install it on your server with the <a href="">LibreTranslate installation guide</a>.' +
'Error: ' + error);
},
/**
* Check status of the requested translation API.
*/
checkTranslatorStatus(url) {
if (url.startsWith('*')) {
url = url.substring(1);
}
const url_obj = new URL(url);
const params = url_obj.searchParams;
if (params.get('lwt_translator') == 'libretranslate') {
try {
this.checkLibreTranslateStatus(url_obj, { key: params.key });
} catch (error) {
this.displayLibreTranslateError(error);
}
}
},
/**
* Check LibreTranslate translator status.
*/
checkLibreTranslateStatus(url, key = "") {
const trans_url = new URL(url);
trans_url.searchParams.append('lwt_key', key);
getLibreTranslateTranslation(trans_url, 'ping', 'en', 'es')
.then(
function (translation) {
if (typeof translation === "string") {
$('#translator_status')
.html('<a href="https://libretranslate.com/">LibreTranslate</a> online!')
.attr('class', 'msgblue');
}
},
this.displayLibreTranslateError
);
},
/**
* Change the size of demo text.
*/
changeLanguageTextSize(value) {
$('#LgTextSizeExample').css("font-size", value + "%");
},
/**
* Handle changes to the words split method.
*/
wordCharChange(value) {
const regex = LANGDEFS[<?php echo json_encode($language->name); ?>][3];
const mecab = "mecab";
let result;
switch (value) {
case "regexp":
result = regex;
break;
case "mecab":
result = mecab;
break;
}
if (result) {
document.forms.lg_form.LgRegexpWordCharacters.value = result;
}
},
/**
* Build a dictionary/translator URL with the pop-up option
*/
addPopUpOption: function(url, checked) {
if (url.startsWith('*')) {
url = url.substring(1);
}
const built_url = new URL(url);
// Remove trivial cases
if (checked && built_url.searchParams.has('lwt_popup'))
return built_url.href;
if (!checked && !built_url.searchParams.has('lwt_popup'))
return built_url.href;
// Now we should change status
if (checked) {
built_url.searchParams.append('lwt_popup', 'true');
return built_url.href;
}
built_url.searchParams.delete('lwt_popup');
return built_url.href;
},
/**
* Change the Pop-Up URL of dictionary.
*/
changePopUpState: function (elem) {
const l_form = document.forms.lg_form;
let target;
switch (elem.name) {
case "LgDict1PopUp":
target = l_form.LgDict1URI;
break;
case "LgDict2PopUp":
target = l_form.LgDict2URI;
break;
case "LgGoogleTranslatePopUp":
target = l_form.LgGoogleTranslateURI;
break;
}
target.value = addPopUpOption(target.value, elem.checked);
},
/**
* Change Pop-Up checkboxes based on input box value.
*/
checkDictionaryChanged: function(input_box) {
const l_form = document.forms.lg_form;
if (input_box.value == '')
return;
switch (input_box.name) {
case "LgDict1URI":
target = l_form.LgDict1PopUp;
break;
case "LgDict2URI":
target = l_form.LgDict2PopUp;
break;
case "LgGoogleTranslateURI":
target = l_form.LgGoogleTranslatePopUp;
break;
}
let popup = false;
if (input_box.value.startsWith('*')) {
input_box.value = input_box.value.substring(1);
popup = true;
}
popup = popup || (new URL(input_box.value)).searchParams.has("lwt_popup");
target.checked = popup;
},
/**
* Modify the value of the translator select box if not coherent with the URL.
*/
checkTranslatorType: function (url, type_select) {
const parsed_url = new URL(url);
let final_value;
switch (parsed_url.searchParams.get("lwt_translator")) {
case "libretranslate":
// Using LibreTranslate
final_value = "libretranslate";
break;
default:
// Defaulting to Google
final_value = "google_translate";
break;
}
type_select.value = final_value;
},
/**
* Check the word splitting method.
*/
checkWordChar: function (method) {
const method_option = (method == "mecab") ? "mecab" : "regexp";
document.forms.lg_form.LgRegexpAlt.value = method_option;
},
checkVoiceAPI: function (api_value) {
message_field = $('#voice-api-message-zone');
if (api_value == "") {
message_field.hide();
return;
}
// Check if we have "lwt_term"
if (!api_value.includes("lwt_term")) {
message_field.text('"lwt_term" is missing!')
message_field.show();
return false
}
// Check if query can be parsed as JSON
let query;
try {
query = JSON.parse(api_value);
} catch (error) {
message_field.text("Cannot parse as JSON! " + error)
message_field.show();
return false;
}
// Check if we find "lwt_term" in JSON
if (deepFindValue(query, "lwt_term") === null) {
message_field.text("Cannot find 'lwt_term' in JSON!")
message_field.show();
return false;
}
message_field.hide();
return true;
},
testVoiceAPI: function () {
const api_value = document.forms.lg_form.LgTTSVoiceAPI.value;
const term = document.forms.lg_form.LgVoiceAPIDemo.value;
const lang = <?php echo json_encode($language->name); ?>;
readTextWithExternal(term, api_value, lang);
},
fullFormCheck: function () {
checkLanguageForm(document.forms.lg_form);
}
}
function reloadDictURLs(sourceLg='auto', targetLg='en') {
return edit_languages_js.reloadDictURLs(sourceLg, targetLg)
}
edit_languages_js.reloadDictURLs(
<?php echo json_encode($sourceLg); ?>,
<?php echo json_encode($targetLg); ?>
);
/**
* Check for specific language option based on language name.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkLanguageChanged.
*/
function checkLanguageChanged(value) {
return edit_languages_js.checkLanguageChanged(value);
}
/**
* Handles any change on multi-words translate mode.
*
* @deprecated Since 2.10.0, use edit_languages_js.multiWordsTranslateChange.
*/
function multiWordsTranslateChange(value) {
return edit_languages_js.multiWordsTranslateChange(value);
}
/**
* Check status of the requested translation API.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkTranslatorStatus
*/
function checkTranslatorStatus(url) {
return edit_languages_js.checkTranslatorStatus(url);
}
/**
* Check LibreTranslate translator status.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkLibreTranslateStatus
*/
function checkLibreTranslateStatus(url, key="") {
return edit_languages_js.checkLibreTranslateStatus(url, key);
}
/**
* Change the size of demo text.
*
* @deprecated Since 2.10.0, use edit_languages_js.changeLanguageTextSize
*/
function changeLanguageTextSize(value) {
return edit_languages_js.changeLanguageTextSize(value);
}
/**
* Handle changes to the words split method.
*
* @deprecated Since 2.10.0, use edit_languages_js.wordCharChange
*/
function wordCharChange(value) {
return edit_languages_js.wordCharChange(value);
}
/**
* Build a dictionary/translator URL with the pop-up option
*
* @deprecated Since 2.10.0, use edit_languages_js.addPopUpOption
*/
function addPopUpOption(url, checked) {
return edit_languages_js.addPopUpOption(url, checked);
}
/**
* Change the Pop-Up URL of dictionary.
*
* @deprecated Since 2.10.0, use edit_languages_js.changePopUpState
*/
function changePopUpState(elem) {
return edit_languages_js.changePopUpState(elem);
}
/**
* Change Pop-Up checkboxes based on input box value.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkDictionaryChanged
*/
function checkDictionaryChanged(input_box) {
return edit_languages_js.checkDictionaryChanged(input_box);
}
/**
* Modify the value of the translator select box if not coherent with the URL.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkTranslatorType
*/
function checkTranslatorType(url, type_select) {
return edit_languages_js.checkTranslatorType(url, type_select);
}
/**
* Check if all fields are coherent with translator URL.
*/
function checkTranslatorChanged(translator_input) {
edit_languages_js.checkTranslatorStatus(translator_input.value);
edit_languages_js.checkDictionaryChanged(translator_input);
edit_languages_js.checkTranslatorType(
translator_input.value, document.forms.lg_form.LgTranslatorName
);
}
/**
* Check the word splitting method.
*
* @deprecated Since 2.10.0, use edit_languages_js.checkWordChar
*/
function checkWordChar(method) {
return edit_languages_js.checkWordChar(method);
}
function checkVoiceAPI(api_value) {
return edit_languages_js.checkVoiceAPI(api_value);
}
/**
* Check if the help field are coherent with the input fields.
*
* param {element} l_form Language form.
*/
function checkLanguageForm(l_form) {
edit_languages_js.checkLanguageChanged(l_form.LgName.value);
edit_languages_js.checkDictionaryChanged(l_form.LgDict1URI);
edit_languages_js.checkDictionaryChanged(l_form.LgDict2URI);
checkTranslatorChanged(l_form.LgGoogleTranslateURI);
edit_languages_js.checkWordChar(l_form.LgRegexpWordCharacters.value);
}
$(edit_languages_js.fullFormCheck);
</script>
<form class="validate" action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post" onsubmit="return check_dupl_lang(<?php echo $language->id; ?>);"
name="lg_form">
<input type="hidden" name="LgID" value="<?php echo $language->id; ?>" />
<table class="tab1" cellspacing="0" cellpadding="5">
<tr>
<td class="td1 right">Study Language "L2":</td>
<td class="td1">
<input type="text" class="notempty setfocus checkoutsidebmp respinput"
data_info="Study Language" name="LgName" id="LgName"
value="<?php echo tohtml($language->name); ?>" maxlength="40"
oninput="checkLanguageChanged(this.value);" />
<img src="icn/status-busy.png" title="Field must not be empty"
alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">Dictionary 1 URI:</td>
<td class="td1">
<input type="url" class="notempty checkdicturl checkoutsidebmp respinput"
name="LgDict1URI"
value="<?php echo tohtml($language->dict1uri); ?>"
maxlength="200" data_info="Dictionary 1 URI"
oninput="checkDictionaryChanged(this);" />
<br />
<input type="checkbox" name="LgDict1PopUp" id="LgDict1PopUp"
onchange="changePopUpState(this);" />
<label for="LgDict1PopUp"
title="Open in a new window. Some dictionaries cannot be displayed in iframes">
Open in Pop-Up
</label>
<img src="icn/status-busy.png" title="Field must not be empty"
alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">Dictionary 2 URI:</td>
<td class="td1">
<input type="url" class="checkdicturl checkoutsidebmp respinput"
name="LgDict2URI"
value="<?php echo tohtml($language->dict2uri); ?>" maxlength="200"
data_info="Dictionary 2 URI"
oninput="checkDictionaryChanged(this);" />
<br />
<input type="checkbox" name="LgDict2PopUp" id="LgDict2PopUp"
onchange="changePopUpState(this);" />
<label for="LgDict2PopUp"
title="Open in a new window. Some dictionaries cannot be displayed in iframes">
Open in Pop-Up
</label>
</td>
</tr>
<tr>
<td class="td1 right">Sentence Translator URI:</td>
<td class="td1">
<select onchange="multiWordsTranslateChange(this.value);"
name="LgTranslatorName">
<option value="google_translate">Google Translate (webpage)</option>
<option value="libretranslate">LibreTranslate API</option>
<option value="ggl">
GoogleTranslate API
</option>
<!-- Glosbe has stopped the API -->
<option value="glosbe" style="display: none;">
Glosbe API
</option>
</select>
<input type="url" class="checkdicturl checkoutsidebmp respinput"
name="LgGoogleTranslateURI"
value="<?php echo tohtml($language->translator); ?>"
maxlength="200" data_info="GoogleTranslate URI"
oninput="checkTranslatorChanged(this);" class="respinput"
/>
<div id="LgTranslatorKeyWrapper" style="display: none;">
<label for="LgTranslatorKey">Key :</label>
<input type="text" id="LgTranslatorKey" name="LgTranslatorKey"/>
</div>
<br />
<input type="checkbox" name="LgGoogleTranslatePopUp"
id="LgGoogleTranslatePopUp"
onchange="edit_languages_js.changePopUpState(this);"/>
<label for="LgGoogleTranslatePopUp"
title="Open in a new window. Some translators cannot be displayed in iframes">
Open in Pop-Up
</label>
<div id="translator_error" class="red" ></div>
</td>
</tr>
<tr>
<td class="td1 right">Text Size (%):</td>
<td class="td1">
<input name="LgTextSize" defaultValue="100" type="number" min="100" max="250"
value="<?php echo $language->textsize; ?>" step="50"
onchange="edit_languages_js.changeLanguageTextSize(this.value);"
class="respinput" />
<input type="text" class="respinput"
style="font-size: <?php echo $language->textsize ?>%;"
id="LgTextSizeExample"
value="Text will be this size" />
</td>
</tr>
<tr>
<td class="td1 right">Character Substitutions:</td>
<td class="td1">
<input type="text" class="checkoutsidebmp respinput"
data_info="Character Substitutions" name="LgCharacterSubstitutions"
value="<?php echo tohtml($language->charactersubst); ?>"
maxlength="500" />
</td>
</tr>
<tr>
<td class="td1 right">RegExp Split Sentences:</td>
<td class="td1">
<input type="text" class="notempty checkoutsidebmp respinput"
name="LgRegexpSplitSentences"
value="<?php echo tohtml($language->regexpsplitsent); ?>"
maxlength="500"
data_info="RegExp Split Sentences" />
<img src="icn/status-busy.png" title="Field must not be empty"
alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">Exceptions Split Sentences:</td>
<td class="td1">
<input type="text" class="checkoutsidebmp respinput"
data_info="Exceptions Split Sentences"
name="LgExceptionsSplitSentences"
value="<?php echo tohtml($language->exceptionsplitsent); ?>"
maxlength="500" />
</td>
</tr>
<tr>
<td class="td1 right">RegExp Word Characters:</td>
<td class="td1">
<select onchange="wordCharChange(this.value);" style="display: none;"
name="LgRegexpAlt">
<option value="regexp">Regular Expressions (demo)</option>
<option value="mecab">MeCab (recommended)</option>
</select>
<input type="text" class="notempty checkoutsidebmp respinput"
data_info="RegExp Word Characters" name="LgRegexpWordCharacters"
value="<?php echo tohtml($language->regexpwordchar); ?>"
maxlength="500" />
<img src="icn/status-busy.png" title="Field must not be empty"
alt="Field must not be empty" />
<div style="display: none;" class="red" id="mecab_not_installed">
<a href="https://en.wikipedia.org/wiki/MeCab">MeCab</a> does
not seem to be installed on your server.
Please read the <a href="">MeCab installation guide</a>.