Refactor translation

This commit is contained in:
Kristóf Tóth 2019-04-06 00:37:17 +02:00
parent 87320b19b8
commit ed08bb79a2
1 changed files with 24 additions and 9 deletions

View File

@ -5,26 +5,41 @@ import (
"os"
"os/exec"
"strings"
"log"
)
func main() {
outBytes, err := execTrans(os.Args[1])
if err != nil {
panic(err)
}
translations := parseTransOutput(string(outBytes))
fmt.Println(strings.Join(uniqueSlice(translations), "\n"))
results := New(os.Args[1], "en", "hu").Translate()
fmt.Println(strings.Join(results, "\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{
"-show-original", "n",
"-show-original-phonetics", "n",
"-show-dictionary", "n",
"-show-languages", "n",
"-show-prompt-message", "n",
"hu:en",
input,
fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang),
tj.word,
}
return exec.Command("trans", config...).Output()
}