Skip to content

Commit

Permalink
add speak button, speak use gg api
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuấn Đỗ Hoàng committed May 28, 2023
1 parent e58f73d commit 68f8c05
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ to install the executable file.

Or build from source code.

In order to listen to the speak of the text, install [google-speech](https://pypi.org/project/google-speech/)
and its dependencies

```sh
pip install google-speech
sudo apt-get install sox
sudo apt-get install libsox-fmt-mp3
```

And create a shortcut to run the command.

![image](https://user-images.githubusercontent.com/36019052/233822979-fe205d12-d59e-463d-896c-1c47bcbaaec5.png)
Expand Down
62 changes: 48 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import (
"golang.org/x/text/unicode/norm"
)

func openTlsBtn(text string) *gtk.Button {
openGgtlstBtn := gtk.NewButtonWithLabel("Open Google Translate")
openGgtlstBtn.SetSizeRequest(20, 10)

openGgtlstBtn.Connect("clicked", func() {
url := fmt.Sprintf("https://translate.google.com/?sl=en&tl=vi&text=%s&op=translate", text)
cmd := exec.Command("google-chrome", url)
err := cmd.Start()
if err != nil {
fmt.Println("Failed to execute commandeclaredd:", err)
}
})
return openGgtlstBtn
}

func getSpeakBtn(text string) *gtk.Button {
btn := gtk.NewButtonWithLabel("Speak")
btn.SetSizeRequest(20, 10)

btn.Connect("clicked", func() {
cmd := exec.Command("google_speech", text)
err := cmd.Start()

if err != nil {
fmt.Println("Failed to execute command:", err)
}
})
return btn
}

func getWindow(originText, transText string) *gtk.Window {

window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
Expand All @@ -22,28 +52,33 @@ func getWindow(originText, transText string) *gtk.Window {
textview.SetWrapMode(gtk.WRAP_WORD)
textview.SetEditable(false)
textview.SetCursorVisible(false)
// textview.SetSizeRequest(20, 10)
var iter gtk.TextIter
buffer := textview.GetBuffer()

buffer.GetStartIter(&iter)
buffer.Insert(&iter, transText)

openGgtlstBtn := gtk.NewButtonWithLabel("Open Google Translate")
openGgtlstBtn.SetSizeRequest(20, 10)

openGgtlstBtn.Connect("clicked", func() {
url := fmt.Sprintf("https://translate.google.com/?sl=en&tl=vi&text=%s&op=translate", originText)
cmd := exec.Command("google-chrome", url)
err := cmd.Start()
if err != nil {
fmt.Println("Failed to execute command:", err)
}
})
topHbox := gtk.NewHBox(true, 0)
topHbox.PackStart(textview, true, true, 0)
topHbox.SetSizeRequest(400, 200)

// Add the button to a vertical box
vbox := gtk.NewVBox(false, 1)
vbox.PackStart(textview, false, false, 0)
vbox.PackEnd(openGgtlstBtn, true, true, 0)
vbox.PackStart(topHbox, true, true, 0)

// button group
bottomHbox := gtk.NewHBox(true, 1)
bottomHbox.SetSizeRequest(400, 50)

openGgtlstBtn := openTlsBtn(originText)
speakBtn := getSpeakBtn(originText)

bottomHbox.PackStart(openGgtlstBtn, true, true, 0)
bottomHbox.PackStart(speakBtn, true, true, 0)

vbox.PackStart(bottomHbox, false, false, 0)
// vbox.PackEnd(openGgtlstBtn, true, true, 0)
window.Add(vbox)

window.SetSizeRequest(400, 300)
Expand All @@ -60,7 +95,6 @@ func trans(text string) string {
return ""
} else {
res := norm.NFC.String(string(out))
fmt.Println(res)
return string(res)
}
}
Expand Down

0 comments on commit 68f8c05

Please sign in to comment.