libretranslate

This commit is contained in:
Arya Kiran 2023-07-03 17:29:46 +05:30
parent 4db20776fa
commit b3bfe78f58
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
4 changed files with 177 additions and 29 deletions

View file

@ -2,8 +2,8 @@ package cmd
import (
"codeberg.org/aryak/simplytranslate/utils"
"fmt"
"github.com/spf13/cobra"
"fmt"
)
var engine string
@ -25,6 +25,22 @@ var translateCmd = &cobra.Command{
} else {
fmt.Println(utils.TranslateReverso(dest, source, query))
}
} else if engine == "deepl" {
if langlist == "sl" {
fmt.Println(utils.LangListDeepl(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListDeepl(langlist))
} else {
fmt.Println("Not Implemented yet.")
}
} else if engine == "libretranslate" {
if langlist == "sl" {
fmt.Println(utils.LangListLibreTranslate(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListLibreTranslate(langlist))
} else {
fmt.Println(utils.TranslateLibreTranslate(dest, source, query))
}
} else if engine == "all" {
fmt.Println(utils.TranslateAll(dest, source, query))
} else {

7
go.mod
View file

@ -3,14 +3,10 @@ module codeberg.org/aryak/simplytranslate
go 1.20
require (
github.com/alecthomas/chroma/v2 v2.7.0
github.com/carlmjohnson/requests v0.23.3
github.com/enescakir/emoji v1.0.0
github.com/gocolly/colly v1.2.0
github.com/gofiber/fiber/v2 v2.43.0
github.com/gofiber/template v1.8.0
github.com/joho/godotenv v1.5.1
github.com/microcosm-cc/bluemonday v1.0.23
github.com/spf13/cobra v1.6.1
github.com/tidwall/gjson v1.14.4
)
@ -22,14 +18,11 @@ require (
github.com/antchfx/htmlquery v1.3.0 // indirect
github.com/antchfx/xmlquery v1.3.15 // indirect
github.com/antchfx/xpath v1.2.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/klauspost/compress v1.16.3 // indirect

17
go.sum
View file

@ -56,10 +56,6 @@ github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKd
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
github.com/alecthomas/chroma/v2 v2.7.0 h1:hm1rY6c/Ob4eGclpQ7X/A3yhqBOZNUTk9q+yhyLIViI=
github.com/alecthomas/chroma/v2 v2.7.0/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@ -81,15 +77,11 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/carlmjohnson/requests v0.23.3 h1:22EEJsJqjNWprjQtqw2nLoQ1Sz+I1qJUbvhd0cHSHUg=
github.com/carlmjohnson/requests v0.23.3/go.mod h1:Qzp6tW4DQyainPP+tGwiJTzwxvElTIKm0B191TgTtOA=
github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9DtDsHo9iM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@ -120,11 +112,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@ -239,8 +227,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.11.0/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M=
github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOjagTIwIR1vPms=
@ -272,7 +258,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@ -326,8 +311,6 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-slim v0.0.4/go.mod h1:kXIwPrQbAZrhungtweoDO6qQQpUem6XkBg/mXykpoO0=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY=
github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=

View file

@ -1,7 +1,6 @@
package utils
import (
"fmt"
"github.com/gocolly/colly"
"os"
"strings"
@ -33,12 +32,19 @@ func TranslateReverso(to string, from string, query string) string {
answer := gjsonArr[0].String()
return answer
}
func TranslateLibreTranslate(to string, from string, query string) string {
json := []byte(`{"q":"`+query+`","source":"`+from+`","target":"`+to+`"}`)
// TODO: Make it configurable
libreTranslateOut := GetRequest("https://translate.argosopentech.com/translate", json)
gjsonArr := libreTranslateOut.Get("translatedText").Array()
answer := gjsonArr[0].String()
return answer
}
func TranslateAll(to string, from string, query string) string {
reverso := TranslateReverso(to, from, query)
google := TranslateGoogle(to, from, query)
fmt.Println("Google: " + google)
fmt.Println("Reverso: " + reverso)
return "Google: " + google + "\nReverso: " + reverso
libretranslate := TranslateLibreTranslate(to, from, query)
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate
}
func LangListGoogle(listType string) []List {
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
@ -63,6 +69,26 @@ func LangListGoogle(listType string) []List {
sc.Visit(url)
return ListData
}
func LangListLibreTranslate(listType string) []List {
// TODO: Make it configurable
libreTranslateOut := GetRequest("https://translate.argosopentech.com/languages", []byte(""))
gjsonArr := libreTranslateOut.Array()
var ListData []List
for _, r := range gjsonArr {
code := r.Get("code").String()
name := r.Get("name").String()
ListData = append(ListData, List{Id: code, Name: name})
}
if listType == "sl" {
auto := List{
Id: "auto",
Name: "Detect Language",
}
ListData = append(ListData, auto)
}
return ListData
}
func LangListReverso(listType string) []List {
// IDs got from original simplytranslate-web and trial and error. Usually first three letters of language.
var ListData = []List{
@ -173,3 +199,133 @@ func LangListReverso(listType string) []List {
}
return ListData
}
func LangListDeepl(listType string) []List {
// IDs got from deepl.com/translator
// Every time you change language, the # will get updated with the lang code.
var ListData = []List{
List{
Id: "bg",
Name: "Bulgarian",
},
List{
Id: "zh",
Name: "Chinese",
},
List{
Id: "cs",
Name: "Czech",
},
List{
Id: "da",
Name: "Danish",
},
List{
Id: "nl",
Name: "Dutch",
},
List{
Id: "en",
Name: "English",
},
List{
Id: "et",
Name: "Estonian",
},
List{
Id: "fi",
Name: "Finnish",
},
List{
Id: "fr",
Name: "French",
},
List{
Id: "de",
Name: "Germany",
},
List{
Id: "el",
Name: "Greek",
},
List{
Id: "hu",
Name: "Hungarian",
},
List{
Id: "id",
Name: "Indonesian",
},
List{
Id: "it",
Name: "Italian",
},
List{
Id: "ja",
Name: "Japanese",
},
List{
Id: "ko",
Name: "Korean",
},
List{
Id: "lv",
Name: "Latvian",
},
List{
Id: "lt",
Name: "Lithuanian",
},
List{
Id: "nb",
Name: "Norwegian",
},
List{
Id: "pl",
Name: "Polish",
},
List{
Id: "pt",
Name: "Portugese",
},
List{
Id: "ro",
Name: "Romanian",
},
List{
Id: "ru",
Name: "Russian",
},
List{
Id: "sk",
Name: "Slovak",
},
List{
Id: "sl",
Name: "Slovenian",
},
List{
Id: "es",
Name: "Spanish",
},
List{
Id: "sv",
Name: "Swedish",
},
List{
Id: "tr",
Name: "Turkish",
},
List{
Id: "uk",
Name: "Ukrainian",
},
}
if listType == "sl" {
auto := List{
Id: "auto",
Name: "Detect Language",
}
ListData = append(ListData, auto)
}
return ListData
}