Last active
October 25, 2018 10:37
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 asde
for German (e.g....&tl=de&...
). The source language code can also be changed fromauto
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:
To illustrate what the bookmarklet does, each part has been replaced with a function that explains what happens:
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.When
isThisGoogleTranslate()
istrue
, the GET parameteru
is checked to see if it exists by using a Regular Expression on the current url.If the parameter
u
exists (is notnull
), the page is reloaded in Google Translate reloads using that 'u' parameter.When
u
isnull
, an error is thrown in the form of a warning dialog.If the page isn't Google Translate, the current URL is encoded and passed to Google Translate.