mozhi/cmd/translate.go

90 lines
2.2 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
"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
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)
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
}
}
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
}
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-05-24 14:16:31 +05:30
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|google|libre|reverso|deepl|yandex|mymemory|duckduckgo]")
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("engine")
translateCmd.MarkFlagRequired("source")
translateCmd.MarkFlagRequired("dest")
translateCmd.MarkFlagRequired("query")
2023-06-25 00:10:57 +05:30
}