separate langlist and engines

This commit is contained in:
Arya 2023-07-03 17:35:56 +05:30
parent b3bfe78f58
commit feb3baef9e
Signed by: arya
GPG Key ID: 842D12BDA50DF120
2 changed files with 296 additions and 288 deletions

View File

@ -6,11 +6,6 @@ import (
"strings"
)
type List struct {
Name string
Id string
}
func TranslateGoogle(to string, from string, query string) string {
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
if !ok {
@ -46,286 +41,3 @@ func TranslateAll(to string, from string, query string) string {
libretranslate := TranslateLibreTranslate(to, from, query)
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate
}
func LangListGoogle(listType string) []List {
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 ListData []List
sc.OnHTML("div.root-container", func(e *colly.HTMLElement) {
e.ForEach("div.language-item", func(i int, el *colly.HTMLElement) {
var ListInfo List
ListInfo.Name = el.ChildText("a")
if listType == "tl" {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&hl=en-US"), "./m?sl=en&tl=")
} else {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&tl=en&hl=en-US"), "./m?sl=")
}
ListData = append(ListData, ListInfo)
})
})
url := "https://translate.google.com/m?sl=en&tl=en&hl=en-US&mui=" + listType
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{
List{
Id: "ara",
Name: "Arabic",
},
List{
Id: "chi",
Name: "Chinese (Simplified)",
},
List{
Id: "cze",
Name: "Czech",
},
List{
Id: "dan",
Name: "Danish",
},
List{
Id: "dut",
Name: "Dutch",
},
List{
Id: "eng",
Name: "English",
},
List{
Id: "fra",
Name: "French",
},
List{
Id: "ger",
Name: "German",
},
List{
Id: "gre",
Name: "Greek",
},
List{
Id: "Hebrew",
Name: "heb",
},
List{
Id: "hin",
Name: "Hindi",
},
List{
Id: "hun",
Name: "Hungarian",
},
List{
Id: "ita",
Name: "Italian",
},
List{
Id: "jpn",
Name: "Japanese",
},
List{
Id: "kor",
Name: "Korean",
},
List{
Id: "per",
Name: "Persian",
},
List{
Id: "pol",
Name: "Polish",
},
List{
Id: "por",
Name: "Portugese",
},
List{
Id: "rum",
Name: "Romanian",
},
List{
Id: "rus",
Name: "Russian",
},
List{
Id: "slo",
Name: "Slovakian",
},
List{
Id: "spa",
Name: "Spanish",
},
List{
Id: "swe",
Name: "Swedish",
},
List{
Id: "tha",
Name: "Thai",
},
List{
Id: "tur",
Name: "Turkish",
},
List{
Id: "ukr",
Name: "Ukrainian",
},
}
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
}

296
utils/langlist.go Normal file
View File

@ -0,0 +1,296 @@
package utils
import (
"github.com/gocolly/colly"
"os"
"strings"
)
type List struct {
Name string
Id string
}
func LangListGoogle(listType string) []List {
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 ListData []List
sc.OnHTML("div.root-container", func(e *colly.HTMLElement) {
e.ForEach("div.language-item", func(i int, el *colly.HTMLElement) {
var ListInfo List
ListInfo.Name = el.ChildText("a")
if listType == "tl" {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&hl=en-US"), "./m?sl=en&tl=")
} else {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&tl=en&hl=en-US"), "./m?sl=")
}
ListData = append(ListData, ListInfo)
})
})
url := "https://translate.google.com/m?sl=en&tl=en&hl=en-US&mui=" + listType
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{
List{
Id: "ara",
Name: "Arabic",
},
List{
Id: "chi",
Name: "Chinese (Simplified)",
},
List{
Id: "cze",
Name: "Czech",
},
List{
Id: "dan",
Name: "Danish",
},
List{
Id: "dut",
Name: "Dutch",
},
List{
Id: "eng",
Name: "English",
},
List{
Id: "fra",
Name: "French",
},
List{
Id: "ger",
Name: "German",
},
List{
Id: "gre",
Name: "Greek",
},
List{
Id: "Hebrew",
Name: "heb",
},
List{
Id: "hin",
Name: "Hindi",
},
List{
Id: "hun",
Name: "Hungarian",
},
List{
Id: "ita",
Name: "Italian",
},
List{
Id: "jpn",
Name: "Japanese",
},
List{
Id: "kor",
Name: "Korean",
},
List{
Id: "per",
Name: "Persian",
},
List{
Id: "pol",
Name: "Polish",
},
List{
Id: "por",
Name: "Portugese",
},
List{
Id: "rum",
Name: "Romanian",
},
List{
Id: "rus",
Name: "Russian",
},
List{
Id: "slo",
Name: "Slovakian",
},
List{
Id: "spa",
Name: "Spanish",
},
List{
Id: "swe",
Name: "Swedish",
},
List{
Id: "tha",
Name: "Thai",
},
List{
Id: "tur",
Name: "Turkish",
},
List{
Id: "ukr",
Name: "Ukrainian",
},
}
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
}