/
languageSelector.go
46 lines (36 loc) · 1.14 KB
/
languageSelector.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
package main
import (
"fmt"
"cloud.google.com/go/translate"
"github.com/divanvisagie/ui"
)
func populateLanguages() ([]translate.Language, error) {
if apiKey == "" {
return nil, fmt.Errorf("apikey is empty")
}
languages, err := ListSupportedLanguages(apiKey, "en")
return languages, err
}
// CreateLanguageSelector creates the combobox that is used to select a languague to translate to
func CreateLanguageSelector(targetLanguageStore *StringStore, destinationFilePathStore *StringStore) *ui.Combobox {
options, _ := populateLanguages()
validKey := IsAPIKeyValid(apiKey)
if !validKey {
ui.MsgBoxError(window, "Error", "API key is not valid, set GOOGLE_API_KEY in your environment variables")
}
selector := ui.NewCombobox()
for _, l := range options {
selector.Append(l.Tag.String())
}
selector.OnSelected(func(c *ui.Combobox) {
itemIndex := c.Selected()
languageCode := options[itemIndex].Tag.String()
targetLanguageStore.SetValue(languageCode)
fmt.Println(languageCode)
if destinationFilePathStore.value == "" {
guess := GuessTarget(targetLanguageStore)
destinationFilePathStore.SetValue(guess)
}
})
return selector
}