Saturday, October 15, 2011

Word and text translation bookmarklet

Sometimes (I hope rarer and rarer :)) I need to translate an unknown foreign word on the page. I used to select it, open Google Dictionary Translate, paste it, set source and destination languages if needs and only then press the button to translate it.

Some time later I discovered bookmarklets. I liked the approach and made one for myself.

Drag this link En<>Ru the bookmark panel to be able translate words or text.

To change default source and destination languages put their codes in place of en and ru.

Source code.
(function() {
    var t;
    try {
        t= ((window.getSelection && window.getSelection()) ||
            (document.getSelection && document.getSelection()) ||
            (document.selection &&
            document.selection.createRange &&
            document.selection.createRange().text));
    }
    catch(e){
        t = "";
    }

    if(t && (t=t.toString()) && t.length){
window.open("http://translate.google.com/?sl=en&tl=ru&hl=en&text="+t+"#en|ru|"+t);
    }
})()

I used Javascript Compressor to get compressed version to use it in the bookmarklet.

2 comments:

  1. How do I use this same bookmarklet only for spanish translation into english

    ReplyDelete
  2. You should change link in code to "http://www.google.com/dictionary?aq=f&langpair=es|en&q="+t.
    And don't forget to obfuscate new code for bookmarklet.

    ReplyDelete

Note: Only a member of this blog may post a comment.