v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-19 01:00:20 -07:00

add mp3 example

This commit is contained in:
eeeXun 2022-10-19 01:14:15 +08:00
parent d3180230c0
commit a69c62fc9f
3 changed files with 32 additions and 2 deletions

View File

@ -18,6 +18,10 @@ https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&
[example](https://gist.github.com/iochen/acad285d2d23066df9449d40a7f3547c) [example](https://gist.github.com/iochen/acad285d2d23066df9449d40a7f3547c)
[play on network](https://github.com/tosone/minimp3#examples-are-here)
[low level](https://github.com/hajimehoshi/oto)
## config ## config
[viper](https://github.com/spf13/viper) [viper](https://github.com/spf13/viper)

View File

@ -10,7 +10,8 @@ import (
) )
const ( const (
API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s" api_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s"
sound_url = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
) )
type Translator struct { type Translator struct {
@ -23,7 +24,7 @@ func (t Translator) Translate(message string) (string, error) {
var translated string var translated string
url_str := fmt.Sprintf( url_str := fmt.Sprintf(
API_URL, api_url,
Lang_Code[t.src_lang], Lang_Code[t.src_lang],
Lang_Code[t.dest_lang], Lang_Code[t.dest_lang],
url.QueryEscape(message), url.QueryEscape(message),

25
ui.go
View File

@ -16,10 +16,35 @@ func ui_init() {
src_box.SetSelectedStyle(tcell.StyleDefault. src_box.SetSelectedStyle(tcell.StyleDefault.
Background(window.src.selected_color). Background(window.src.selected_color).
Foreground(window.src.foreground_color)) Foreground(window.src.foreground_color))
src_box.SetInputCapture(InputHandle)
dest_box.SetBorder(true). dest_box.SetBorder(true).
SetTitle(translator.dest_lang). SetTitle(translator.dest_lang).
SetBorderColor(window.dest.border_color). SetBorderColor(window.dest.border_color).
SetTitleColor(window.dest.border_color). SetTitleColor(window.dest.border_color).
SetBackgroundColor(window.dest.background_color) SetBackgroundColor(window.dest.background_color)
dest_box.SetTextColor(window.dest.foreground_color)
}
func InputHandle(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
switch key {
case tcell.KeyCtrlJ:
result, err := translator.Translate(src_box.GetText())
if err != nil {
dest_box.SetText(err.Error())
} else {
dest_box.SetText(result)
}
case tcell.KeyCtrlQ:
src_box.SetText("", true)
case tcell.KeyCtrlN:
dest_box.SetText("NNN")
case tcell.KeyCtrlP:
dest_box.SetText("PPP")
case tcell.KeyCtrlT:
dest_box.SetText("TTT")
}
return event
} }