Tuesday, June 7, 2011

Easily Translate Any Website with the Google Translate Bookmarklet

 Capture on 06-06-2011 23-26-23 Capture on 06-06-2011 23-52-51 

I have gotten quite hooked on bookmarklets since I discovered that they work nicely on the WebKit browser in BlackBerry OS 6. The latest one I have been playing with is a Google Translate bookmarklet. I happen to read a few Spanish, Hebrew, and other language sites and find I sometimes need a rough translator in the browser. Google was kind enough to give me the starting point with what they call Google Translation Browser buttons but they sadly did not work for me on the BlackBerry Browser though they do work in Firefox.

Capture on 06-06-2011 23-24-54  Capture on 06-06-2011 23-25-54 

I found I had to edit the code a bit to get it to work properly for me. I created two different bookmarklets for English translation with one that opens it up in a new window and one that replaces the current window with the translation:

The bookmarklet is smart enough to know if you have text highlighted if you only want to translate the highlighted text. If you need help making one of the many other Google Translate languages work let me know. It is relatively painless replacing the main to “location.href” references with “window.open” for a new window or “window.location.replace” for the same window. I also found it worked better if I replaced the equal sign after that method with prentices.

For example Google’s Browser button for Spanish translation of

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/?text='+t+'&hl=en&langpair=auto|es&tbb=1&ie='+e;}else{location.href='http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=en&langpair=auto|es&tbb=1&ie='+e;};

Becomes this for a new window

 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!=''){window.open('http://translate.google.com/?text='+t+'&hl=en&langpair=auto|es&tbb=1&ie='+e);}else{window.open('http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=en&langpair=auto|es&tbb=1&ie='+e);};

or this for the same window

 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!=''){window.location.replace('http://translate.google.com/?text='+t+'&hl=en&langpair=auto|es&tbb=1&ie='+e);}else{window.location.replace('http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=en&langpair=auto|es&tbb=1&ie='+e);};

Once again to add the bookmarklet you need to hover over the link in the BlackBerry Browser and select “Add Link to Bookmarks”. You can then hit the K button or go to your bookmarks whenever you are on a page you wish to translate. Select the bookmarklet and it will translate the page!

More on BerryReview

No comments:

Post a Comment

Comment

Comment