mozhi/cmd/translate.go

107 lines
2.8 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"
"strings"
2023-08-29 14:50:56 +05:30
"codeberg.org/aryak/libmozhi"
2023-08-27 19:54:30 +05:30
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
engines string
2023-08-29 14:50:56 +05:30
query string
source string
dest string
rawjson bool
2023-08-27 19:54:30 +05:30
)
2023-06-25 00:10:57 +05:30
2024-02-20 14:41:45 +05:30
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)
}
func printRaw(data interface{}) {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
}
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 := libmozhi.TranslateAll(dest, source, query)
2023-08-29 14:50:56 +05:30
if rawjson {
2024-02-20 14:41:45 +05:30
printRaw(data)
2023-08-29 14:50:56 +05:30
} else {
2024-02-20 14:41:45 +05:30
for _, result := range data {
printEngineResult(result, true)
2023-08-28 21:55:00 +05:30
}
2024-08-27 13:02:34 +05:30
fmt.Println("-----------------------------------")
}
} else if engine == "some" {
if engines != "" {
data, err := libmozhi.TranslateSome(strings.Split(engines, ","), dest, source, query)
if rawjson {
printRaw(data)
} else {
if err != nil {
fmt.Println(err)
} else {
for _, result := range data {
printEngineResult(result, true)
}
fmt.Println("-----------------------------------")
}
}
} else {
fmt.Println("Please add the --engines / -E argument when using the engine `some`")
2023-08-27 22:28:34 +05:30
}
2023-08-28 21:55:00 +05:30
} else {
data, err := libmozhi.Translate(engine, dest, source, query)
2023-08-29 14:50:56 +05:30
if rawjson {
2024-02-20 14:41:45 +05:30
printRaw(data)
2023-08-29 14:50:56 +05:30
} else {
if err != nil {
fmt.Println(err)
} else {
2024-02-20 14:41:45 +05:30
printEngineResult(data, false)
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
2024-08-27 13:02:34 +05:30
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|some|google|libre|reverso|deepl|yandex|mymemory|duckduckgo]")
translateCmd.Flags().StringVarP(&engines, "engines", "E", "", "Engines to select. This flag is to be used with the `some` engine only")
2023-08-28 21:55:00 +05:30
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("source")
translateCmd.MarkFlagRequired("dest")
translateCmd.MarkFlagRequired("query")
2023-06-25 00:10:57 +05:30
}