Identify langage if no language map is provided by user

This commit is contained in:
Kristóf Tóth 2019-10-12 00:33:16 +02:00
parent 4aa5a8b0a9
commit c04b2bcf3a

16
main.go
View File

@ -5,6 +5,7 @@ import (
"os" "os"
"strings" "strings"
"./trans" "./trans"
"./identify"
"github.com/deanishe/awgo" "github.com/deanishe/awgo"
"regexp" "regexp"
) )
@ -21,6 +22,9 @@ func main() {
func run() { func run() {
word, fromLang, toLang := parseProgramArgs(os.Args) word, fromLang, toLang := parseProgramArgs(os.Args)
if fromLang == "" && toLang == "" {
fromLang, toLang = decideWhatLanguagesToUse(word)
}
translations := trans.New(word, fromLang, toLang).Translate() translations := trans.New(word, fromLang, toLang).Translate()
for _, translation := range translations { for _, translation := range translations {
@ -52,3 +56,15 @@ func isTransLanguageSpecifier(arg string) bool {
ok, _ := regexp.MatchString("(\\w{2})?:(\\w{2})?", os.Args[1]) ok, _ := regexp.MatchString("(\\w{2})?:(\\w{2})?", os.Args[1])
return ok return ok
} }
func decideWhatLanguagesToUse(word string) (string, string) {
fromLang := identify.New(word).Identify()
toLang := map[string]string{
"en": "hu",
"hu": "en",
}[fromLang]
if toLang == "" {
toLang = "en"
}
return fromLang, toLang
}