mirror of
https://codeberg.org/aryak/mozhi
synced 2024-12-22 12:59:46 +05:30
add json option for transl. + gofmt
This commit is contained in:
parent
d92958aae0
commit
5ffd20046a
@ -2,9 +2,11 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"codeberg.org/aryak/mozhi/utils"
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/ktr0731/go-fuzzyfinder"
|
"github.com/ktr0731/go-fuzzyfinder"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"codeberg.org/aryak/mozhi/utils"
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -12,6 +14,7 @@ var (
|
|||||||
query string
|
query string
|
||||||
source string
|
source string
|
||||||
dest string
|
dest string
|
||||||
|
rawjson bool
|
||||||
)
|
)
|
||||||
|
|
||||||
var translateCmd = &cobra.Command{
|
var translateCmd = &cobra.Command{
|
||||||
@ -20,6 +23,14 @@ var translateCmd = &cobra.Command{
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if engine == "all" {
|
if engine == "all" {
|
||||||
data := utils.TranslateAll(dest, source, query)
|
data := utils.TranslateAll(dest, source, query)
|
||||||
|
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++ {
|
for i := 0; i < len(data); i++ {
|
||||||
fmt.Println("-----------------------------------")
|
fmt.Println("-----------------------------------")
|
||||||
fmt.Println("Engine: " + data[i].Engine)
|
fmt.Println("Engine: " + data[i].Engine)
|
||||||
@ -30,8 +41,17 @@ var translateCmd = &cobra.Command{
|
|||||||
fmt.Println("Source Language: " + data[i].SourceLang)
|
fmt.Println("Source Language: " + data[i].SourceLang)
|
||||||
fmt.Println("Target Language: " + data[i].TargetLang)
|
fmt.Println("Target Language: " + data[i].TargetLang)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
data, err := utils.Translate(engine, dest, source, query)
|
data, err := utils.Translate(engine, dest, source, query)
|
||||||
|
if rawjson {
|
||||||
|
j, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(j))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@ -42,6 +62,7 @@ var translateCmd = &cobra.Command{
|
|||||||
fmt.Println("Source Language: " + data.SourceLang)
|
fmt.Println("Source Language: " + data.SourceLang)
|
||||||
fmt.Println("Target Language: " + data.TargetLang)
|
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(&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(&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().StringVarP(&query, "query", "q", "", "Text to be translated")
|
||||||
|
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
|
||||||
|
|
||||||
translateCmd.MarkFlagRequired("engine")
|
translateCmd.MarkFlagRequired("engine")
|
||||||
translateCmd.MarkFlagRequired("source")
|
translateCmd.MarkFlagRequired("source")
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// HandleSourceLanguages godoc
|
// HandleSourceLanguages godoc
|
||||||
|
//
|
||||||
// @Summary Show list of available source languages for engine
|
// @Summary Show list of available source languages for engine
|
||||||
// @Param engine query string true "Engine name"
|
// @Param engine query string true "Engine name"
|
||||||
// @Success 200 {object} utils.List
|
// @Success 200 {object} utils.List
|
||||||
@ -23,6 +24,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleTargetLanguages godoc
|
// HandleTargetLanguages godoc
|
||||||
|
//
|
||||||
// @Summary Show list of available target languages for engine
|
// @Summary Show list of available target languages for engine
|
||||||
// @Param engine query string true "Engine name"
|
// @Param engine query string true "Engine name"
|
||||||
// @Success 200 {object} utils.List
|
// @Success 200 {object} utils.List
|
||||||
@ -40,6 +42,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleTTS godoc
|
// HandleTTS godoc
|
||||||
|
//
|
||||||
// @Summary Get Text-To-Speech for specified language using specified engine
|
// @Summary Get Text-To-Speech for specified language using specified engine
|
||||||
// @Param engine query string true "Engine name"
|
// @Param engine query string true "Engine name"
|
||||||
// @Param lang query string true "Language being TTS'd"
|
// @Param lang query string true "Language being TTS'd"
|
||||||
@ -61,6 +64,7 @@ func HandleTTS(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleTranslate godoc
|
// HandleTranslate godoc
|
||||||
|
//
|
||||||
// @Summary Translate text
|
// @Summary Translate text
|
||||||
// @Description When engine is set to all, it will return an array of utils.LangOut.
|
// @Description When engine is set to all, it will return an array of utils.LangOut.
|
||||||
// @Param engine query string true "Engine name"
|
// @Param engine query string true "Engine name"
|
||||||
|
@ -6,9 +6,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
_ "codeberg.org/aryak/mozhi/docs"
|
||||||
"codeberg.org/aryak/mozhi/pages"
|
"codeberg.org/aryak/mozhi/pages"
|
||||||
"codeberg.org/aryak/mozhi/utils"
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
_ "codeberg.org/aryak/mozhi/docs"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||||
|
Loading…
Reference in New Issue
Block a user