add the api route for translation

This commit is contained in:
Arya 2023-08-15 21:41:02 +05:30
parent 977e3dff2e
commit f7ec6b556d
Signed by: arya
GPG Key ID: 842D12BDA50DF120
6 changed files with 62 additions and 31 deletions

View File

@ -6,7 +6,7 @@ import (
)
func HandleSourceLanguages(c *fiber.Ctx) error {
engine := c.Query("engine")
engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" {
return c.SendStatus(fiber.StatusBadRequest)
}
@ -29,7 +29,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
return c.JSON(data)
}
func HandleTargetLanguages(c *fiber.Ctx) error {
engine := c.Query("engine")
engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" {
return c.SendStatus(fiber.StatusBadRequest)
}
@ -37,7 +37,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
if engine == "google" {
data = utils.LangListGoogle("tl")
} else if engine == "libre" {
data = utils.LangListLibreTranslate("sl")
data = utils.LangListLibreTranslate("tl")
} else if engine == "reverso" {
data = utils.LangListReverso("tl")
} else if engine == "deepl" {
@ -52,8 +52,8 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
return c.JSON(data)
}
func HandleTTS(c *fiber.Ctx) error {
engine := c.Query("engine")
lang := c.Query("lang")
engine := utils.Sanitize(c.Query("engine"), "alpha")
lang := utils.Sanitize(c.Query("lang"), "alpha")
text := c.Query("text")
// Why does go not have an andor statement :(
if engine == "" {
@ -72,3 +72,35 @@ func HandleTTS(c *fiber.Ctx) error {
c.Set("Content-Type", "audio/mpeg")
return c.Send([]byte(data))
}
func HandleTranslate(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha")
from := utils.Sanitize(c.Query("from"), "alpha")
to := utils.Sanitize(c.Query("to"), "alpha")
text := c.Query("text")
if engine == "" && from == "" && to == "" && text == ""{
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
}
var data utils.LangOut
var err error
if engine == "google" {
data, err = utils.TranslateGoogle(to, from, text)
} else if engine == "libre" {
data, err = utils.TranslateLibreTranslate(to, from, text)
} else if engine == "reverso" {
data, err = utils.TranslateReverso(to, from, text)
} else if engine == "deepl" {
data, err = utils.TranslateDeepl(to, from, text)
} else if engine == "watson" {
data, err = utils.TranslateWatson(to, from, text)
} else if engine == "yandex" {
data, err = utils.TranslateYandex(to, from, text)
} else if engine == "mymemory" {
data, err = utils.TranslateMyMemory(to, from, text)
}
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
data.SourceLang = from
data.TargetLang = to
return c.JSON(data)
}

View File

@ -5,7 +5,6 @@ import (
"log"
"os"
"runtime"
"strings"
"codeberg.org/aryak/simplytranslate/pages"
"codeberg.org/aryak/simplytranslate/utils"
@ -39,23 +38,6 @@ func Serve(port string) {
EnableTrustedProxyCheck: true,
TrustedProxies: []string{"0.0.0.0/0"},
ProxyHeader: fiber.HeaderXForwardedFor,
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
link := strings.TrimPrefix(err.Error(), "Cannot GET ")
err = ctx.Status(code).Render("error", fiber.Map{
"error": err,
"link": link,
})
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return nil
},
})
// For debugging purposes
@ -97,7 +79,7 @@ func Serve(port string) {
})
app.Get("/", pages.HandleIndex)
//app.Get("/api/translate", pages.HandleTranslateApi)
app.Get("/api/translate", pages.HandleTranslate)
app.Get("/api/source_languages", pages.HandleSourceLanguages)
app.Get("/api/target_languages", pages.HandleTargetLanguages)
app.Get("/api/tts", pages.HandleTTS)

View File

@ -11,8 +11,10 @@ import (
)
type LangOut struct {
OutputText string
AutoDetect string
OutputText string `json:"translated-text"`
AutoDetect string `json:"detected"`
SourceLang string `json:"source_language"`
TargetLang string `json:"target_language"`
}
func TranslateGoogle(to string, from string, text string) (LangOut, error) {

View File

@ -1,10 +1,5 @@
package utils
type List struct {
Name string
Id string
}
func LangListReverso(listType string) []List {
// IDs got from original simplytranslate-web and trial and error. Usually first three letters of language.
var ListData = []List{

6
utils/main.go Normal file
View File

@ -0,0 +1,6 @@
package utils
type List struct {
Name string
Id string
}

14
utils/sanitize.go Normal file
View File

@ -0,0 +1,14 @@
package utils
import "regexp"
func Sanitize(str string, strip string) string {
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
if strip == "alpha" {
return nonAlphaRegex.ReplaceAllString(str, "")
} else if strip == "alphanumeric" {
return nonAlphanumericRegex.ReplaceAllString(str, "")
}
return ""
}