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

refactor(argostranslate): use PostForm instead of Post

The newline of message in url.Values in PostForm doesn't need to be
replaced by `\n`
This commit is contained in:
eeeXun 2023-02-04 13:31:56 +08:00
parent 513ad36470
commit 8a141e641e

View File

@ -1,14 +1,13 @@
package argostranslate package argostranslate
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gtt/internal/lock" "gtt/internal/lock"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "net/url"
) )
const ( const (
@ -57,15 +56,12 @@ func (t *ArgosTranslate) Translate(message string) (
err error) { err error) {
var data interface{} var data interface{}
res, err := http.Post(textURL, res, err := http.PostForm(textURL,
"application/json", url.Values{
bytes.NewBuffer([]byte(fmt.Sprintf(`{ "q": {message},
"q": "%s", "source": {langCode[t.srcLang]},
"source": "%s", "target": {langCode[t.dstLang]},
"target": "%s" }`, })
strings.Replace(message, "\n", `\n`, -1),
langCode[t.srcLang],
langCode[t.dstLang]))))
if err != nil { if err != nil {
return "", "", "", err return "", "", "", err
} }