Tuesday, June 24, 2008

Modifying Google Translation Button to Open Translation in New Tab/Window

The translation button from Google has proved to be very useful in most situation (by me), especially since the language detection was introduced. However, I feel that there is something lacking. The problem is that the translation generated replaces the current active window/tab while sometimes I prefer to have another window/tab open. The translation button itself is a JavaScript, so we can play with is just a little bit. Here is the original code for translation button (I use the code for translation into English):

javascript:
var t = ((window.getSelection&&window.getSelection()) || (document.getSelection&&document.getSelection()) || (document.selection&&document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t!='') {location.href='http://translate.google.com/translate_t?text=' + t + '&hl = en&langpair=auto|en&tbb=1&ie=' + e;} else {location.href='http://translate.google.com/translate?u=' + escape(location.href) + '&hl = en&langpair=auto|en&tbb=1&ie=' + e;};

In order to make the translation to be opened in a new window/tab, we just have to make a bit change in it:

javascript:
var t = ((window.getSelection&&window.getSelection()) || (document.getSelection&&document.getSelection()) || (document.selection&&document.selection.createRange && document.selection.createRange().text));
var e = (document.charset||document.characterSet);
if (t!='') {var myWindow=window.open('http://translate.google.com/translate_t?text=' + t + '&hl=en&langpair=auto|en&tbb=1&ie='+e,'Translation','')} else
{var myWindow=window.open('http://translate.google.com/translate?u=' + escape(location.href) + '&hl=en&langpair=auto|en&tbb=1&ie=' + e,'Translation','')};

If you hate this kind of stuff, just drag and drop the link below to your browser toolbar:
Translate to English

I hope this is useful. Please let me know if you have a better idea. Obviously, any questions are welcome.

edited: thanks for the suggestion

Related Posts

6 comments:

  1. replace "window.open(..." with "var myWindow=window.open(..." and remove "var a = location.href;" and "location.href = a;"
    so no reopen of current page
    ReplyDelete
  2. I'm responding to an email I received from a French-speaking acquaintance. I've been able to cobble most of it together using Google translation and modifying the formal output to informal, and then feeding it back though just to make sure i didn't screw it up. But there are two pieces that i can't get right.
    ReplyDelete
  3. This is a helpful post. Thanks for sharing it. Looking forward to reading more of your post
    ReplyDelete
  4. does not work for chrome 16
    ReplyDelete
  5. Hi, I really liked your button and I've just found it doesn't work in Firefox 10...could you fix it?
    Thank you again for your work, I really appreciate it.
    ReplyDelete