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