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
|
####################################################################
# Translate.awk #
####################################################################
function provides(engineName) {
Translator[tolower(engineName)] = TRUE
}
function engineMethod(methodName, engine, translator) {
if (!Translator[Option["engine"]]) {
# case-insensitive match engine name
engine = tolower(Option["engine"])
if (!Translator[engine]) # fuzzy match engine name
for (translator in Translator)
if (Translator[translator] && # there IS such a translator
translator ~ "^"engine) {
engine = translator
break
}
if (!Translator[engine]) {
e("[ERROR] Translator not found: " Option["engine"] "\n" \
" Run '-list-engines / -S' to see a list of available engines.")
exit 1
}
Option["engine"] = engine
}
return Option["engine"] methodName
}
# Detect external audio player (mplayer, mpv, mpg123).
function initAudioPlayer() {
AudioPlayer = !system("mplayer" SUPOUT SUPERR) ?
"mplayer" :
(!system("mpv" SUPOUT SUPERR) ?
"mpv" :
(!system("mpg123 --version" SUPOUT SUPERR) ?
"mpg123" :
""))
}
# Detect external speech synthesizer (say, espeak).
function initSpeechSynthesizer() {
SpeechSynthesizer = !system("say ''" SUPOUT SUPERR) ?
"say" :
(!system("espeak ''" SUPOUT SUPERR) ?
"espeak" :
"")
}
# Detect external terminal pager (less, more, most).
function initPager() {
Pager = !system("less -V" SUPOUT SUPERR) ?
"less" :
(!system("more -V" SUPOUT SUPERR) ?
"more" :
(!system("most" SUPOUT SUPERR) ?
"most" :
""))
}
# Initialize `HttpService`.
function initHttpService() {
_Init()
if (Option["proxy"]) {
match(Option["proxy"], /^(http:\/*)?([^\/]*):([^\/:]*)/, HttpProxySpec)
HttpService = "/inet/tcp/0/" HttpProxySpec[2] "/" HttpProxySpec[3]
HttpPathPrefix = HttpProtocol HttpHost
} else {
HttpService = "/inet/tcp/0/" HttpHost "/" HttpPort
HttpPathPrefix = ""
}
}
# Pre-process string (URL-encode before send).
function preprocess(text) {
return quote(text)
}
# Post-process string (remove any redundant whitespace).
function postprocess(text) {
text = gensub(/ ([.,;:?!"])/, "\\1", "g", text)
text = gensub(/(["]) /, "\\1", "g", text)
return text
}
# Send an HTTP GET request and get response from an online translator.
function getResponse(text, sl, tl, hl, content, header, isBody, url) {
url = _RequestUrl(text, sl, tl, hl)
header = "GET " url " HTTP/1.1\n" \
"Host: " HttpHost "\n" \
"Connection: close\n"
if (Option["user-agent"])
header = header "User-Agent: " Option["user-agent"] "\n"
if (Cookie)
header = header "Cookie: " Cookie "\n"
content = NULLSTR; isBody = 0
print header |& HttpService
while ((HttpService |& getline) > 0) {
if (isBody)
content = content ? content "\n" $0 : $0
else if (length($0) <= 1)
isBody = 1
l(sprintf("%4s bytes > %s", length($0), $0))
}
close(HttpService)
return assert(content, "[ERROR] Null response.")
}
# Print a string (to output file or terminal pager).
function p(string) {
if (Option["view"])
print string | Option["pager"]
else
print string > Option["output"]
}
# Play using a Text-to-Speech engine.
function play(text, tl, url) {
url = _TTSUrl(text, tl)
# Don't use getline from pipe here - the same pipe will be run only once for each AWK script!
system(Option["player"] " " parameterize(url) SUPOUT SUPERR)
}
# Get the translation of a string.
function getTranslation(text, sl, tl, hl,
isVerbose, toSpeech, returnPlaylist, returnIl) {
return _Translate(text, sl, tl, hl,
isVerbose, toSpeech, returnPlaylist, returnIl)
}
# Translate a file.
function fileTranslation(uri, group, temp1, temp2) {
temp1 = Option["input"]
temp2 = Option["verbose"]
match(uri, /^file:\/\/(.*)/, group)
Option["input"] = group[1]
Option["verbose"] = 0
translateMain()
Option["input"] = temp1
Option["verbose"] = temp2
}
# Start a browser session and translate a web page.
function webTranslation(uri, sl, tl, hl) {
system(Option["browser"] " " \
parameterize(_WebTranslateUrl(uri, sl, tl, hl)) "&")
}
# Translate the source text (into all target languages).
function translate(text, inline,
####
i, j, playlist, il, saveSortedIn) {
if (!getCode(Option["hl"])) {
# Check if home language is supported
w("[WARNING] Unknown language code: " Option["hl"] ", fallback to English: en")
Option["hl"] = "en" # fallback to English
} else if (isRTL(Option["hl"])) {
# Check if home language is R-to-L
if (!FriBidi)
w("[WARNING] " getName(Option["hl"]) " is a right-to-left language, but FriBidi is not found.")
}
if (!getCode(Option["sl"])) {
# Check if source language is supported
w("[WARNING] Unknown source language code: " Option["sl"])
} else if (isRTL(Option["sl"])) {
# Check if source language is R-to-L
if (!FriBidi)
w("[WARNING] " getName(Option["sl"]) " is a right-to-left language, but FriBidi is not found.")
}
saveSortedIn = PROCINFO["sorted_in"]
PROCINFO["sorted_in"] = "@ind_num_asc"
for (i in Option["tl"]) {
# Non-interactive verbose mode: separator between targets
if (!Option["interactive"])
if (Option["verbose"] && i > 1)
p(prettify("target-seperator", replicate(Option["chr-target-seperator"], Option["width"])))
if (inline &&
startsWithAny(text, UriSchemes) == "file://") {
# translate URL only from command-line parameters (inline)
fileTranslation(text)
} else if (inline &&
startsWithAny(text, UriSchemes) == "http://" ||
startsWithAny(text, UriSchemes) == "https://") {
# translate URL only from command-line parameters (inline)
webTranslation(text, Option["sl"], Option["tl"][i], Option["hl"])
} else {
p(getTranslation(text, Option["sl"], Option["tl"][i], Option["hl"], Option["verbose"], Option["play"], playlist, il))
if (Option["play"] == 1) {
if (Option["player"])
for (j in playlist)
play(playlist[j]["text"], playlist[j]["tl"])
else if (SpeechSynthesizer)
for (j in playlist)
print playlist[j]["text"] | SpeechSynthesizer
} else if (Option["play"] == 2) {
if (Option["player"])
play(text, il[0])
else if (SpeechSynthesizer)
print text | SpeechSynthesizer
}
}
}
PROCINFO["sorted_in"] = saveSortedIn
}
# Read from input and translate each line.
function translateMain( i, line) {
if (Option["interactive"])
prompt()
if (Option["input"] == STDIN || fileExists(Option["input"])) {
i = 0
while (getline line < Option["input"])
if (line) {
# Non-interactive verbose mode: separator between sources
if (!Option["interactive"])
if (Option["verbose"] && i++ > 0)
p(prettify("source-seperator",
replicate(Option["chr-source-seperator"],
Option["width"])))
if (Option["interactive"])
repl(line)
else
translate(line)
}
} else
e("[ERROR] File not found: " Option["input"])
}
|