166 lines
3.2 KiB
Go
166 lines
3.2 KiB
Go
package translator
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"go/format"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
"unicode"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
)
|
|
|
|
var trTemplateStr = `
|
|
package translator
|
|
|
|
type TrCode string
|
|
// 翻译对应的标志字符串ID
|
|
const (
|
|
{{- range .}}
|
|
// {{.MessageID}}
|
|
// description: "{{.Desc}}" one: "{{.One}}" other: "{{.Other}}"
|
|
{{.MessageCamelID}} TrCode = "{{.MessageID}}"
|
|
{{end}}
|
|
)
|
|
`
|
|
|
|
// type TrCode string
|
|
|
|
// const (
|
|
// a TrCode = "1"
|
|
// )
|
|
|
|
type TrCodeData struct {
|
|
MessageID string
|
|
MessageCamelID string
|
|
Desc string
|
|
One string
|
|
Other string
|
|
}
|
|
|
|
func TestGenTrCode(t *testing.T) {
|
|
_, currentFile, _, _ := runtime.Caller(0)
|
|
currentDir := filepath.Dir(currentFile)
|
|
|
|
// 解析模板文件
|
|
tfile := filepath.Join(currentDir, "/zh_cn.toml")
|
|
createTrCode(tfile)
|
|
// log.Println(string(buf.Bytes()))
|
|
}
|
|
|
|
func createTrCode(filePath string) {
|
|
tomlFile, err := os.Open(filePath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var tomlMap map[string]any
|
|
err = toml.NewDecoder(tomlFile).Decode(&tomlMap)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var datas []*TrCodeData
|
|
|
|
for key, valuesMap := range tomlMap {
|
|
values := valuesMap.(map[string]any)
|
|
data := &TrCodeData{}
|
|
data.MessageID = key
|
|
data.MessageCamelID = toCamelCase(key)
|
|
data.Desc = values["description"].(string)
|
|
data.One = values["one"].(string)
|
|
data.Other = values["other"].(string)
|
|
datas = append(datas, data)
|
|
}
|
|
|
|
sort.Slice(datas, func(i, j int) bool {
|
|
return datas[i].MessageID < datas[j].MessageID
|
|
})
|
|
|
|
tpl := template.New("translator")
|
|
tpl.Parse(trTemplateStr)
|
|
var buf bytes.Buffer
|
|
err = tpl.Execute(&buf, datas)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
wdata, err := format.Source(buf.Bytes())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = os.WriteFile("./translator_gen.go", wdata, 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type TranslateRequest struct {
|
|
Q string `json:"q"`
|
|
Source string `json:"source"`
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
type TranslateResponse struct {
|
|
TranslatedText string `json:"translatedText"`
|
|
}
|
|
|
|
func TestTrOnline(t *testing.T) {
|
|
requestBody, err := json.Marshal(TranslateRequest{
|
|
Q: "Hello!",
|
|
Source: "en",
|
|
Target: "es",
|
|
})
|
|
if err != nil {
|
|
fmt.Println("Error marshaling request body:", err)
|
|
return
|
|
}
|
|
|
|
resp, err := http.Post("https://libretranslate.com/translate", "application/json", bytes.NewBuffer(requestBody))
|
|
if err != nil {
|
|
fmt.Println("Error sending request:", err)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Println("Error reading response body:", err)
|
|
return
|
|
}
|
|
|
|
var translationResponse TranslateResponse
|
|
err = json.Unmarshal(body, &translationResponse)
|
|
if err != nil {
|
|
fmt.Println("Error unmarshaling response:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(translationResponse.TranslatedText)
|
|
}
|
|
|
|
func toCamelCase(s string) string {
|
|
var sb strings.Builder
|
|
capitalizeNext := true
|
|
for _, r := range s {
|
|
if r == '_' || r == '/' || r == '-' {
|
|
capitalizeNext = true
|
|
} else if capitalizeNext {
|
|
sb.WriteRune(unicode.ToUpper(r))
|
|
capitalizeNext = false
|
|
} else {
|
|
sb.WriteRune(unicode.ToLower(r))
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|