mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-29 01:22:01 +05:30
Compare commits
2 Commits
c985f14cfe
...
05d39a5013
Author | SHA1 | Date | |
---|---|---|---|
05d39a5013 | |||
|
65a9fe29b2 |
@ -33,7 +33,7 @@ var langlistCmd = &cobra.Command{
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, idx := range idxs {
|
||||
if raw == true {
|
||||
if raw {
|
||||
fmt.Println(list[idx].Id)
|
||||
} else {
|
||||
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")
|
||||
|
@ -17,6 +17,33 @@ var (
|
||||
rawjson bool
|
||||
)
|
||||
|
||||
func printEngineResult(result libmozhi.LangOut, printPlaceHolderAndEngineName bool) {
|
||||
if printPlaceHolderAndEngineName {
|
||||
fmt.Println("-----------------------------------")
|
||||
fmt.Println("Engine: " + result.Engine)
|
||||
}
|
||||
|
||||
fmt.Println("Translated Text: " + result.OutputText)
|
||||
if source == "auto" {
|
||||
fmt.Println("Detected Language: " + result.AutoDetect)
|
||||
}
|
||||
fmt.Println("Source Language: " + result.SourceLang)
|
||||
fmt.Println("Target Language: " + result.TargetLang)
|
||||
|
||||
if printPlaceHolderAndEngineName {
|
||||
fmt.Println("-----------------------------------")
|
||||
}
|
||||
}
|
||||
|
||||
func printRaw(data interface{}) {
|
||||
j, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(string(j))
|
||||
}
|
||||
}
|
||||
|
||||
var translateCmd = &cobra.Command{
|
||||
Use: "translate",
|
||||
Short: "Translate.",
|
||||
@ -24,44 +51,21 @@ var translateCmd = &cobra.Command{
|
||||
if engine == "all" {
|
||||
data := libmozhi.TranslateAll(dest, source, query)
|
||||
if rawjson {
|
||||
j, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(string(j))
|
||||
}
|
||||
printRaw(data)
|
||||
} else {
|
||||
for i := 0; i < len(data); i++ {
|
||||
fmt.Println("-----------------------------------")
|
||||
fmt.Println("Engine: " + data[i].Engine)
|
||||
fmt.Println("Translated Text: " + data[i].OutputText)
|
||||
if source == "auto" {
|
||||
fmt.Println("Detected Language: " + data[i].AutoDetect)
|
||||
}
|
||||
fmt.Println("Source Language: " + data[i].SourceLang)
|
||||
fmt.Println("Target Language: " + data[i].TargetLang)
|
||||
for _, result := range data {
|
||||
printEngineResult(result, true)
|
||||
}
|
||||
fmt.Println("-----------------------------------")
|
||||
}
|
||||
} else {
|
||||
data, err := libmozhi.Translate(engine, dest, source, query)
|
||||
if rawjson {
|
||||
j, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(string(j))
|
||||
}
|
||||
printRaw(data)
|
||||
} else {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Translated Text: " + data.OutputText)
|
||||
if source == "auto" {
|
||||
fmt.Println("Detected Language: " + data.AutoDetect)
|
||||
}
|
||||
fmt.Println("Source Language: " + data.SourceLang)
|
||||
fmt.Println("Target Language: " + data.TargetLang)
|
||||
printEngineResult(data, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
pages/api.go
12
pages/api.go
@ -82,20 +82,14 @@ func HandleTranslate(c *fiber.Ctx) error {
|
||||
if engine == "" || from == "" || to == "" || text == "" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
|
||||
}
|
||||
var dataarr []libmozhi.LangOut
|
||||
var data libmozhi.LangOut
|
||||
var err error
|
||||
if engine == "all" {
|
||||
dataarr = libmozhi.TranslateAll(to, from, text)
|
||||
dataarr := libmozhi.TranslateAll(to, from, text)
|
||||
return c.JSON(dataarr)
|
||||
} else {
|
||||
data, err = libmozhi.Translate(engine, to, from, text)
|
||||
data, err := libmozhi.Translate(engine, to, from, text)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
if engine == "all" {
|
||||
return c.JSON(dataarr)
|
||||
} else {
|
||||
return c.JSON(data)
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
|
||||
sl := []libmozhi.List{}
|
||||
tl := []libmozhi.List{}
|
||||
for key, _ := range engines {
|
||||
for key := range engines {
|
||||
temp, _ := libmozhi.LangList(key, "sl")
|
||||
temp2, _ := libmozhi.LangList(key, "tl")
|
||||
sl = append(sl, temp...)
|
||||
|
@ -7,6 +7,9 @@ import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z]+`)
|
||||
var nonAlphaRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
|
||||
|
||||
func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
|
||||
if c.Method() == "POST" {
|
||||
return c.FormValue(key)
|
||||
@ -16,15 +19,12 @@ func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
|
||||
}
|
||||
|
||||
func EnvTrueNoExist(env string) bool {
|
||||
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
_, envFound := os.LookupEnv(env)
|
||||
|
||||
return !envFound || os.Getenv(env) == "true"
|
||||
}
|
||||
|
||||
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" {
|
||||
|
Loading…
Reference in New Issue
Block a user