Refactor translation
This commit is contained in:
		
							
								
								
									
										33
									
								
								trans.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								trans.go
									
									
									
									
									
								
							@@ -5,26 +5,41 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	outBytes, err := execTrans(os.Args[1])
 | 
						results := New(os.Args[1], "en", "hu").Translate()
 | 
				
			||||||
	if err != nil {
 | 
						fmt.Println(strings.Join(results, "\n"))
 | 
				
			||||||
		panic(err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	translations := parseTransOutput(string(outBytes))
 | 
					 | 
				
			||||||
	fmt.Println(strings.Join(uniqueSlice(translations), "\n"))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func execTrans(input string) ([]byte, error) {
 | 
					type transJob struct {
 | 
				
			||||||
 | 
						word string
 | 
				
			||||||
 | 
						fromLang string
 | 
				
			||||||
 | 
						toLang string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func New(word, fromLang, toLang string) transJob {
 | 
				
			||||||
 | 
						return transJob{word: word, fromLang: fromLang, toLang: toLang}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (tj transJob) Translate() []string {
 | 
				
			||||||
 | 
						outBytes, err := tj.execTrans()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatal("Failed to execute command 'trans'!")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return uniqueSlice(parseTransOutput(string(outBytes)))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (tj transJob) execTrans() ([]byte, error) {
 | 
				
			||||||
	config := []string{
 | 
						config := []string{
 | 
				
			||||||
		"-show-original", "n",
 | 
							"-show-original", "n",
 | 
				
			||||||
		"-show-original-phonetics", "n",
 | 
							"-show-original-phonetics", "n",
 | 
				
			||||||
		"-show-dictionary", "n",
 | 
							"-show-dictionary", "n",
 | 
				
			||||||
		"-show-languages", "n",
 | 
							"-show-languages", "n",
 | 
				
			||||||
		"-show-prompt-message", "n",
 | 
							"-show-prompt-message", "n",
 | 
				
			||||||
		"hu:en",
 | 
							fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang),
 | 
				
			||||||
		input,
 | 
							tj.word,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return exec.Command("trans", config...).Output()
 | 
						return exec.Command("trans", config...).Output()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user