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

refactor: check Apertium translate with responseData instead of StatusCode

This commit is contained in:
eeeXun 2023-04-29 21:18:09 +08:00
parent ecdebc39ad
commit 24cadc4072

View File

@ -58,21 +58,18 @@ func (t *Translator) Translate(message string) (translation *core.Translation, e
if len(data) <= 0 { if len(data) <= 0 {
return nil, errors.New("Translation not found") return nil, errors.New("Translation not found")
} }
// If responseData is nil, then suppose the translation pair is not available
switch res.StatusCode { if data["responseData"] == nil {
case 200: return nil, errors.New(fmt.Sprintf("%s does not support translate from %s to %s.",
translation.TEXT = fmt.Sprintf("%v", t.GetEngineName(),
data["responseData"].(map[string]interface{})["translatedText"]) t.GetSrcLang(),
default: t.GetDstLang(),
return nil, errors.New( ))
fmt.Sprintf("%s does not support translate from %s to %s.\nSee available pair on %s",
t.GetEngineName(),
t.GetSrcLang(),
t.GetDstLang(),
"https://www.apertium.org/",
))
} }
translation.TEXT = fmt.Sprintf("%v",
data["responseData"].(map[string]interface{})["translatedText"])
return translation, nil return translation, nil
} }