ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.
http://py-googletrans.rtfd.io
MIT License
3.87k stars 717 forks source link

Skip translation of specific fields #415

Open 510208 opened 3 weeks ago

510208 commented 3 weeks ago

Googletrans version:

I'm submitting a ...

Current behavior:

I am trying translate some text from English to Chinese(Tradional), below is my code:

def translate(text, dest='zh-tw'):
    if not text:
        return text  # 返回原文本如果是 None 或空字符串
    translator = Translator()
    result = translator.translate(text, dest).text
    print(f"Translated '{text}' to '{result}'")
    return result

I tried translate a text that contained a Placeholder like this:

%username% joined the server

and it responsed this text:

___%用戶名%__加入服務器

(If you want to know why the placeholder have some underline with the placeholder? Because I find this post on StackOverflow: How to tell Googletrans to ignore certain parts?)

Expected behavior:

It won't translate placeholder, like below:

%username%加入服務器

Steps to reproduce:

Below is my code, but I think it won't help... Translator.py: https://gist.github.com/510208/a60f2752872eabe11e8552e94d278e45 Checkformat.py: https://gist.github.com/510208/8b07f6a939bb8fa970cefdc9e86e8077

All right, the annotation is Chinese, maybe you need translate it or ignore it.

Related code:

Haeherfeder commented 3 weeks ago

tried the other comment? replace it with 1 before translating, and replace it back later. or just look for the ___% and the %after that, and replace it back with the content you wanted