-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (122 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"log"
"strings"
"github.com/andlabs/ui"
_ "github.com/andlabs/ui/winmanifest"
"github.com/arrufat/papago"
)
func makeTranslatePage() ui.Control {
vbox := ui.NewVerticalBox()
hbox := ui.NewHorizontalBox()
hbox.SetPadded(true)
// source language group
group := ui.NewGroup("Source Language")
group.SetMargined(true)
sourceLangCBox := ui.NewCombobox()
targetLangCBox := ui.NewCombobox()
for _, lang := range papago.SupportedLanguages() {
sourceLangCBox.Append(lang.String())
targetLangCBox.Append(lang.String())
}
sourceLangCBox.SetSelected(1)
targetLangCBox.SetSelected(0)
smle := ui.NewMultilineEntry()
group.SetChild(sourceLangCBox)
vbox.Append(group, false)
vbox.Append(smle, true)
hbox.Append(vbox, true)
// swap button
swapBtn := ui.NewButton(" ⇄ ")
grid := ui.NewGrid()
grid.SetPadded(true)
grid.Append(swapBtn, 0, 0, 2, 1, false, ui.AlignCenter, false, ui.AlignCenter)
translateBtn := ui.NewButton("Translate")
grid.Append(translateBtn, 0, 1, 2, 1, false, ui.AlignCenter, false, ui.AlignCenter)
hbox.Append(grid, false)
// target language group
vbox = ui.NewVerticalBox()
group = ui.NewGroup("Target Language")
group.SetMargined(true)
tmle := ui.NewMultilineEntry()
group.SetChild(targetLangCBox)
vbox.Append(group, false)
vbox.Append(tmle, true)
hbox.Append(vbox, true)
translateCallback := func() {
if smle.Text() == "" {
return
}
var sourceLang, targetLang papago.Language
sourceLangIdx := sourceLangCBox.Selected()
if sourceLangIdx == -1 {
detLang, err := papago.Detect(smle.Text())
if err != nil {
log.Fatal(err)
}
sourceLang = detLang
} else {
sourceLang = papago.SupportedLanguages()[sourceLangIdx]
}
targetLangIdx := targetLangCBox.Selected()
if targetLangIdx == -1 {
log.Println("Target language not selected, skipping...")
return
}
targetLang = papago.SupportedLanguages()[targetLangIdx]
if sourceLang == targetLang {
log.Println("Source and target languages are the same, skipping...")
return
}
text := strings.Replace(smle.Text(), "\n", "\\n", -1)
fmt.Printf("%q\n", text)
translation, err := papago.Translate(text, sourceLang, targetLang, papago.TranslateOptions{})
if err != nil {
log.Fatal(err)
}
tmle.SetText(translation)
}
targetLangCBox.OnSelected(func(*ui.Combobox) {
translateCallback()
})
translateBtn.OnClicked(func(*ui.Button) {
translateCallback()
})
smle.OnChanged(func(*ui.MultilineEntry) {
// if smle.Text() == "" || strings.HasSuffix(smle.Text(), "\n") {
// translateCallback()
// }
})
swapBtn.OnClicked(func(*ui.Button) {
auxLangIdx := sourceLangCBox.Selected()
auxText := smle.Text()
sourceLangCBox.SetSelected(targetLangCBox.Selected())
targetLangCBox.SetSelected(auxLangIdx)
smle.SetText(tmle.Text())
tmle.SetText(auxText)
})
return hbox
}
// func makeSettingsPage() ui.Control {
// }
func setupUI() {
mainwin := ui.NewWindow("NAVER Papago Translate", 640, 480, true)
mainwin.OnClosing(func(*ui.Window) bool {
ui.Quit()
return true
})
ui.OnShouldQuit(func() bool {
mainwin.Destroy()
return true
})
tab := ui.NewTab()
mainwin.SetChild(tab)
mainwin.SetMargined(true)
tab.Append("Translation", makeTranslatePage())
tab.SetMargined(0, true)
mainwin.Show()
}
func main() {
ui.Main(setupUI)
}