forked from HugoFara/lwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggl.php
More file actions
173 lines (157 loc) · 4.82 KB
/
ggl.php
File metadata and controls
173 lines (157 loc) · 4.82 KB
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
<?php
/*
* \file
* \brief Google Translate interface
*
* Call: ggl.php?text=[text]&sl=[source language]&tl=[target language] ... translate text
* ... sent=[int] ... single sentence mode.
*
* PHP version 8.1
*
* @package Lwt
* @author LWT Project <lwt-project@hotmail.com>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/ggl.html
* @since 1.6.0
* @since 2.7.0 Refactored with functional paradigm
*/
namespace Lwt\Interface;
require_once 'inc/session_utility.php';
require_once 'inc/google_time_token.php' ;
require_once 'inc/classes/GoogleTranslate.php';
use Lwt\Classes\GoogleTranslate as GoogleTranslate;
use function Lwt\Includes\getGoogleTimeToken;
/*
* Translate a single sentence using Google Translate.
*
* @param string $text Text to translate
* @param string $translation Sentence translation
*
* @return void
*/
function translate_sentence($text, $translation): void
{
?>
<h2>Sentence Translation</h2>
<span title="Translated via Google Translate">
<?php echo tohtml($translation); ?>
</span>
<p>Original sentence: </p>
<blockquote><?php echo tohtml($text); ?></blockquote>
<?php
}
/*
* Translate input text using Google Translate.
*
* @param string $text Text to translate
* @param array $file Array of translated terms
* @param string $sl Source language (e. g. "es")
* @param string $tl Target language (e. g. "en")
*
* @return void
*/
function translate_term($text, $file, $sl, $tl): void
{
global $tbpref;
$lg_id = getSetting('currentlangage');
$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = $lg_id"
);
?>
<h2 title="Translate with Google Translate">
Word translation: <?php echo tohtml($text) ?>
<img id="textToSpeech" style="cursor: pointer;" title="Click to read!"
src="<?php print_file_path('icn/speaker-volume.png'); ?>" ></img>
<img id="del_translation" style="cursor: pointer;"
title="Empty Translation Field" onclick="deleteTranslation ();"
src="<?php print_file_path('icn/broom.png'); ?>" ></img>
</h2>
<script type="text/javascript">
$('#textToSpeech').on('click', function () {
const txt = <?php echo json_encode($text); ?>;
speechDispatcher(txt, <?php echo json_encode($lg_id); ?>);
});
$(document).ready(function() {
if (window.parent.frames['ro'] === undefined && window.opener === undefined)
$('#del_translation').remove();
});
</script>
<?php
foreach ($file as $word){
echo '<span class="click" onclick="addTranslation(' .
prepare_textdata_js($word) . ');">' .
'<img src="icn/tick-button.png" title="Copy" alt="Copy" /> ' .
tohtml($word) . '</span><br />';
}
?>
<p>
(Click on <img src="icn/tick-button.png" title="Choose" alt="Choose" />
to copy word(s) into above term)<br />
</p>
<hr />
<form action="ggl.php" method="get">
Unhappy?<br/>Change term:
<input type="text" name="text" maxlength="250" size="15"
value="<?php echo tohtml($text); ?>">
<input type="hidden" name="sl" value="<?php echo tohtml($sl); ?>">
<input type="hidden" name="tl" value="<?php echo tohtml($tl); ?>">
<input type="submit" value="Translate via Google Translate">
</form>
<?php
}
/*
* Translate input text using Google Translate.
*
* @param string $text Text to translate
* @param string $sl Source language (e. g. "es")
* @param string $tl Target language (e. g. "en")
* @param bool $sentence_mode Set to true for full sentence translation
*
* @return void
*/
function translate_text($text, $sl, $tl, $sentence_mode): void
{
$file = GoogleTranslate::staticTranslate(
$text, $sl, $tl, getGoogleTimeToken()
);
if ($file === false) {
my_die("Unable to get translation from Google!");
}
$gglink = makeOpenDictStr(
createTheDictLink(
"https://translate.google.com/?sl=$sl&tl=$tl&text=lwt_term&lwt_popup=true",
$text
),
"View on Google Translate"
);
if ($sentence_mode) {
translate_sentence($text, $file[0]);
} else {
translate_term($text, $file, $sl, $tl);
}
echo $gglink;
}
/*
* Translate input text using Google Translate.
*
* @param string $text Text to translate
*
* @return void
*/
function do_content($text): void
{
header('Pragma: no-cache');
header('Expires: 0');
pagestart_nobody('Google Translate');
if (trim($text) != '') {
translate_text($text, $_GET["sl"], $_GET["tl"], isset($_GET['sent']));
} else {
echo '<p class="msgblue">Term is not set!</p>';
}
pageend();
}
if (isset($_GET["text"])) {
do_content($_GET["text"]);
}
?>