UlionTse / translators

🌏🌍🌎Translators🌎🌍🌏 is a library that aims to bring free, multiple, enjoyable translations to individuals and students in Python. Translators是一个旨在用Python为个人和学生带来免费、多样、愉快翻译的库。
https://pypi.org/project/translators/
GNU General Public License v3.0
1.62k stars 189 forks source link

[Bug]: 当用sogou批量翻译时返回一些奇怪的随机数,要怎么捕获处理,或者有没有建议的请求频率 #130

Closed firmianay closed 1 year ago

firmianay commented 1 year ago

Debug Tips

What happened?

A bug happened!

APP Version

latest

Python Version

3.10

Runtime Environment

Linux Ubuntu

Country/Region

China

Relevant log output

No response

Screenshots

![DESCRIPTION](LINK.png)

Code of Conduct

UlionTse commented 1 year ago

Hello friend! You can catch exceptions by getting the complete response data by using the parameter is_detail_result=True.

firmianay commented 1 year ago

我包装了一下,现在还行,中文不是ascii:

def translate(text: str):
    if text:
        with contextlib.suppress(Exception):
            result = translators.translate_text(text, translator='sogou')

        if not result.isascii():
            return result

    return ''
UlionTse commented 1 year ago

Okay. According to your written, I wrote it also, hope to help you.

def translate(query_text):
    result = translators.translate_text(query_text, if_ignore_empty_query=True)
    return result.encode(encoding).decode('utf-8')  # encoding depends on your system.