Skip to content

字幕与翻译 ​

把文本送进字幕链路,或直接调用当前活跃的翻译引擎。


feed_direct_caption — 只要原文 ​

python
host.feed_direct_caption(
    text: str,
    lang: str = "auto",
    *,
    tts: bool = False,
    drop_noise: bool = False,
) -> bool
参数说明
text原文;空串返回 False
lang源语言,默认 auto
tts是否朗读译文;工具类默认 False
drop_noise是否丢弃 ok / you 之类网页噪声;工具类必须 False

行为:

  • 同传在跑:走完整字幕 / 历史 / TTS 策略。
  • 否则:轻量路径(MT + 字幕 + 历史),不拉 ASR、不开声卡。
  • 若用户把「原文」「译文」都关了,会自动打开显示(避免"翻译了但看不见")。

兼容别名:feedDirectCaption(text, lang) ≡ feed_direct_caption(text, lang, tts=False, drop_noise=False)。


feed_caption — 已有译文 ​

python
host.feed_caption(
    text: str,
    translation: str | None = None,
    lang: str = "auto",
    source: str = "plugin",
    *,
    tts: bool = False,
    drop_noise: bool = False,
) -> bool

translation 非空时跳过 MT,直接上双语字幕。用于:划词已有译文、字幕桥补帧等。

source 写入历史记录(默认 "plugin";内部轻量路径用 "select",浏览器字幕用 "web")。


translate / translate_batch ​

python
host.translate(text: str, source: str = "auto", target: str = "zh",
               glossary_context: str | None = None) -> str

host.translate_batch(texts: list[str], source: str = "auto", target: str = "zh",
                     *, glossary_context: str | None = None) -> list[str]
  • 走当前活跃的 BaseMTEngineProvider.translate。
  • 失败返回空串,不抛异常(translate_batch 里失败条目同样是空串)。
  • glossary_context 会透传给当前活跃 MT(见 mt)。

该用哪一条 ​

情况用
只有原文,想让宿主全套处理(含朗读)feed_direct_caption(..., tts=True)
只有原文,不要朗读feed_direct_caption(...)
我已经有译文了(划词 / 外部同步字幕)feed_caption(text, translation, source=…)
只想悄悄翻译一段文本,不想上字幕translate()
需要「翻译 + 字幕 + 朗读」一体feed_direct_caption(..., tts=True) —— 不要 speak() + feed() 拼

相关 ​

TransBox - 赋能全人类无界限跨语言交流