Anki语言卡片Google/DeepL翻译链接适配Ankidroid问题求助
解决方案
一、强制在浏览器打开Google翻译链接(绕过APP唤起)
如果不想依赖Google翻译APP,直接强制Ankidroid用默认浏览器打开链接,确保文本能正常传递,只需在卡片模板中用JavaScript的window.open替代普通a标签:
<button onclick="window.open('https://translate.google.com/?sl=en&text={{text:Sentence}}', '_blank')">Google 翻译</button>
- 原理:
window.open会强制调用系统浏览器打开链接,避免Ankidroid自动唤起Google翻译APP,网页版能正常读取text参数内容,且桌面端也可正常使用。
二、让Google翻译APP正确带入待翻译文本
使用Google翻译APP的自定义URL Scheme,替代网页版链接,确保APP能识别并加载待翻译文本:
<script> function openGoogleTranslate() { // 对句子进行URL编码,避免特殊字符导致参数失效 const encodedText = encodeURIComponent("{{text:Sentence}}"); // APP专属链接,tl=auto会自动匹配用户上次使用的目标语言或设备语言 const appLink = `googletranslate://?sl=en&tl=auto&text=${encodedText}`; // 备用网页链接 const webLink = `https://translate.google.com/?sl=en&text=${encodedText}`; // 先尝试唤起APP window.location.href = appLink; // 1秒后未唤起则跳转网页 setTimeout(() => window.open(webLink, '_blank'), 1000); } </script> <button onclick="openGoogleTranslate()">Google 翻译</button>
- 说明:
googletranslate://是Google翻译APP的专属协议,tl=auto会自动适配用户常用目标语言;同时加入网页 fallback,避免用户未安装APP时无法使用。
三、让DeepL自动适配设备默认语言
通过JavaScript获取设备的默认语言,动态拼接DeepL的翻译链接,替代固定目标语言的设置:
<script> function openDeepLTranslate() { const encodedText = encodeURIComponent("{{text:Sentence}}"); // 获取设备默认语言(如zh-CN,截取前两位得到zh) const targetLang = navigator.language.split('-')[0]; // 拼接DeepL链接,自动使用设备语言作为目标语言 const deepLLink = `https://www.deepl.com/translator#en/${targetLang}/${encodedText}`; window.open(deepLLink, '_blank'); } </script> <button onclick="openDeepLTranslate()">DeepL 翻译</button>
- 原理:
navigator.language会读取设备系统语言,截取前两位(如zh-CN取zh)作为DeepL的目标语言参数,若DeepL不支持该语言,会自动 fallback 到相近的支持语言。
内容的提问来源于stack exchange,提问作者zozobra
相关产品推荐
相关产品推荐

