在 JSON 中 returns 是否有 Python 的 API 翻译?

Is there any API for Python that returns translations in JSON?

所以我正在制作一个带有 Kivy, KivyMD, and Python code that translates text from any language to any language. I used the googletrans module in Python for the translations. It's a Python API for translating text. It does exactly what Google Translate does. It searches already translated documents on the internet and finds similar phrases to the phrase the user wants to translate. Then, it returns the results it found. I'm actually thinking it gets the results back in JSON format and then picks the best result and returns it to the user. Because a few days ago when I was using this module, I got a JSONDecodeError. So my question is: Is there any website that returns translations in JSON format so that I can instead just get the translations with the requests 模块的 Android 应用程序?

如此处所示 https://cloud.google.com/translate/docs/basic/translating-text,您可以使用 google 翻译 API 接收 json 格式的结果。您应该创建一个名为 request.json 的文件(通过 python 自动或手动创建),其中包含以下内容,其中 target 是要翻译成的语言,q 是要翻译的短语列表:

{
  "q": ["Hello world", "My name is Jeff"],
  "target": "de"
}

然后您可以 运行 以下终端命令检索 json 文件,然后在 python.

中解析该文件
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://translation.googleapis.com/language/translate/v2

如果你想从 python 运行 这个,你可以使用 subprocess 模块,或者按照网站上的指南从 [=] 运行 这个25=] (https://cloud.google.com/translate/docs/basic/translating-text#translate_translate_text-python)