Skip to content

Commit 6ca6e7b

Browse files
committed
Auto-translation, reviewing edit_languages.php.
Auto-translation applies to single words with LibreTranslate API. Many changes to the edit_languages interface. Laying fundation for a choice Japanese with/without MeCab. Laying fundation for language text size input change.
1 parent 27cd2a6 commit 6ca6e7b

5 files changed

Lines changed: 341 additions & 114 deletions

File tree

‎edit_languages.php‎

Lines changed: 201 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -458,106 +458,218 @@ function edit_languages_change($lid)
458458
?>
459459

460460
<script type="text/javascript" charset="utf-8">
461+
462+
/**
463+
* Handles any change on multi-words translate mode.
464+
*/
465+
function multiWordsTranslateChange(value) {
466+
const ggtranslate = <?php echo json_encode($record['LgGoogleTranslateURI']); ?>;
467+
const libretranslate = "libretranslate http://localhost:5000/translate";
468+
469+
let result;
470+
switch (value) {
471+
case "google_translate":
472+
result = ggtranslate;
473+
break;
474+
case "libretranslate":
475+
result = libretranslate;
476+
break;
477+
}
478+
if (result) {
479+
document.forms.lg_form.LgGoogleTranslateURI.value = result;
480+
}
481+
}
482+
483+
/**
484+
* Check status of the requested translation API.
485+
*/
486+
function checkTranslatorStatus(url) {
487+
if (url.startsWith("libretranslate ")) {
488+
try {
489+
const url_parts = url.split(' ');
490+
checkLibreTranslateStatus(url_parts[1], key=url_parts[2]);
491+
} catch (error) {
492+
$('#translator_status')
493+
.html('<a href="https://libretranslate.com/">LibreTranslate</a> server seems to be unreachable.' +
494+
'You can install it on your server with the <a href="">LibreTranslate installation guide</a>.' +
495+
'Error: ' + error);
496+
}
497+
}
498+
}
499+
500+
/**
501+
* Check LibreTranslate translator status.
502+
*/
503+
function checkLibreTranslateStatus(url, key="") {
504+
getLibreTranslateTranslation('ping', 'en', 'es', key, url=url)
505+
.then(
506+
function (translation) {
507+
if (typeof translation === "string") {
508+
$('#translator_status')
509+
.html('<a href="https://libretranslate.com/">LibreTranslate</a> online!')
510+
.attr('class', 'msgblue');
511+
}
512+
},
513+
function (error) {
514+
$('#translator_status')
515+
.html('<a href="https://libretranslate.com/">LibreTranslate</a> server seems to be unreachable.' +
516+
'You can install it on your server with the <a href="">LibreTranslate installation guide</a>.' +
517+
'Error: ' + error);
518+
}
519+
);
520+
}
521+
522+
/**
523+
* Handle changes to the words split method.
524+
*/
525+
function wordsSplitChange(value) {
526+
const regex = <?php echo json_encode($record['LgRegexpWordCharacters']); ?>;
527+
const mecab = "mecab";
528+
529+
let result, fixed = false;
530+
switch (value) {
531+
case "regexp":
532+
result = regex;
533+
break;
534+
case "mecab":
535+
result = mecab;
536+
fixed = true;
537+
break;
538+
}
539+
if (result) {
540+
document.forms.lg_form.LgRegexpWordCharacters.value = result;
541+
document.forms.lg_form.LgRegexpWordCharacters.disabled = fixed;
542+
}
543+
544+
}
545+
461546
$(document).ready(ask_before_exiting);
462547
</script>
463548
<h2>Edit Language
464549
<a href="docs/info.html#howtolang">
465550
<img src="icn/question-frame.png" title="Help" alt="Help" />
466551
</a>
467552
</h2>
468-
<form class="validate" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return check_dupl_lang(<?php echo $lid; ?>);">
469-
<input type="hidden" name="LgID" value="<?php echo $lid; ?>" />
470-
<table class="tab2" cellspacing="0" cellpadding="5">
471-
<tr>
472-
<td class="td1 right">Study Language "L2":</td>
473-
<td class="td1">
474-
<input type="text" class="notempty setfocus checkoutsidebmp" data_info="Study Language" name="LgName" id="LgName" value="<?php echo tohtml($record['LgName']); ?>" maxlength="40" size="40" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" /></td>
475-
</tr>
476-
<tr>
477-
<td class="td1 right">Dictionary 1 URI:</td>
478-
<td class="td1">
479-
<input type="text" class="notempty checkdicturl checkoutsidebmp" name="LgDict1URI" value="<?php echo tohtml($record['LgDict1URI']); ?>" maxlength="200" size="60" data_info="Dictionary 1 URI" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" /></td>
480-
</tr>
481-
<tr>
482-
<td class="td1 right">Dictionary 2 URI:</td>
483-
<td class="td1">
484-
<input type="text" class="checkdicturl checkoutsidebmp" name="LgDict2URI" value="<?php echo tohtml($record['LgDict2URI']); ?>" maxlength="200" size="60" data_info="Dictionary 2 URI" /></td>
485-
</tr>
486-
<tr>
487-
<td class="td1 right">GoogleTranslate URI:</td>
488-
<td class="td1">
489-
<input type="text" class="checkdicturl checkoutsidebmp" name="LgGoogleTranslateURI" value="<?php echo tohtml($record['LgGoogleTranslateURI']); ?>" maxlength="200" size="60" data_info="GoogleTranslate URI" /></td>
490-
</tr>
491-
<tr>
492-
<td class="td1 right">Text Size:</td>
493-
<td class="td1">
494-
<select name="LgTextSize"><?php echo get_languagessize_selectoptions($record['LgTextSize']); ?></select></td>
495-
</tr>
496-
<tr>
497-
<td class="td1 right">Character Substitutions:</td>
498-
<td class="td1">
499-
<input type="text" class="checkoutsidebmp" data_info="Character Substitutions" name="LgCharacterSubstitutions" value="<?php echo tohtml($record['LgCharacterSubstitutions']); ?>" maxlength="500" size="60" /></td>
500-
</tr>
501-
<tr>
502-
<td class="td1 right">RegExp Split Sentences:</td>
503-
<td class="td1">
504-
<input type="text" class="notempty checkoutsidebmp" name="LgRegexpSplitSentences" value="<?php echo tohtml($record['LgRegexpSplitSentences']); ?>" maxlength="500" size="60" data_info="RegExp Split Sentences" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" /></td>
505-
</tr>
506-
<tr>
507-
<td class="td1 right">Exceptions Split Sentences:</td>
508-
<td class="td1">
509-
<input type="text" class="checkoutsidebmp" data_info="Exceptions Split Sentences" name="LgExceptionsSplitSentences" value="<?php echo tohtml($record['LgExceptionsSplitSentences']); ?>" maxlength="500" size="60" /></td>
510-
</tr>
511-
<tr>
512-
<td class="td1 right">RegExp Word Characters:</td>
513-
<td class="td1">
514-
<input type="text" class="notempty checkoutsidebmp" data_info="RegExp Word Characters" name="LgRegexpWordCharacters" value="<?php echo tohtml($record['LgRegexpWordCharacters']); ?>" maxlength="500" size="60" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" /></td>
515-
</tr>
516-
<tr>
517-
<td class="td1 right">Make each character a word:</td>
518-
<td class="td1">
519-
<select name="LgSplitEachChar">
520-
<?php echo get_yesno_selectoptions($record['LgSplitEachChar']); ?>
521-
</select>
522-
(e.g. for Chinese, Japanese, etc.)</td>
523-
</tr>
524-
<tr>
525-
<td class="td1 right">Remove spaces:</td>
526-
<td class="td1">
527-
<select name="LgRemoveSpaces">
528-
<?php echo get_yesno_selectoptions($record['LgRemoveSpaces']); ?>
553+
<form class="validate" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return check_dupl_lang(<?php echo $lid; ?>);" name="lg_form">
554+
<input type="hidden" name="LgID" value="<?php echo $lid; ?>" />
555+
<table class="tab2" cellspacing="0" cellpadding="5">
556+
<tr>
557+
<td class="td1 right">Study Language "L2":</td>
558+
<td class="td1">
559+
<input type="text" class="notempty setfocus checkoutsidebmp" data_info="Study Language" name="LgName" id="LgName" value="<?php echo tohtml($record['LgName']); ?>" maxlength="40" size="40" />
560+
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
561+
</td>
562+
</tr>
563+
<tr>
564+
<td class="td1 right">Dictionary 1 URI:</td>
565+
<td class="td1">
566+
<input type="text" class="notempty checkdicturl checkoutsidebmp" name="LgDict1URI" value="<?php echo tohtml($record['LgDict1URI']); ?>" maxlength="200" size="60" data_info="Dictionary 1 URI" />
567+
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
568+
</td>
569+
</tr>
570+
<tr>
571+
<td class="td1 right">Dictionary 2 URI:</td>
572+
<td class="td1">
573+
<input type="text" class="checkdicturl checkoutsidebmp" name="LgDict2URI" value="<?php echo tohtml($record['LgDict2URI']); ?>" maxlength="200" size="60" data_info="Dictionary 2 URI" />
574+
</td>
575+
</tr>
576+
<tr>
577+
<td class="td1 right">GoogleTranslate URI:</td>
578+
<td class="td1">
579+
<select onchange="multiWordsTranslateChange(this.value);">
580+
<option value="google_translate">Google Translate</option>
581+
<option value="libretranslate">LibreTranslate</option>
529582
</select>
530-
(e.g. for Chinese, Japanese, etc.)</td>
531-
</tr>
532-
<tr>
533-
<td class="td1 right">Right-To-Left Script:</td>
534-
<td class="td1">
535-
<select name="LgRightToLeft">
536-
<?php echo get_yesno_selectoptions($record['LgRightToLeft']); ?>
583+
<input type="text" class="checkdicturl checkoutsidebmp" name="LgGoogleTranslateURI" value="<?php echo tohtml($record['LgGoogleTranslateURI']); ?>" maxlength="200" size="60" data_info="GoogleTranslate URI" oninput="checkTranslatorStatus(this.value);"/>
584+
<div id="translator_error" class="red" ></div>
585+
</td>
586+
</tr>
587+
<tr>
588+
<td class="td1 right">Text Size:</td>
589+
<td class="td1">
590+
<select name="LgTextSize">
591+
<?php echo get_languagessize_selectoptions($record['LgTextSize']); ?>
537592
</select>
538-
(e.g. for Arabic, Hebrew, Farsi, Urdu, etc.)</td>
539-
</tr>
540-
<tr>
541-
<td class="td1 right">
542-
Export Template
543-
<img class="click" src="icn/question-frame.png" title="Help" alt="Help" onclick="oewin('export_template.html');" /> :
593+
<input style="display: none;" type="number" min="100" max="250" value="100" step="50" />%
594+
</td>
595+
</tr>
596+
<tr>
597+
<td class="td1 right">Character Substitutions:</td>
598+
<td class="td1">
599+
<input type="text" class="checkoutsidebmp" data_info="Character Substitutions" name="LgCharacterSubstitutions" value="<?php echo tohtml($record['LgCharacterSubstitutions']); ?>" maxlength="500" size="60" />
544600
</td>
545-
<td class="td1">
546-
<input type="text" class="checkoutsidebmp" data_info="Export Template" name="LgExportTemplate" value="<?php echo tohtml($record['LgExportTemplate']); ?>" maxlength="1000" size="60" />
601+
</tr>
602+
<tr>
603+
<td class="td1 right">RegExp Split Sentences:</td>
604+
<td class="td1">
605+
<input type="text" class="notempty checkoutsidebmp" name="LgRegexpSplitSentences" value="<?php echo tohtml($record['LgRegexpSplitSentences']); ?>" maxlength="500" size="60" data_info="RegExp Split Sentences" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
547606
</td>
548-
</tr>
549-
<tr>
550-
<td class="td1 right" colspan="2">
551-
<input type="button" value="Cancel" onclick="{resetDirty(); location.href='edit_languages.php';}" />
552-
<input type="submit" name="op" value="Change" /></td>
553-
</tr>
554-
</table>
555-
<p class="smallgray">
556-
<b>Warning:</b> Changing certain language settings
557-
(e.g. RegExp Word Characters, etc.)<br />may cause partial or complete loss
558-
of improved annotated texts!
559-
</p>
560-
</form>
607+
</tr>
608+
<tr>
609+
<td class="td1 right">Exceptions Split Sentences:</td>
610+
<td class="td1">
611+
<input type="text" class="checkoutsidebmp" data_info="Exceptions Split Sentences" name="LgExceptionsSplitSentences" value="<?php echo tohtml($record['LgExceptionsSplitSentences']); ?>" maxlength="500" size="60" />
612+
</td>
613+
</tr>
614+
<tr>
615+
<td class="td1 right">RegExp Word Characters:</td>
616+
<td class="td1">
617+
<select onchange="wordsSplitChange(this.value);" style="display: none;" name="LgRegexpAlt">
618+
<option value="regexp">Regular Expressions (demo)</option>
619+
<option value="mecab">MeCab (recommended)</option>
620+
</select>
621+
<input type="text" class="notempty checkoutsidebmp" data_info="RegExp Word Characters" name="LgRegexpWordCharacters" value="<?php echo tohtml($record['LgRegexpWordCharacters']); ?>" maxlength="500" size="60" /> <img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
622+
<div style="display: none;" class="red" id="mecab_not_installed">
623+
<a href="https://en.wikipedia.org/wiki/MeCab">MeCab</a> does not seem to be installed on your server.
624+
Please read the <a href="">MeCab installation guide</a>.
625+
</div>
626+
</td>
627+
</tr>
628+
<tr>
629+
<td class="td1 right">Make each character a word:</td>
630+
<td class="td1">
631+
<select name="LgSplitEachChar">
632+
<?php echo get_yesno_selectoptions($record['LgSplitEachChar']); ?>
633+
</select>
634+
(e.g. for Chinese, Japanese, etc.)</td>
635+
</tr>
636+
<tr>
637+
<td class="td1 right">Remove spaces:</td>
638+
<td class="td1">
639+
<select name="LgRemoveSpaces">
640+
<?php echo get_yesno_selectoptions($record['LgRemoveSpaces']); ?>
641+
</select>
642+
(e.g. for Chinese, Japanese, etc.)</td>
643+
</tr>
644+
<tr>
645+
<td class="td1 right">Right-To-Left Script:</td>
646+
<td class="td1">
647+
<select name="LgRightToLeft">
648+
<?php echo get_yesno_selectoptions($record['LgRightToLeft']); ?>
649+
</select>
650+
(e.g. for Arabic, Hebrew, Farsi, Urdu, etc.)</td>
651+
</tr>
652+
<tr>
653+
<td class="td1 right">
654+
Export Template
655+
<img class="click" src="icn/question-frame.png" title="Help" alt="Help" onclick="oewin('export_template.html');" /> :
656+
</td>
657+
<td class="td1">
658+
<input type="text" class="checkoutsidebmp" data_info="Export Template" name="LgExportTemplate" value="<?php echo tohtml($record['LgExportTemplate']); ?>" maxlength="1000" size="60" />
659+
</td>
660+
</tr>
661+
<tr>
662+
<td class="td1 right" colspan="2">
663+
<input type="button" value="Cancel" onclick="{resetDirty(); location.href='edit_languages.php';}" />
664+
<input type="submit" name="op" value="Change" /></td>
665+
</tr>
666+
</table>
667+
<p class="smallgray">
668+
<b>Warning:</b> Changing certain language settings
669+
(e.g. RegExp Word Characters, etc.)<br />may cause partial or complete loss
670+
of improved annotated texts!
671+
</p>
672+
</form>
561673
<?php
562674

563675
}

0 commit comments

Comments
 (0)