mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a43aba64c1 | ||
|
|
3acec2c252 | ||
|
|
10f33401e7 | ||
|
|
1fa6d7a2e3 |
+18
-114
@@ -23,7 +23,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -105,108 +104,26 @@ func newInstanceID() string {
|
|||||||
return fmt.Sprintf("%s-%s-%s-%s-%s", s[0:8], s[8:12], s[12:16], s[16:20], s[20:32])
|
return fmt.Sprintf("%s-%s-%s-%s-%s", s[0:8], s[8:12], s[12:16], s[16:20], s[20:32])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Language code tables mirror the bundled list in the extension's
|
// langCodeToOneshot translates DeepL's uppercase codes (DE, EN, ZH, ...)
|
||||||
// background.js (arrays `y` ~offset 6000 for the full target-capable
|
// to the lowercase BCP-47-ish codes the oneshot endpoint requires (de,
|
||||||
// set, `A` for source-only aliases). Keys are the uppercase forms
|
// en-US, zh-Hans, ...). Unknown codes fall through lowercased.
|
||||||
// callers pass; values are the lowercase BCP-47-ish forms the oneshot
|
var langCodeToOneshot = map[string]string{
|
||||||
// endpoint expects ("de", "en-US", "zh-Hans", ...).
|
|
||||||
//
|
|
||||||
// targetLangMap is what the API accepts as `target_lang`. EN and PT
|
|
||||||
// are intentionally absent — DeepL deprecated them as target codes in
|
|
||||||
// favour of EN-US/EN-GB and PT-BR/PT-PT, and the extension's y array
|
|
||||||
// reflects that. We accept EN/PT as a backward-compat convenience and
|
|
||||||
// resolve them to the regional default (en-US, pt-BR).
|
|
||||||
var targetLangMap = map[string]string{
|
|
||||||
"AR": "ar", "BG": "bg", "CS": "cs", "DA": "da", "DE": "de", "EL": "el",
|
"AR": "ar", "BG": "bg", "CS": "cs", "DA": "da", "DE": "de", "EL": "el",
|
||||||
"EN-GB": "en-GB", "EN-US": "en-US",
|
"EN": "en-US", "EN-GB": "en-GB", "EN-US": "en-US",
|
||||||
"ES": "es", "ES-419": "es-419", "ET": "et", "FI": "fi", "FR": "fr",
|
"ES": "es", "ET": "et", "FI": "fi", "FR": "fr", "HU": "hu",
|
||||||
"HE": "he", "HU": "hu", "ID": "id", "IT": "it", "JA": "ja", "KO": "ko",
|
"ID": "id", "IT": "it", "JA": "ja", "KO": "ko", "LT": "lt", "LV": "lv",
|
||||||
"LT": "lt", "LV": "lv", "NB": "nb", "NL": "nl", "PL": "pl",
|
"NB": "nb", "NL": "nl", "PL": "pl",
|
||||||
"PT-BR": "pt-BR", "PT-PT": "pt-PT",
|
"PT": "pt-BR", "PT-BR": "pt-BR", "PT-PT": "pt-PT",
|
||||||
"RO": "ro", "RU": "ru", "SK": "sk", "SL": "sl", "SV": "sv",
|
"RO": "ro", "RU": "ru", "SK": "sk", "SL": "sl", "SV": "sv",
|
||||||
"TR": "tr", "UK": "uk", "VI": "vi",
|
"TR": "tr", "UK": "uk",
|
||||||
"ZH": "zh-Hans", "ZH-HANS": "zh-Hans", "ZH-HANT": "zh-Hant",
|
"ZH": "zh-Hans", "ZH-HANS": "zh-Hans", "ZH-HANT": "zh-Hant",
|
||||||
// Convenience aliases for legacy callers.
|
|
||||||
"EN": "en-US",
|
|
||||||
"PT": "pt-BR",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sourceLangMap is what the API accepts as `source_lang`. It is a
|
func toOneshotLang(code string) string {
|
||||||
// superset of targetLangMap: EN and PT are first-class source codes
|
if v, ok := langCodeToOneshot[strings.ToUpper(code)]; ok {
|
||||||
// (extension array `A`) mapping to the generic "en"/"pt" — used when
|
return v
|
||||||
// the caller knows the input is English/Portuguese but does not want
|
|
||||||
// to commit to a regional variant.
|
|
||||||
var sourceLangMap = func() map[string]string {
|
|
||||||
m := make(map[string]string, len(targetLangMap)+2)
|
|
||||||
for k, v := range targetLangMap {
|
|
||||||
m[k] = v
|
|
||||||
}
|
}
|
||||||
m["EN"] = "en"
|
return strings.ToLower(code)
|
||||||
m["PT"] = "pt"
|
|
||||||
return m
|
|
||||||
}()
|
|
||||||
|
|
||||||
// resolveTargetLang validates and normalizes a user-supplied target
|
|
||||||
// language code. Returns "" and a non-nil error if the code is empty,
|
|
||||||
// "auto", or otherwise not in the supported set.
|
|
||||||
func resolveTargetLang(code string) (string, error) {
|
|
||||||
if code == "" {
|
|
||||||
return "", fmt.Errorf("target_lang is required")
|
|
||||||
}
|
|
||||||
if strings.EqualFold(code, "auto") {
|
|
||||||
return "", fmt.Errorf("target_lang cannot be \"auto\"; pick one of: %s", supportedTargetLangsList())
|
|
||||||
}
|
|
||||||
if v, ok := targetLangMap[strings.ToUpper(code)]; ok {
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("unsupported target_lang %q; valid codes: %s", code, supportedTargetLangsList())
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolveSourceLang validates and normalizes a user-supplied source
|
|
||||||
// language code. An empty string or "auto" is allowed and returns
|
|
||||||
// ("", nil) so the caller omits source_lang and lets the server
|
|
||||||
// autodetect.
|
|
||||||
func resolveSourceLang(code string) (string, error) {
|
|
||||||
if code == "" || strings.EqualFold(code, "auto") {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
if v, ok := sourceLangMap[strings.ToUpper(code)]; ok {
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("unsupported source_lang %q; valid codes: %s (or \"auto\")", code, supportedSourceLangsList())
|
|
||||||
}
|
|
||||||
|
|
||||||
// supportedTargetLangsList / supportedSourceLangsList return a sorted,
|
|
||||||
// comma-separated rendering of the supported codes for use in error
|
|
||||||
// messages. Cached at first call.
|
|
||||||
var (
|
|
||||||
targetLangsListOnce sync.Once
|
|
||||||
targetLangsList string
|
|
||||||
sourceLangsListOnce sync.Once
|
|
||||||
sourceLangsList string
|
|
||||||
)
|
|
||||||
|
|
||||||
func supportedTargetLangsList() string {
|
|
||||||
targetLangsListOnce.Do(func() {
|
|
||||||
targetLangsList = sortedKeys(targetLangMap)
|
|
||||||
})
|
|
||||||
return targetLangsList
|
|
||||||
}
|
|
||||||
|
|
||||||
func supportedSourceLangsList() string {
|
|
||||||
sourceLangsListOnce.Do(func() {
|
|
||||||
sourceLangsList = sortedKeys(sourceLangMap)
|
|
||||||
})
|
|
||||||
return sourceLangsList
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedKeys(m map[string]string) string {
|
|
||||||
keys := make([]string, 0, len(m))
|
|
||||||
for k := range m {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
return strings.Join(keys, ", ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// appInformation matches the snake_case shape produced by background.js
|
// appInformation matches the snake_case shape produced by background.js
|
||||||
@@ -334,25 +251,9 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
resolvedTarget, err := resolveTargetLang(targetLang)
|
|
||||||
if err != nil {
|
|
||||||
return DeepLXTranslationResult{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
Message: err.Error(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
resolvedSource, err := resolveSourceLang(sourceLang)
|
|
||||||
if err != nil {
|
|
||||||
return DeepLXTranslationResult{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
Message: err.Error(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
reqStruct := oneshotRequest{
|
reqStruct := oneshotRequest{
|
||||||
Text: []string{text},
|
Text: []string{text},
|
||||||
TargetLang: resolvedTarget,
|
TargetLang: toOneshotLang(targetLang),
|
||||||
SourceLang: resolvedSource, // empty = autodetect; omitempty drops the field
|
|
||||||
UsageType: "Translate",
|
UsageType: "Translate",
|
||||||
AppInformation: appInformation{
|
AppInformation: appInformation{
|
||||||
OS: "brex_macOS",
|
OS: "brex_macOS",
|
||||||
@@ -362,6 +263,9 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if sourceLang != "" && !strings.EqualFold(sourceLang, "auto") {
|
||||||
|
reqStruct.SourceLang = toOneshotLang(sourceLang)
|
||||||
|
}
|
||||||
bodyBytes, _ := json.Marshal(reqStruct)
|
bodyBytes, _ := json.Marshal(reqStruct)
|
||||||
|
|
||||||
endpoint := oneshotFreeEndpoint
|
endpoint := oneshotFreeEndpoint
|
||||||
|
|||||||
Reference in New Issue
Block a user