A simple bookmarklet to translate text on webpages.
Use the following snippet:
javascript:(async function () {
try {
const sel = window.getSelection();
if (!sel || sel.rangeCount === 0) {
alert("Please select some text to translate.");
return;
}
const tl = prompt("Translate to which language? (e.g., fr, es, en)", "en");
if (!tl) return;
for (let i = 0; i < sel.rangeCount; i++) {
const range = sel.getRangeAt(i);
const text = range.toString().trim();
if (!text) continue;
const res = await fetch(
`https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${encodeURIComponent(tl)}&dt=t&q=${encodeURIComponent(text)}`
).then(r => r.json());
const translated = res[0].map(seg => seg[0]).join("");
range.deleteContents();
range.insertNode(document.createTextNode(translated));
}
} catch (err) {
console.error("Translation error:", err);
alert("There was an error translating the text.");
}
})();Or, drag this link to your bookmarks bar.
- Select the text you want to translate.
- Run the bookmarklet, which will send the selected text through a Google Translate API for translation
- Text will be replaced with translated version (may take some time).