请教关于eaf-browser沉浸式翻译功能的问题

万分抱歉,刚才不小心把编辑到一半的草稿发了出来,请大佬忽略,谢谢!

正文:

环境

  • Centaur Emacs 30.1
  • Kde Plasma 6.12.0
  • Manjaro Linux

现象

当我使用eaf-browser访问英文网页(示例网页)的时候,我使用了M-i调用eaf浏览器的沉浸式翻译(eaf-py-proxy-immersive_translation),过了一会,网页黑屏了(Emacs其他部分没有问题),eaf崩溃,*eaf*报错如下:

setHighDpiScaleFactorRoundingPolicy must be called before creating the QGuiApplication instance
[EAF] Browser https://github.com/alex47/KDE-Rounded-Corners loading time: 4.872657299041748s
Traceback (most recent call last):
  File "/home/doing/.emacs.d/site-lisp/emacs-application-framework/core/webengine.py", line 1908, in run
    translation = json.loads(''.join(list(map(lambda b: b.decode("utf-8"), result))))["translated-text"]
                  ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/usr/lib/python3.13/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/json/decoder.py", line 363, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process *eaf* 已中止 (core dumped)

排查

经输出中间文件并查看,定位到是core/webengine.py的1905行,crow在翻译的时候会调用https://mozhi.aryak.me/api/translate?engine=google&from=auto&to=zh-CN&text=你的文本 ,而由于Web请求的限制,一次发送太多内容会导致请求失败

问题

请问我该如何解决这种问题呢?万分感谢🙏,万分抱歉😢

路过,抱歉并没用过 eaf ,不过看你那个排查结果大概是边界情况没考虑到,导致异常崩溃

  • 有没有可能修改翻译地址为 google 官方?
  • 或者修改原 python 脚本 webengine.py:1908 异常判断方式?

感谢回复,但是实践起来似乎有一点问题。。。

我尝试了去curl google的API,但是由于这个文本太长了,直接GET请求好像都不行,似乎需要用POST请求?

我试了,但是这个沉浸式翻译是eaf把crow当API使,所以如果crow返回不了结果,那么eaf异常判断也得不到结果

我目前的解决方案是用eaf-py-proxy-insert_or_translate_page代替,就是自动跳转到google的网页翻译,除了不能中英对照倒也没有大问题

eaf-browser 的沉浸式翻译代码太初级, 建议移植chrome插件到EAF中。

感谢大佬回复。 从尽量减少修改量的角度来看,我认为移植Chrome插件(而且目前效果最好的Chrome插件“沉浸式翻译”还不开源)的工作量不小。

现在我这里有两个想法:

  1. 寻找一个支持POST接口访问的翻译API(GET请求由于网址访问原理上的限制,很难承受大量文本)
  2. 将接口请求量和单次调用的文本量做折中

感觉第一种好像更完美一些,请大佬多指正

找到了一个类似于crow的东西,比POST还方便一些:translate-shell

trans -t 'zh' -j -e 'google' -i -b /path/to/file 

不能输出为JSON,不过经过处理应该也能用,部分截图:

1 个赞

最近时间都在公司卖货,写代码的时间很少,欢迎补丁。

查了下 eaf 和 crow-translate 的相关代码, eaf 里是通过 hardcode 方式调用 crow 查询 google 翻译[^1]来获取的结果,并且这个查询方式就是用的 get [^2]

同时查看 crow-translate 的代码逻辑,yandex/bing 均是 POST 请求, 那有可能支持你的 “POST” 部分的需求, 需要把 webengine.py:1905 部分代码改为 bing 或 yandex

另外, QOnlineTranslator::requestGoogleTranslat [^2] 代码里做了异常检测, 居然还是会报错, 它代码逻辑没覆盖边缘异常?

[^1] EAF 调用 Crow 查询 google 翻译: emacs-application-framework/core/webengine.py at cf62c2dc4b7064c687a9ae134159d83d33b67345 · emacs-eaf/emacs-application-framework · GitHub

[^2] QOnlineTranslator::requestGoogleTranslat: QOnlineTranslator/src/qonlinetranslator.cpp at 8541f9b78cbffa15f2aa3412fe1314ea37c7805e · crow-translate/QOnlineTranslator · GitHub

1 个赞

谢谢回复,我刚才试了,改成bing或yandex仍然会出问题。

目前我尝试了:

这个可以用,但是在大部分网页中中英内容会错位,只有在结构特别简单的网页比如 example.org 才不会错位。

刚刚安装了 eaf 试了下, crow 不能在 macOS 上使用, 因此 eaf 的默认方案不通用

其次 translate-shell 在我的机器上使用起来也很慢, 不知道什么原因。。而且确实不能输出 json 格式,可能要做处理才能集成进来