OwO-Network / DeepLX

Powerful Free DeepL API, No Token Required
https://ssa.sx/deeplx
MIT License
6.68k stars 534 forks source link

Missing Regional Variant handling for languages with variants (e.g. zh-Hans/zh-Hant) #150

Closed yikZero closed 2 weeks ago

yikZero commented 2 weeks ago

The current implementation lacks handling for regional variants, which affects languages like Chinese (zh-Hans/zh-Hant) and Portuguese (pt-BR/pt-PT). This is particularly noticeable with Chinese translations where the distinction between Simplified and Traditional Chinese is not properly maintained.

Current Behavior

When translating to Chinese variants (zh-Hans/zh-Hant), the regional variant information is lost because CommonJobParams.RegionalVariant is not set in the request. The current code only sends:

Lang: Lang{
    SourceLangComputed: strings.ToUpper(sourceLang),
    TargetLang:         strings.ToUpper(targetLang),
}

Expected Behavior

The request should handle regional variants by:

  1. Splitting the target language code (e.g., "zh-Hans" → "zh" + "Hans")
  2. Setting the base language as TargetLang
  3. Setting the full variant code as RegionalVariant

Example implementation like PR #118:

hasRegionalVariant := false
targetLangParts := strings.Split(targetLang, "-")
targetLangCode := targetLangParts[0]
if len(targetLangParts) > 1 {
    hasRegionalVariant = true
}

postData := &PostData{
    Params: Params{
        CommonJobParams: CommonJobParams{
            Mode: "translate",
            RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
        },
        Lang: Lang{
            SourceLangComputed: strings.ToUpper(sourceLang),
            TargetLang:         strings.ToUpper(targetLangCode),
        },
        // ...other params
    },
}

This matches the behavior of DeepL's website, which uses:

Additional Notes

missuo commented 2 weeks ago

This is a temporary restored version. I don't think this version will last long. I will try my best to improve the features implemented before.

missuo commented 2 weeks ago

Done. 9edb997f069eec845cb9aa385290f9fea8894790