Skip to content

Commit

Permalink
Translate: fixed Yandex engine (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
eisaev committed Jan 27, 2024
1 parent c5ba657 commit fd32771
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions include/Translators/YandexTranslate.awk
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,44 @@
# Last Updated: 11 Aug 2018
BEGIN { provides("yandex") }

function genSID( content, group, temp) {
content = curl("http://translate.yandex.com")

match(content, /SID:[[:space:]]*'([^']+)'/, group)
if (group[1]) {
split(group[1], temp, ".")
SID = reverse(temp[1]) "." reverse(temp[2]) "." reverse(temp[3])
} else {
e("[ERROR] Oops! Something went wrong and I can't translate it for you :(")
exit 1
}
function genUcid() {
"uuidgen" | getline uuid;
close("uuidgen");
gsub("-", "", uuid);
ucid = tolower(uuid)
}

function yandexInit() {
genSID() # generate a one-time key
genUcid() # generate a one-time key
YandexWebTranslate = "z5h64q92x9.net" # host for web translation

HttpProtocol = "http://"
HttpHost = "translate.yandex.net"
HttpPort = 80
}

function yandexRequestUrl(text, sl, tl, hl, group) {
function yandexPostRequestUrl(text, sl, tl, hl, group) {
# Quick hack: Yandex doesn't support digraphia code (yet)
split(sl, group, "-"); sl = group[1]
split(tl, group, "-"); tl = group[1]

return HttpPathPrefix "/api/v1/tr.json/translate?" \
"id=" SID "-0-0&srv=tr-text" \
"&text=" preprocess(text) "&lang=" (sl == "auto" ? tl : sl "-" tl)
return HttpPathPrefix "/api/v1/tr.json/translate" \
"?ucid=" ucid \
"&srv=android" \
"&text=" preprocess(text) \
"&lang=" (sl == "auto" ? tl : sl "-" tl)
}

function yandexPostRequestBody(text, sl, tl, hl, type) {
# type == "translit"
return "text=" quote(text) "&lang=" sl
return ""
}

function yandexPostRequestContentType(text, sl, tl, hl, type) {
return "application/x-www-form-urlencoded"
}

function yandexPostRequestUserAgent(text, sl, tl, hl, type) {
return ""
}

function yandexGetDictionaryResponse(text, sl, tl, hl, content, header, isBody, url) {
Expand Down Expand Up @@ -131,7 +134,7 @@ function yandexTranslate(text, sl, tl, hl,
_tl = getCode(tl); if (!_tl) _tl = tl
_hl = getCode(hl); if (!_hl) _hl = hl

content = getResponse(text, _sl, _tl, _hl)
content = postResponse(text, _sl, _tl, _hl)
if (Option["dump"])
return content
tokenize(tokens, content)
Expand All @@ -156,8 +159,7 @@ function yandexTranslate(text, sl, tl, hl,
# Transliteration
wShowTranslationPhonetics = Option["show-translation-phonetics"]
if (wShowTranslationPhonetics && _tl != "emj") {
split(_tl, group, "-")
data = yandexPostRequestBody(translation, group[1], group[1], _hl, "translit")
data = "text=" text "&lang=" tl
content = curlPost("https://translate.yandex.net/translit/translit", data)
phonetics = (content ~ /not supported$/) ? "" : unparameterize(content)
}
Expand Down Expand Up @@ -188,8 +190,7 @@ function yandexTranslate(text, sl, tl, hl,
# Transliteration (original)
wShowOriginalPhonetics = Option["show-original-phonetics"]
if (wShowTranslationPhonetics && il != "emj") {
split(il, group, "-")
data = yandexPostRequestBody(text, group[1], group[1], _hl, "translit")
data = "text=" text "&lang=" sl
content = curlPost("https://translate.yandex.net/translit/translit", data)
oPhonetics = (content ~ /not supported$/) ? "" : unparameterize(content)
}
Expand Down

0 comments on commit fd32771

Please sign in to comment.