mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-17 04:12:58 +05:30
add error handling for incorrect to/from
This commit is contained in:
parent
9c2eaa6b99
commit
93b70ed559
106
utils/engines.go
106
utils/engines.go
@ -7,6 +7,32 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TranslateGoogle(to string, from string, text string) string {
|
func TranslateGoogle(to string, from string, text string) string {
|
||||||
|
// 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"
|
||||||
|
}
|
||||||
|
|
||||||
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
|
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
|
||||||
if !ok {
|
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"
|
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
||||||
@ -29,6 +55,25 @@ func TranslateGoogle(to string, from string, text string) string {
|
|||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
func TranslateReverso(to string, from string, query string) string {
|
func TranslateReverso(to string, from string, query string) string {
|
||||||
|
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"
|
||||||
|
}
|
||||||
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
|
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
|
||||||
reversoOut := PostRequest("https://api.reverso.net/translate/v1/translation", json)
|
reversoOut := PostRequest("https://api.reverso.net/translate/v1/translation", json)
|
||||||
gjsonArr := reversoOut.Get("translation").Array()
|
gjsonArr := reversoOut.Get("translation").Array()
|
||||||
@ -36,6 +81,25 @@ func TranslateReverso(to string, from string, query string) string {
|
|||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
func TranslateLibreTranslate(to string, from string, query string) string {
|
func TranslateLibreTranslate(to string, from string, query string) string {
|
||||||
|
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"
|
||||||
|
}
|
||||||
json := []byte(`{"q":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
json := []byte(`{"q":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
||||||
// TODO: Make it configurable
|
// TODO: Make it configurable
|
||||||
libreTranslateOut := PostRequest("https://translate.argosopentech.com/translate", json)
|
libreTranslateOut := PostRequest("https://translate.argosopentech.com/translate", json)
|
||||||
@ -44,6 +108,25 @@ func TranslateLibreTranslate(to string, from string, query string) string {
|
|||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
func TranslateWatson(to string, from string, query string) string {
|
func TranslateWatson(to string, from string, query string) string {
|
||||||
|
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"
|
||||||
|
}
|
||||||
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
||||||
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
|
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
|
||||||
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
||||||
@ -51,6 +134,25 @@ func TranslateWatson(to string, from string, query string) string {
|
|||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
func TranslateYandex(to string, from string, text string) string {
|
func TranslateYandex(to string, from string, text string) string {
|
||||||
|
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"
|
||||||
|
}
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Translate string `url:"lang"`
|
Translate string `url:"lang"`
|
||||||
Text string `url:"text"`
|
Text string `url:"text"`
|
||||||
@ -70,5 +172,7 @@ func TranslateAll(to string, from string, query string) string {
|
|||||||
reverso := TranslateReverso(to, from, query)
|
reverso := TranslateReverso(to, from, query)
|
||||||
google := TranslateGoogle(to, from, query)
|
google := TranslateGoogle(to, from, query)
|
||||||
libretranslate := TranslateLibreTranslate(to, from, query)
|
libretranslate := TranslateLibreTranslate(to, from, query)
|
||||||
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate
|
watson := TranslateWatson(to, from, query)
|
||||||
|
yandex := TranslateYandex(to, from, query)
|
||||||
|
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate + "\nWatson: "+ watson + "\nYandex: "+ yandex
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,10 @@ func LangListWatson(listType string) []List {
|
|||||||
Id: "nl",
|
Id: "nl",
|
||||||
Name: "Dutch",
|
Name: "Dutch",
|
||||||
},
|
},
|
||||||
|
List{
|
||||||
|
Id: "en",
|
||||||
|
Name: "English",
|
||||||
|
},
|
||||||
List{
|
List{
|
||||||
Id: "et",
|
Id: "et",
|
||||||
Name: "Estonian",
|
Name: "Estonian",
|
||||||
|
Loading…
Reference in New Issue
Block a user