Tuesday, April 15, 2008

Create a Google Translator Bookmark in Firefox

I wanted a way to quickly translate any web page in any language to English with a single click of a button. I use Firefox as my main browser, but I'd assume you can do this in IE and other browsers also.

  1. Right-click on the Firefox Bookmarks Pane and choose "New bookmark".
  2. In the Name field, fill in: Translate.
  3. In the Location field fill in:
    javascript:window.location='http://www.google.com/translate_c?u='+window.location
    

Now when ever you're on a page that isn't in English, just click on the "Translate" bookmark and the page will be translated for you.

How it works is that in the Location field you can place JavaScript using the URL pseudo JavaScript protocol. A URL has about 5 main parts:

http://www.example.com/path/?query=abc&query2=xyz#bookmark
  1. http:// - This is the Protocol
  2. www.example.com - The domain
  3. /path/ - The path to the page on the domain
  4. ?query=abc&query2=xyz - The query (an associative list of parameters and values)
  5. #bookmark - The bookmark (a link to a part of a page)
If you were to replace the Protocol portion of the URL with javascript: then the browser will interpret everything after the protocol portion as JavaScript code to execute.

The JavaScript we execute is:

window.location='http://www.google.com/translate_c?u='+window.location
The JavaScript executes in the scope of the current document, so window.location will refer to the current URL. (or window.location.href if you want to be specific, window.location probably has a toString() method that returns this.href).

So when clicked the bookmark executes the JavaScript code that instructs the window to go to the Google Translate page and appends teh current URL as the parameter value for u, which is the URL to translate.

I did a search and found this Extension for Firefox also: https://addons.mozilla.org/en-US/firefox/addon/918. It allows you to translate the selected text inside a web page with Google Translator. We could also do the same with our bookmark method, but since there is an extension for it, lets just leave it at that. Enjoy.

1 comment:

Wangbu said...

Hello! I am a blog fan from the Philippines. I just happen to discover your site. It is interesting. I find it worth visiting.