2023-06-25 00:10:57 +05:30
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gocolly/colly"
|
2023-07-22 19:13:42 +05:30
|
|
|
"github.com/google/go-querystring/query"
|
2023-06-25 00:10:57 +05:30
|
|
|
"os"
|
|
|
|
)
|
2023-06-25 00:11:11 +05:30
|
|
|
|
2023-07-22 19:13:42 +05:30
|
|
|
func TranslateGoogle(to string, from string, text string) string {
|
2023-07-22 21:11:35 +05:30
|
|
|
// For some reason google uses no for norwegian instead of nb like the rest of the translators. This is for the All function primarily
|
|
|
|
if to == "nb" {
|
|
|
|
to = "no"
|
|
|
|
} else if from == "nb" {
|
|
|
|
to = "no"
|
|
|
|
}
|
|
|
|
var ToValid bool
|
|
|
|
var FromValid bool
|
|
|
|
for _, v := range LangListGoogle("sl") {
|
|
|
|
if v.Id == to {
|
|
|
|
ToValid = true
|
|
|
|
}
|
|
|
|
if v.Id == from {
|
|
|
|
FromValid = true
|
|
|
|
}
|
|
|
|
if FromValid == true && ToValid == true {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ToValid != true {
|
|
|
|
return "Target Language Code invalid"
|
|
|
|
}
|
|
|
|
if FromValid != true {
|
|
|
|
return "Source Language Code invalid"
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:10:57 +05:30
|
|
|
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
|
|
|
|
if !ok {
|
|
|
|
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
|
|
|
}
|
|
|
|
sc := colly.NewCollector(colly.AllowedDomains("translate.google.com"), colly.UserAgent(UserAgent))
|
|
|
|
var answer string
|
|
|
|
sc.OnHTML("div.result-container", func(e *colly.HTMLElement) {
|
|
|
|
answer = e.Text
|
|
|
|
})
|
2023-07-22 19:13:42 +05:30
|
|
|
type Options struct {
|
2023-07-22 19:54:15 +05:30
|
|
|
To string `url:"tl"`
|
|
|
|
UI string `url:"hl"`
|
|
|
|
From string `url:"sl"`
|
|
|
|
Text string `url:"q"`
|
2023-07-22 19:13:42 +05:30
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
opt := Options{to, to, from, text}
|
2023-07-22 19:13:42 +05:30
|
|
|
v, _ := query.Values(opt)
|
2023-07-22 19:54:15 +05:30
|
|
|
url := "https://translate.google.com/m?" + v.Encode()
|
2023-06-25 00:10:57 +05:30
|
|
|
sc.Visit(url)
|
|
|
|
return answer
|
|
|
|
}
|
|
|
|
func TranslateReverso(to string, from string, query string) string {
|
2023-07-22 21:11:35 +05:30
|
|
|
var ToValid bool
|
|
|
|
var FromValid bool
|
|
|
|
for _, v := range LangListReverso("sl") {
|
|
|
|
if v.Id == to {
|
|
|
|
ToValid = true
|
|
|
|
}
|
|
|
|
if v.Id == from {
|
|
|
|
FromValid = true
|
|
|
|
}
|
|
|
|
if FromValid == true && ToValid == true {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ToValid != true {
|
|
|
|
return "Target Language Code invalid"
|
|
|
|
}
|
|
|
|
if FromValid != true {
|
|
|
|
return "Source Language Code invalid"
|
|
|
|
}
|
2023-06-25 00:11:11 +05:30
|
|
|
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
|
2023-07-22 19:13:42 +05:30
|
|
|
reversoOut := PostRequest("https://api.reverso.net/translate/v1/translation", json)
|
2023-06-25 00:10:57 +05:30
|
|
|
gjsonArr := reversoOut.Get("translation").Array()
|
|
|
|
answer := gjsonArr[0].String()
|
|
|
|
return answer
|
|
|
|
}
|
2023-07-03 17:29:46 +05:30
|
|
|
func TranslateLibreTranslate(to string, from string, query string) string {
|
2023-07-22 21:11:35 +05:30
|
|
|
var ToValid bool
|
|
|
|
var FromValid bool
|
|
|
|
for _, v := range LangListLibreTranslate("sl") {
|
|
|
|
if v.Id == to {
|
|
|
|
ToValid = true
|
|
|
|
}
|
|
|
|
if v.Id == from {
|
|
|
|
FromValid = true
|
|
|
|
}
|
|
|
|
if FromValid == true && ToValid == true {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ToValid != true {
|
|
|
|
return "Target Language Code invalid"
|
|
|
|
}
|
|
|
|
if FromValid != true {
|
|
|
|
return "Source Language Code invalid"
|
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
json := []byte(`{"q":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
2023-07-03 17:29:46 +05:30
|
|
|
// TODO: Make it configurable
|
2023-07-22 19:13:42 +05:30
|
|
|
libreTranslateOut := PostRequest("https://translate.argosopentech.com/translate", json)
|
2023-07-03 17:29:46 +05:30
|
|
|
gjsonArr := libreTranslateOut.Get("translatedText").Array()
|
|
|
|
answer := gjsonArr[0].String()
|
|
|
|
return answer
|
|
|
|
}
|
2023-07-22 11:12:26 +05:30
|
|
|
func TranslateWatson(to string, from string, query string) string {
|
2023-07-22 21:11:35 +05:30
|
|
|
var ToValid bool
|
|
|
|
var FromValid bool
|
|
|
|
for _, v := range LangListWatson("sl") {
|
|
|
|
if v.Id == to {
|
|
|
|
ToValid = true
|
|
|
|
}
|
|
|
|
if v.Id == from {
|
|
|
|
FromValid = true
|
|
|
|
}
|
|
|
|
if FromValid == true && ToValid == true {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ToValid != true {
|
|
|
|
return "Target Language Code invalid"
|
|
|
|
}
|
|
|
|
if FromValid != true {
|
|
|
|
return "Source Language Code invalid"
|
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
|
|
|
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
|
2023-07-22 11:12:26 +05:30
|
|
|
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
|
|
|
answer := gjsonArr[0].String()
|
|
|
|
return answer
|
|
|
|
}
|
2023-07-22 19:13:42 +05:30
|
|
|
func TranslateYandex(to string, from string, text string) string {
|
2023-07-22 21:11:35 +05:30
|
|
|
var ToValid bool
|
|
|
|
var FromValid bool
|
|
|
|
for _, v := range LangListYandex("sl") {
|
|
|
|
if v.Id == to {
|
|
|
|
ToValid = true
|
|
|
|
}
|
|
|
|
if v.Id == from {
|
|
|
|
FromValid = true
|
|
|
|
}
|
|
|
|
if FromValid == true && ToValid == true {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ToValid != true {
|
|
|
|
return "Target Language Code invalid"
|
|
|
|
}
|
|
|
|
if FromValid != true {
|
|
|
|
return "Source Language Code invalid"
|
|
|
|
}
|
2023-07-22 19:13:42 +05:30
|
|
|
type Options struct {
|
2023-07-22 19:54:15 +05:30
|
|
|
Translate string `url:"lang"`
|
|
|
|
Text string `url:"text"`
|
|
|
|
Srv string `url:"srv"`
|
|
|
|
Id string `url:"id"`
|
|
|
|
Reason string `url:"reason"`
|
2023-07-22 19:13:42 +05:30
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
opt := Options{from + "-" + to, text, "tr-mobile", "c2317111.64bac36a.ab16ef22.74722d6d6f62696c65-0-0", "submit"}
|
2023-07-22 19:13:42 +05:30
|
|
|
v, _ := query.Values(opt)
|
|
|
|
|
2023-07-22 19:54:15 +05:30
|
|
|
yandexOut := GetRequest("https://translate.yandex.net/api/v1/tr.json/translate?" + v.Encode())
|
2023-07-22 19:13:42 +05:30
|
|
|
gjsonArr := yandexOut.Get("text.0").Array()
|
|
|
|
answer := gjsonArr[0].String()
|
|
|
|
return answer
|
|
|
|
}
|
2023-06-25 00:10:57 +05:30
|
|
|
func TranslateAll(to string, from string, query string) string {
|
|
|
|
reverso := TranslateReverso(to, from, query)
|
|
|
|
google := TranslateGoogle(to, from, query)
|
2023-07-03 17:29:46 +05:30
|
|
|
libretranslate := TranslateLibreTranslate(to, from, query)
|
2023-07-22 21:11:35 +05:30
|
|
|
watson := TranslateWatson(to, from, query)
|
|
|
|
yandex := TranslateYandex(to, from, query)
|
|
|
|
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate + "\nWatson: "+ watson + "\nYandex: "+ yandex
|
2023-06-25 00:10:57 +05:30
|
|
|
}
|