add json option for transl. + gofmt

This commit is contained in:
Arya Kiran 2023-08-29 14:50:56 +05:30
parent d92958aae0
commit 5ffd20046a
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
4 changed files with 58 additions and 30 deletions

View file

@ -2,15 +2,17 @@ package cmd
import (
"fmt"
"codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/spf13/cobra"
)
var (
engineused string
listtype string
raw bool
listtype string
raw bool
)
// langlistCmd represents the langlist command

View file

@ -1,17 +1,20 @@
package cmd
import (
"encoding/json"
"fmt"
"codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra"
)
var (
engine string
query string
source string
dest string
engine string
query string
source string
dest string
rawjson bool
)
var translateCmd = &cobra.Command{
@ -20,27 +23,45 @@ var translateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
if engine == "all" {
data := utils.TranslateAll(dest, source, query)
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)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} 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)
}
fmt.Println("Source Language: "+data[i].SourceLang)
fmt.Println("Target Language: "+data[i].TargetLang)
}
} else {
data, err := utils.Translate(engine, dest, source, query)
if err != nil {
fmt.Println(err)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} else {
if err != nil {
fmt.Println(err)
}
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)
}
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)
}
},
}
@ -53,6 +74,7 @@ func init() {
translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated")
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
translateCmd.MarkFlagRequired("engine")
translateCmd.MarkFlagRequired("source")

View file

@ -6,6 +6,7 @@ import (
)
// HandleSourceLanguages godoc
//
// @Summary Show list of available source languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} utils.List
@ -23,6 +24,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
}
// HandleTargetLanguages godoc
//
// @Summary Show list of available target languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} utils.List
@ -40,6 +42,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
}
// HandleTTS godoc
//
// @Summary Get Text-To-Speech for specified language using specified engine
// @Param engine query string true "Engine name"
// @Param lang query string true "Language being TTS'd"
@ -61,6 +64,7 @@ func HandleTTS(c *fiber.Ctx) error {
}
// HandleTranslate godoc
//
// @Summary Translate text
// @Description When engine is set to all, it will return an array of utils.LangOut.
// @Param engine query string true "Engine name"

View file

@ -6,9 +6,9 @@ import (
"os"
"runtime"
_ "codeberg.org/aryak/mozhi/docs"
"codeberg.org/aryak/mozhi/pages"
"codeberg.org/aryak/mozhi/utils"
_ "codeberg.org/aryak/mozhi/docs"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
@ -22,12 +22,12 @@ import (
_ "github.com/joho/godotenv/autoload"
)
// @title Mozhi API
// @version 1.0
// @description API for Mozhi, the alternative-frontend for many translation engines.
// @license.name AGPL 3.0
// @license.url https://www.gnu.org/licenses/agpl-3.0.txt
// @BasePath /api
// @title Mozhi API
// @version 1.0
// @description API for Mozhi, the alternative-frontend for many translation engines.
// @license.name AGPL 3.0
// @license.url https://www.gnu.org/licenses/agpl-3.0.txt
// @BasePath /api
func Serve(port string) {
engine := html.New("./views", ".html")