You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用google_trans_new做语言检测与翻译出现JSONDecodeError问题求助

问题原因

你遇到的JSONDecodeError报错是因为google_trans_new库已停止维护,谷歌翻译的官方接口返回结构和参数规则早已更新,旧版库的解析逻辑无法适配新的接口返回内容,所以语言检测和翻译功能都会触发异常。

解决方案

方案1:更换维护状态正常的翻译库(推荐)

推荐使用deep-translator库,该库持续适配最新的谷歌翻译接口,稳定性更高。

  • 安装依赖:
pip install deep-translator langdetect
  • 语言检测代码示例:
from langdetect import detect

detect_result = detect('小')
print(detect_result)
  • 翻译代码示例:
from deep_translator import GoogleTranslator

sentence = "アフィリエイト代理店 打合せ 商談"
translate_text = GoogleTranslator(source='auto', target='en').translate(sentence)
print(translate_text)

方案2:修改现有google_trans_new库源码

如果要继续使用原库,可手动修改源码适配新接口:

  1. 找到本地Python环境下site-packages/google_trans_new/google_trans_new.py文件
  2. 将文件中url_params = {'client': 'webapp'}的webapp修改为gtx
  3. 找到返回结果解析部分的正则匹配逻辑,删除对多余返回字段的校验,即可解决JSON解析错误

内容的提问来源于stack exchange,提问作者Sid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 06:15:04