Skip to content

Instantly share code, notes, and snippets.

@samthecodingman
Last active October 25, 2018 10:37
Show Gist options
  • Save samthecodingman/eadd8f35e962e262ea5e to your computer and use it in GitHub Desktop.
Save samthecodingman/eadd8f35e962e262ea5e to your computer and use it in GitHub Desktop.
Reload the current page in Google Translate (English) [Javascript Bookmarklet] [CC-BY License]
javascript:(window.location.href.indexOf("translate.google.com")>-1)?((new%20RegExp('[?&]u=([^&]*)')).exec(location.search)!=null)?window.location.assign("https://translate.google.com/translate?hl=en&sl=auto&tl=en&sandbox=1&u="+(new%20RegExp('[?&]u=([^&]*)')).exec(location.search)[1]):alert("Error:%20This%20google%20translate%20page%20does%20not%20have%20a%20url%20parameter."):window.location.assign("https://translate.google.com/translate?hl=en&sl=auto&tl=en&sandbox=1&u="+encodeURIComponent(window.location.href));
@samthecodingman
Copy link
Author

Google Translate Bookmarklet

Author Note: The red syntax highlighting is an unintended bug of GitHub.

How to use

To use this bookmarklet, add it as a Bookmark to your favourites bar of your browser under the 'URL' or 'Location' field. When clicked, the Javascript function will run and reload the page in English using Google Translate.

Why use a bookmarklet?

Well, in comparison to a full plugin, a bookmarklet consists of a small amount of text that just performs the function you need. It may not be as pretty as a plugin, but it doesn't require updating, doesn't have version incompatibilities and most importantly won't actually read any of the page's contents. So there is zero snooping/logging/tracking risk.

Configuration

To use for languages other than English, change the target language code in the google URLs from en (at ...&tl=en&...) to something else such as de for German (e.g. ...&tl=de&...). The source language code can also be changed from auto to some other language (by changing ...&sl=auto&...).

How it works

On the surface...
The bookmarklet is actually quite primitive and simplistic, consisting of some simple error checking using the ternary operator:

statement ? doIfTrue() : doIfFalse();

To illustrate what the bookmarklet does, each part has been replaced with a function that explains what happens:

(isThisGoogleTranslate() ? (itIsButDoesParamUExist() ? itDoesSoReload() : itDoesNotThrowError()) : notGoogleSoSendToGoogle())

A more in depth look...
Using that analogy, isThisGoogleTranslate() checks if the current page is Google Translate by searching for the substring "translate.google.com" in the page URL.

window.location.href.indexOf("translate.google.com")>-1

When isThisGoogleTranslate() is true, the GET parameter u is checked to see if it exists by using a Regular Expression on the current url.

(new%20RegExp('[?&]u=([^&]*)')).exec(location.search)!=null

If the parameter u exists (is not null), the page is reloaded in Google Translate reloads using that 'u' parameter.

window.location.assign("https://translate.google.com/translate?hl=en&sl=auto&tl=en&sandbox=1&u="+(new%20RegExp('[?&]u=([^&]*)')).exec(location.search)[1]);

When u is null, an error is thrown in the form of a warning dialog.

alert("Error:%20This%20google%20translate%20page%20does%20not%20have%20a%20url%20parameter.");

If the page isn't Google Translate, the current URL is encoded and passed to Google Translate.

window.location.assign("https://translate.google.com/translate?hl=en&sl=auto&tl=en&sandbox=1&u="+encodeURIComponent(window.location.href));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment