-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
417 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
tidy: | ||
go mod tidy | ||
|
||
BinName=anto-v3.4.7-windows.exe | ||
BinName=anto-v3.4.9-windows.exe | ||
|
||
deploy: rs build | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package deepl | ||
|
||
import ( | ||
"anto/domain/service/translator" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct { | ||
*translator.DefaultConfig | ||
ApiKey string `mapstructure:"api_key"` | ||
QPS int `mapstructure:"qps"` | ||
MaxCharNum int `mapstructure:"max_single_text_length"` | ||
MaxCoroutineNum int `mapstructure:"max_coroutine_num"` | ||
} | ||
|
||
func (config *Config) Default() translator.ImplConfig { | ||
return &Config{ | ||
ApiKey: "", | ||
MaxCharNum: 1000, QPS: 10, MaxCoroutineNum: 5, | ||
} | ||
} | ||
|
||
func (config *Config) SyncDisk(currentViper *viper.Viper) error { | ||
tagAndVal := config.JoinAllTagAndValue(API(), config, "mapstructure") | ||
|
||
for tag, val := range tagAndVal { | ||
currentViper.Set(tag, val) | ||
} | ||
return nil | ||
} | ||
|
||
func (config *Config) GetAK() string { return config.ApiKey } | ||
func (config *Config) GetQPS() int { return config.QPS } | ||
func (config *Config) GetMaxCharNum() int { return config.MaxCharNum } | ||
func (config *Config) GetMaxCoroutineNum() int { return config.MaxCoroutineNum } | ||
|
||
func (config *Config) SetAK(str string) error { | ||
if err := config.ValidatorStr(str); err != nil { | ||
return err | ||
} | ||
config.ApiKey = str | ||
return nil | ||
} | ||
|
||
func (config *Config) SetQPS(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.QPS = num | ||
return nil | ||
} | ||
|
||
func (config *Config) SetMaxCharNum(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.MaxCharNum = num | ||
return nil | ||
} | ||
|
||
func (config *Config) SetMaxCoroutineNum(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.MaxCoroutineNum = num | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package deepl | ||
|
||
import ( | ||
"anto/domain/service/translator" | ||
) | ||
|
||
var langSupported = []translator.LangPair{ | ||
{"ZH", "中文"}, | ||
{"EN", "英语"}, | ||
{"DE", "德语"}, | ||
{"ES", "西班牙语"}, | ||
{"FR", "法语"}, | ||
{"IT", "意大利语"}, | ||
{"PL", "波兰语"}, | ||
{"PT", "葡萄牙语"}, | ||
{"RU", "俄语"}, | ||
{"BG", "保加利亚语"}, | ||
{"CS", "捷克语"}, | ||
{"DA", "丹麦语"}, | ||
{"EL", "希腊语"}, | ||
{"EL", "丹麦语"}, | ||
{"ET", "爱沙尼亚语"}, | ||
{"FI", "芬兰语"}, | ||
{"HU", "匈牙利语"}, | ||
{"LT", "立陶宛语"}, | ||
{"LV", "拉脱维亚语"}, | ||
{"RO", "罗马尼亚语"}, | ||
{"SK", "斯洛伐克语"}, | ||
{"SL", "斯洛文尼亚语"}, | ||
{"SV", "瑞典语"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package deepl | ||
|
||
import ( | ||
"anto/domain/service/translator" | ||
"anto/lib/log" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/golang-module/carbon" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
apiTranslator *Translator | ||
onceTranslator sync.Once | ||
) | ||
|
||
const ( | ||
TRANSLATE_API_FREE string = "https://api-free.deepl.com/v2/translate" | ||
TRANSLATE_API_PRO string = "https://api.deepl.com/v2/translate" | ||
) | ||
|
||
func API() *Translator { | ||
onceTranslator.Do(func() { | ||
apiTranslator = New() | ||
}) | ||
return apiTranslator | ||
} | ||
|
||
func New() *Translator { | ||
return &Translator{ | ||
id: "deepl", | ||
name: "DeepL", | ||
sep: "\n", | ||
langSupported: langSupported, | ||
} | ||
} | ||
|
||
type Translator struct { | ||
id string | ||
name string | ||
cfg translator.ImplConfig | ||
langSupported []translator.LangPair | ||
sep string | ||
} | ||
|
||
func (customT *Translator) Init(cfg translator.ImplConfig) { customT.cfg = cfg } | ||
|
||
func (customT *Translator) GetId() string { return customT.id } | ||
func (customT *Translator) GetShortId() string { return "dl" } | ||
func (customT *Translator) GetName() string { return customT.name } | ||
func (customT *Translator) GetCfg() translator.ImplConfig { return customT.cfg } | ||
func (customT *Translator) GetLangSupported() []translator.LangPair { return customT.langSupported } | ||
func (customT *Translator) GetSep() string { return customT.sep } | ||
func (customT *Translator) IsValid() bool { return true } | ||
|
||
func (customT *Translator) Translate(ctx context.Context, args *translator.TranslateArgs) (*translator.TranslateRes, error) { | ||
timeStart := carbon.Now() | ||
texts := strings.Split(args.TextContent, customT.GetSep()) | ||
req := &translateReq{ | ||
Text: texts, SourceLang: args.FromLang, TargetLang: args.ToLang, SplitSentences: "0", | ||
} | ||
|
||
reqBytes, _ := json.Marshal(req) | ||
respBytes, err := translator.RequestSimpleHttp(ctx, customT, TRANSLATE_API_FREE, true, reqBytes, map[string]string{ | ||
"Authorization": fmt.Sprintf("DeepL-Auth-Key %s", customT.cfg.GetAK()), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := new(translateResp) | ||
if err = json.Unmarshal(respBytes, resp); err != nil { | ||
log.Singleton().ErrorF("解析报文异常, 引擎: %s, 错误: %s", customT.GetName(), err) | ||
return nil, fmt.Errorf("解析报文出现异常, 错误: %s", err.Error()) | ||
} | ||
|
||
ret := new(translator.TranslateRes) | ||
|
||
for textIdx, textTarget := range resp.Translations { | ||
ret.Results = append(ret.Results, &translator.TranslateResBlock{ | ||
Id: texts[textIdx], | ||
TextTranslated: textTarget.Text, | ||
}) | ||
} | ||
|
||
ret.TimeUsed = int(carbon.Now().DiffAbsInSeconds(timeStart)) | ||
return ret, nil | ||
} | ||
|
||
type translateReq struct { | ||
Text []string `json:"text"` | ||
SourceLang string `json:"source_lang"` | ||
TargetLang string `json:"target_lang"` | ||
SplitSentences string `json:"split_sentences"` | ||
} | ||
|
||
type translateResp struct { | ||
Translations []struct { | ||
DetectedSourceLanguage string `json:"detected_source_language"` | ||
Text string `json:"text"` | ||
} `json:"translations"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package deepl_pro | ||
|
||
import ( | ||
"anto/domain/service/translator" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct { | ||
*translator.DefaultConfig | ||
ApiKey string `mapstructure:"api_key"` | ||
QPS int `mapstructure:"qps"` | ||
MaxCharNum int `mapstructure:"max_single_text_length"` | ||
MaxCoroutineNum int `mapstructure:"max_coroutine_num"` | ||
} | ||
|
||
func (config *Config) Default() translator.ImplConfig { | ||
return &Config{ | ||
ApiKey: "", | ||
MaxCharNum: 1000, QPS: 10, MaxCoroutineNum: 5, | ||
} | ||
} | ||
|
||
func (config *Config) SyncDisk(currentViper *viper.Viper) error { | ||
tagAndVal := config.JoinAllTagAndValue(API(), config, "mapstructure") | ||
|
||
for tag, val := range tagAndVal { | ||
currentViper.Set(tag, val) | ||
} | ||
return nil | ||
} | ||
|
||
func (config *Config) GetAK() string { return config.ApiKey } | ||
func (config *Config) GetQPS() int { return config.QPS } | ||
func (config *Config) GetMaxCharNum() int { return config.MaxCharNum } | ||
func (config *Config) GetMaxCoroutineNum() int { return config.MaxCoroutineNum } | ||
|
||
func (config *Config) SetAK(str string) error { | ||
if err := config.ValidatorStr(str); err != nil { | ||
return err | ||
} | ||
config.ApiKey = str | ||
return nil | ||
} | ||
|
||
func (config *Config) SetQPS(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.QPS = num | ||
return nil | ||
} | ||
|
||
func (config *Config) SetMaxCharNum(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.MaxCharNum = num | ||
return nil | ||
} | ||
|
||
func (config *Config) SetMaxCoroutineNum(num int) error { | ||
if err := config.ValidatorNum(num); err != nil { | ||
return err | ||
} | ||
config.MaxCoroutineNum = num | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package deepl_pro | ||
|
||
import ( | ||
"anto/domain/service/translator" | ||
) | ||
|
||
var langSupported = []translator.LangPair{ | ||
{"ZH", "中文"}, | ||
{"EN", "英语"}, | ||
{"DE", "德语"}, | ||
{"ES", "西班牙语"}, | ||
{"FR", "法语"}, | ||
{"IT", "意大利语"}, | ||
{"PL", "波兰语"}, | ||
{"PT", "葡萄牙语"}, | ||
{"RU", "俄语"}, | ||
{"BG", "保加利亚语"}, | ||
{"CS", "捷克语"}, | ||
{"DA", "丹麦语"}, | ||
{"EL", "希腊语"}, | ||
{"EL", "丹麦语"}, | ||
{"ET", "爱沙尼亚语"}, | ||
{"FI", "芬兰语"}, | ||
{"HU", "匈牙利语"}, | ||
{"LT", "立陶宛语"}, | ||
{"LV", "拉脱维亚语"}, | ||
{"RO", "罗马尼亚语"}, | ||
{"SK", "斯洛伐克语"}, | ||
{"SL", "斯洛文尼亚语"}, | ||
{"SV", "瑞典语"}, | ||
} |
Oops, something went wrong.