mozhi/cmd/translate.go

90 lines
2.2 KiB
Go
Raw Normal View History

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
"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
2024-02-20 09:11:45 +00:00
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-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" {
data := libmozhi.TranslateAll(dest, source, query)
2023-08-29 09:20:56 +00:00
if rawjson {
2024-02-20 09:11:45 +00:00
printRaw(data)
2023-08-29 09:20:56 +00:00
} else {
2024-02-20 09:11:45 +00:00
for _, result := range data {
printEngineResult(result, true)
2023-08-28 16:25:00 +00:00
}
2023-08-27 16:58:34 +00:00
}
2023-08-28 16:25:00 +00:00
} else {
data, err := libmozhi.Translate(engine, dest, source, query)
2023-08-29 09:20:56 +00:00
if rawjson {
2024-02-20 09:11:45 +00:00
printRaw(data)
2023-08-29 09:20:56 +00:00
} else {
if err != nil {
fmt.Println(err)
} else {
2024-02-20 09:11:45 +00:00
printEngineResult(data, false)
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
2024-05-24 08:46:31 +00:00
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|google|libre|reverso|deepl|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
}