mozhi/cmd/translate.go

86 lines
2.3 KiB
Go
Raw Normal View History

2023-06-25 00:10:57 +05:30
package cmd
import (
2023-08-29 14:50:56 +05:30
"encoding/json"
2023-07-03 17:29:46 +05:30
"fmt"
2023-08-29 14:50:56 +05:30
2023-08-27 19:54:30 +05:30
"codeberg.org/aryak/mozhi/utils"
2023-07-22 19:54:15 +05:30
"github.com/spf13/cobra"
2023-06-25 00:10:57 +05:30
)
2023-08-27 19:54:30 +05:30
var (
2023-08-29 14:50:56 +05:30
engine string
query string
source string
dest string
rawjson bool
2023-08-27 19:54:30 +05:30
)
2023-06-25 00:10:57 +05:30
var translateCmd = &cobra.Command{
Use: "translate",
Short: "Translate.",
Run: func(cmd *cobra.Command, args []string) {
2023-08-28 21:55:00 +05:30
if engine == "all" {
data := utils.TranslateAll(dest, source, query)
2023-08-29 14:50:56 +05:30
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)
2023-08-28 21:55:00 +05:30
}
2023-08-30 16:05:16 +05:30
fmt.Println("-----------------------------------")
2023-08-27 22:28:34 +05:30
}
2023-08-28 21:55:00 +05:30
} else {
data, err := utils.Translate(engine, dest, source, query)
2023-08-29 14:50:56 +05:30
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)
} 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)
2023-08-29 14:50:56 +05:30
}
2023-08-27 22:28:34 +05:30
}
2023-06-25 00:10:57 +05:30
}
},
}
func init() {
rootCmd.AddCommand(translateCmd)
2023-08-28 21:55:00 +05:30
translateCmd.Flags().SortFlags = false
2023-06-25 00:10:57 +05:30
2023-08-28 21:55:00 +05:30
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]")
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")
2023-08-29 14:50:56 +05:30
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
2023-06-25 00:10:57 +05:30
2023-08-28 21:55:00 +05:30
translateCmd.MarkFlagRequired("engine")
translateCmd.MarkFlagRequired("source")
translateCmd.MarkFlagRequired("dest")
translateCmd.MarkFlagRequired("query")
2023-06-25 00:10:57 +05:30
}