mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-18 08:40:35 -07:00
feat: add definition
This commit is contained in:
parent
b994a3d872
commit
16a9c3a676
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
textURL = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dt=bd&sl=%s&tl=%s&q=%s"
|
textURL = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dt=bd&dt=md&dt=ex&sl=%s&tl=%s&q=%s"
|
||||||
soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
|
soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +30,11 @@ func NewTranslator() *Translator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Translator) Translate(message string) (translation string, partOfSpeech string, err error) {
|
func (t *Translator) Translate(message string) (
|
||||||
|
translation string,
|
||||||
|
definition string,
|
||||||
|
partOfSpeech string,
|
||||||
|
err error) {
|
||||||
var data []interface{}
|
var data []interface{}
|
||||||
|
|
||||||
urlStr := fmt.Sprintf(
|
urlStr := fmt.Sprintf(
|
||||||
@ -41,25 +45,25 @@ func (t *Translator) Translate(message string) (translation string, partOfSpeech
|
|||||||
)
|
)
|
||||||
res, err := http.Get(urlStr)
|
res, err := http.Get(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = json.Unmarshal(body, &data); err != nil {
|
if err = json.Unmarshal(body, &data); err != nil {
|
||||||
return "", "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
// translation
|
// translation = data[0]
|
||||||
for _, lines := range data[0].([]interface{}) {
|
for _, lines := range data[0].([]interface{}) {
|
||||||
translatedLine := lines.([]interface{})[0]
|
translatedLine := lines.([]interface{})[0]
|
||||||
translation += fmt.Sprintf("%v", translatedLine)
|
translation += fmt.Sprintf("%v", translatedLine)
|
||||||
}
|
}
|
||||||
|
|
||||||
// part of speech
|
// part of speech = data[1]
|
||||||
if data[1] != nil {
|
if data[1] != nil {
|
||||||
for _, parts := range data[1].([]interface{}) {
|
for _, parts := range data[1].([]interface{}) {
|
||||||
// part of speech
|
// part of speech
|
||||||
@ -80,13 +84,26 @@ func (t *Translator) Translate(message string) (translation string, partOfSpeech
|
|||||||
}
|
}
|
||||||
partOfSpeech += "\n"
|
partOfSpeech += "\n"
|
||||||
}
|
}
|
||||||
partOfSpeech += "\n"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return translation, partOfSpeech, nil
|
|
||||||
|
// definition = data[12]
|
||||||
|
if len(data) >= 13 && data[12] != nil {
|
||||||
|
for _, parts := range data[12].([]interface{}) {
|
||||||
|
definition += fmt.Sprintf("[%v]\n", parts.([]interface{})[0])
|
||||||
|
for _, sentences := range parts.([]interface{})[1].([]interface{}) {
|
||||||
|
definition += fmt.Sprintf("\t- %v\n", sentences.([]interface{})[0])
|
||||||
|
// Get example sentence
|
||||||
|
if len(sentences.([]interface{})) >= 3 && sentences.([]interface{})[2] != nil {
|
||||||
|
definition += fmt.Sprintf("\t\t- \"%v\"\n", sentences.([]interface{})[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return translation, definition, partOfSpeech, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", "", errors.New("Translation not found")
|
return "", "", "", errors.New("Translation not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Translator) PlaySound(lang string, message string) error {
|
func (t *Translator) PlaySound(lang string, message string) error {
|
||||||
|
33
main.go
33
main.go
@ -11,20 +11,25 @@ var (
|
|||||||
// Translate
|
// Translate
|
||||||
translator = translate.NewTranslator()
|
translator = translate.NewTranslator()
|
||||||
// UI
|
// UI
|
||||||
app = tview.NewApplication()
|
app = tview.NewApplication()
|
||||||
srcInput = tview.NewTextArea()
|
srcInput = tview.NewTextArea()
|
||||||
dstOutput = tview.NewTextView()
|
dstOutput = tview.NewTextView()
|
||||||
defOutput = tview.NewTextArea()
|
defOutput = tview.NewTextArea()
|
||||||
posOutput = tview.NewTextArea()
|
posOutput = tview.NewTextArea()
|
||||||
srcLangDropDown = tview.NewDropDown()
|
srcLangDropDown = tview.NewDropDown()
|
||||||
dstLangDropDown = tview.NewDropDown()
|
dstLangDropDown = tview.NewDropDown()
|
||||||
langCycle = ui.NewUICycle(srcLangDropDown, dstLangDropDown)
|
langCycle = ui.NewUICycle(srcLangDropDown, dstLangDropDown)
|
||||||
themeDropDown = tview.NewDropDown()
|
themeDropDown = tview.NewDropDown()
|
||||||
transparentDropDown = tview.NewDropDown()
|
transparentDropDown = tview.NewDropDown()
|
||||||
hideBelowDropDown = tview.NewDropDown()
|
hideBelowDropDown = tview.NewDropDown()
|
||||||
srcBorderDropDown = tview.NewDropDown()
|
srcBorderDropDown = tview.NewDropDown()
|
||||||
dstBorderDropDown = tview.NewDropDown()
|
dstBorderDropDown = tview.NewDropDown()
|
||||||
styleCycle = ui.NewUICycle(themeDropDown, transparentDropDown, hideBelowDropDown, srcBorderDropDown, dstBorderDropDown)
|
styleCycle = ui.NewUICycle(
|
||||||
|
themeDropDown,
|
||||||
|
transparentDropDown,
|
||||||
|
hideBelowDropDown,
|
||||||
|
srcBorderDropDown,
|
||||||
|
dstBorderDropDown)
|
||||||
keyMapMenu = tview.NewTextView()
|
keyMapMenu = tview.NewTextView()
|
||||||
langButton = tview.NewButton("(1)Language")
|
langButton = tview.NewButton("(1)Language")
|
||||||
styleButton = tview.NewButton("(2)Style")
|
styleButton = tview.NewButton("(2)Style")
|
||||||
|
3
ui.go
3
ui.go
@ -399,11 +399,12 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
message := srcInput.GetText()
|
message := srcInput.GetText()
|
||||||
// Only translate when message exist
|
// Only translate when message exist
|
||||||
if len(message) > 0 {
|
if len(message) > 0 {
|
||||||
translation, partOfSpeech, err := translator.Translate(message)
|
translation, definition, partOfSpeech, err := translator.Translate(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dstOutput.SetText(err.Error())
|
dstOutput.SetText(err.Error())
|
||||||
} else {
|
} else {
|
||||||
dstOutput.SetText(translation)
|
dstOutput.SetText(translation)
|
||||||
|
defOutput.SetText(definition, false)
|
||||||
posOutput.SetText(partOfSpeech, false)
|
posOutput.SetText(partOfSpeech, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user