From 9f7db5e3479dd50fb09569e1aa49c18eae6aea84 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Sun, 23 Jul 2023 20:00:20 +0530 Subject: [PATCH] Finally DeepL --- TODO.md | 2 - cmd/translate.go | 2 +- go.mod | 2 + go.sum | 4 + utils/engines.go | 29 +++ utils/langlist-bing.go | 520 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 556 insertions(+), 3 deletions(-) create mode 100644 utils/langlist-bing.go diff --git a/TODO.md b/TODO.md index 48af2b0..b39cfd5 100644 --- a/TODO.md +++ b/TODO.md @@ -6,6 +6,4 @@ - Alternative words - Dictionary - Bing Translate -- MyMemory -- DeepL - ICIBA diff --git a/cmd/translate.go b/cmd/translate.go index 0f6c6cf..bc6733e 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -31,7 +31,7 @@ var translateCmd = &cobra.Command{ } else if langlist == "tl" { fmt.Println(utils.LangListDeepl(langlist)) } else { - fmt.Println("Not Implemented yet.") + fmt.Println(utils.TranslateDeepl(dest, source, query)) } } else if engine == "libretranslate" { if langlist == "sl" { diff --git a/go.mod b/go.mod index fd210dd..2944674 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,9 @@ require ( ) require ( + github.com/OwO-Network/gdeeplx v0.0.1 // indirect github.com/PuerkitoBio/goquery v1.8.1 // indirect + github.com/abadojack/whatlanggo v1.0.1 // indirect github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/antchfx/htmlquery v1.3.0 // indirect diff --git a/go.sum b/go.sum index 027358e..36a82ce 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,12 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/OwO-Network/gdeeplx v0.0.1 h1:uWkXp1AiD3jAgsg9Gw5ak8TH6ZZ/RjgfFXgYA8kyE3g= +github.com/OwO-Network/gdeeplx v0.0.1/go.mod h1:xLqaj0n6E2VmiI1RXBOFy8wLoiOurvO3O6QxUvepyVo= github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= +github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4= +github.com/abadojack/whatlanggo v1.0.1/go.mod h1:66WiQbSbJBIlOZMsvbKe5m6pzQovxCH9B/K8tQB2uoc= 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= diff --git a/utils/engines.go b/utils/engines.go index af47dd2..292154c 100644 --- a/utils/engines.go +++ b/utils/engines.go @@ -3,6 +3,7 @@ package utils import ( "github.com/gocolly/colly" "github.com/google/go-querystring/query" + "github.com/OwO-Network/gdeeplx" "os" ) @@ -199,6 +200,34 @@ func TranslateYandex(to string, from string, text string) string { answer := gjsonArr[0].String() return answer } +func TranslateDeepl(to string, from string, text string) string { + var ToValid bool + var FromValid bool + for _, v := range LangListDeepl("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" + } + answer, err := gdeeplx.Translate(text, from, to, 0) + if err != nil { + return "failed" + } + answer1 := answer.(map[string]interface{}) + ans:= answer1["data"].(string) + return ans +} func TranslateAll(to string, from string, query string) string { reverso := TranslateReverso(to, from, query) google := TranslateGoogle(to, from, query) diff --git a/utils/langlist-bing.go b/utils/langlist-bing.go new file mode 100644 index 0000000..ff3482b --- /dev/null +++ b/utils/langlist-bing.go @@ -0,0 +1,520 @@ +package utils + +func LangListBing(listType string) []List { + // Converted json of https://github.com/plainheart/bing-translate-api/blob/master/src/lang.json to this format + var ListData = []List{ + List{ + Id: "af", + Name: "Afrikaans", + }, + List{ + Id: "sq", + Name: "Albanian", + }, + List{ + Id: "am", + Name: "Amharic", + }, + List{ + Id: "ar", + Name: "Arabic", + }, + List{ + Id: "hy", + Name: "Armenian", + }, + List{ + Id: "as", + Name: "Assamese", + }, + List{ + Id: "az", + Name: "Azerbaijani", + }, + List{ + Id: "bn", + Name: "Bangla", + }, + List{ + Id: "ba", + Name: "Bashkir", + }, + List{ + Id: "eu", + Name: "Basque", + }, + List{ + Id: "bs", + Name: "Bosnian", + }, + List{ + Id: "bg", + Name: "Bulgarian", + }, + List{ + Id: "yue", + Name: "Cantonese (Traditional)", + }, + List{ + Id: "ca", + Name: "Catalan", + }, + List{ + Id: "lzh", + Name: "Chinese (Literary)", + }, + List{ + Id: "zh-Hans", + Name: "Chinese Simplified", + }, + List{ + Id: "zh-Hant", + Name: "Chinese Traditional", + }, + List{ + Id: "hr", + Name: "Croatian", + }, + List{ + Id: "cs", + Name: "Czech", + }, + List{ + Id: "da", + Name: "Danish", + }, + List{ + Id: "prs", + Name: "Dari", + }, + List{ + Id: "dv", + Name: "Divehi", + }, + List{ + Id: "nl", + Name: "Dutch", + }, + List{ + Id: "en", + Name: "English", + }, + List{ + Id: "et", + Name: "Estonian", + }, + List{ + Id: "fo", + Name: "Faroese", + }, + List{ + Id: "fj", + Name: "Fijian", + }, + List{ + Id: "fil", + Name: "Filipino", + }, + List{ + Id: "fi", + Name: "Finnish", + }, + List{ + Id: "fr", + Name: "French", + }, + List{ + Id: "fr-CA", + Name: "French (Canada)", + }, + List{ + Id: "gl", + Name: "Galician", + }, + List{ + Id: "lug", + Name: "Ganda", + }, + List{ + Id: "ka", + Name: "Georgian", + }, + List{ + Id: "de", + Name: "German", + }, + List{ + Id: "el", + Name: "Greek", + }, + List{ + Id: "gu", + Name: "Gujarati", + }, + List{ + Id: "ht", + Name: "Haitian Creole", + }, + List{ + Id: "ha", + Name: "Hausa", + }, + List{ + Id: "he", + Name: "Hebrew", + }, + List{ + Id: "hi", + Name: "Hindi", + }, + List{ + Id: "mww", + Name: "Hmong Daw", + }, + List{ + Id: "hu", + Name: "Hungarian", + }, + List{ + Id: "is", + Name: "Icelandic", + }, + List{ + Id: "ig", + Name: "Igbo", + }, + List{ + Id: "id", + Name: "Indonesian", + }, + List{ + Id: "ikt", + Name: "Inuinnaqtun", + }, + List{ + Id: "iu", + Name: "Inuktitut", + }, + List{ + Id: "iu-Latn", + Name: "Inuktitut (Latin)", + }, + List{ + Id: "ga", + Name: "Irish", + }, + List{ + Id: "it", + Name: "Italian", + }, + List{ + Id: "ja", + Name: "Japanese", + }, + List{ + Id: "kn", + Name: "Kannada", + }, + List{ + Id: "kk", + Name: "Kazakh", + }, + List{ + Id: "km", + Name: "Khmer", + }, + List{ + Id: "rw", + Name: "Kinyarwanda", + }, + List{ + Id: "tlh-Latn", + Name: "Klingon (Latin)", + }, + List{ + Id: "gom", + Name: "Konkani", + }, + List{ + Id: "ko", + Name: "Korean", + }, + List{ + Id: "ku", + Name: "Kurdish (Central)", + }, + List{ + Id: "kmr", + Name: "Kurdish (Northern)", + }, + List{ + Id: "ky", + Name: "Kyrgyz", + }, + List{ + Id: "lo", + Name: "Lao", + }, + List{ + Id: "lv", + Name: "Latvian", + }, + List{ + Id: "ln", + Name: "Lingala", + }, + List{ + Id: "lt", + Name: "Lithuanian", + }, + List{ + Id: "dsb", + Name: "Lower Sorbian", + }, + List{ + Id: "mk", + Name: "Macedonian", + }, + List{ + Id: "mai", + Name: "Maithili", + }, + List{ + Id: "mg", + Name: "Malagasy", + }, + List{ + Id: "ms", + Name: "Malay", + }, + List{ + Id: "ml", + Name: "Malayalam", + }, + List{ + Id: "mt", + Name: "Maltese", + }, + List{ + Id: "mr", + Name: "Marathi", + }, + List{ + Id: "mn-Cyrl", + Name: "Mongolian (Cyrillic)", + }, + List{ + Id: "mn-Mong", + Name: "Mongolian (Traditional)", + }, + List{ + Id: "my", + Name: "Myanmar (Burmese)", + }, + List{ + Id: "mi", + Name: "Māori", + }, + List{ + Id: "ne", + Name: "Nepali", + }, + List{ + Id: "nb", + Name: "Norwegian", + }, + List{ + Id: "nya", + Name: "Nyanja", + }, + List{ + Id: "or", + Name: "Odia", + }, + List{ + Id: "ps", + Name: "Pashto", + }, + List{ + Id: "fa", + Name: "Persian", + }, + List{ + Id: "pl", + Name: "Polish", + }, + List{ + Id: "pt", + Name: "Portuguese (Brazil)", + }, + List{ + Id: "pt-PT", + Name: "Portuguese (Portugal)", + }, + List{ + Id: "pa", + Name: "Punjabi", + }, + List{ + Id: "otq", + Name: "Querétaro Otomi", + }, + List{ + Id: "ro", + Name: "Romanian", + }, + List{ + Id: "run", + Name: "Rundi", + }, + List{ + Id: "ru", + Name: "Russian", + }, + List{ + Id: "sm", + Name: "Samoan", + }, + List{ + Id: "sr-Cyrl", + Name: "Serbian (Cyrillic)", + }, + List{ + Id: "sr-Latn", + Name: "Serbian (Latin)", + }, + List{ + Id: "st", + Name: "Sesotho", + }, + List{ + Id: "nso", + Name: "Sesotho sa Leboa", + }, + List{ + Id: "tn", + Name: "Setswana", + }, + List{ + Id: "sn", + Name: "Shona", + }, + List{ + Id: "sd", + Name: "Sindhi", + }, + List{ + Id: "si", + Name: "Sinhala", + }, + List{ + Id: "sk", + Name: "Slovak", + }, + List{ + Id: "sl", + Name: "Slovenian", + }, + List{ + Id: "so", + Name: "Somali", + }, + List{ + Id: "es", + Name: "Spanish", + }, + List{ + Id: "sw", + Name: "Swahili", + }, + List{ + Id: "sv", + Name: "Swedish", + }, + List{ + Id: "ty", + Name: "Tahitian", + }, + List{ + Id: "ta", + Name: "Tamil", + }, + List{ + Id: "tt", + Name: "Tatar", + }, + List{ + Id: "te", + Name: "Telugu", + }, + List{ + Id: "th", + Name: "Thai", + }, + List{ + Id: "bo", + Name: "Tibetan", + }, + List{ + Id: "ti", + Name: "Tigrinya", + }, + List{ + Id: "to", + Name: "Tongan", + }, + List{ + Id: "tr", + Name: "Turkish", + }, + List{ + Id: "tk", + Name: "Turkmen", + }, + List{ + Id: "uk", + Name: "Ukrainian", + }, + List{ + Id: "hsb", + Name: "Upper Sorbian", + }, + List{ + Id: "ur", + Name: "Urdu", + }, + List{ + Id: "ug", + Name: "Uyghur", + }, + List{ + Id: "uz", + Name: "Uzbek (Latin)", + }, + List{ + Id: "vi", + Name: "Vietnamese", + }, + List{ + Id: "cy", + Name: "Welsh", + }, + List{ + Id: "xh", + Name: "Xhosa", + }, + List{ + Id: "yo", + Name: "Yoruba", + }, + List{ + Id: "yua", + Name: "Yucatec Maya", + }, + List{ + Id: "zu", + Name: "Zulu", + }, + } + return ListData +}