Created
January 1, 2016 14:42
-
-
Save brookslyrette/8b1ec4cda64172a77cc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //google translate from source to target language | |
| var callbackCount = 0; | |
| var google = { | |
| key: <google_translate_api_key_goes_here>, | |
| endpoint: "https://www.googleapis.com/language/translate/v2", | |
| language: { | |
| ContentType: {TEXT: "text"}, | |
| translate: function(options, sourceCode, targetCode, callback) { | |
| var callbackName = "callback" + callbackCount; | |
| var localCallbackWrapper = function(resp) { | |
| callback({error: false, translation: resp.data.translations[0].translatedText}); | |
| }; | |
| google.language[callbackName] = localCallbackWrapper; | |
| callbackCount++; | |
| var newScript = document.createElement('script'); | |
| newScript.type = 'text/javascript'; | |
| var source = google.endpoint + "?key=" + google.key + | |
| "&source=" + sourceCode + | |
| "&target=" + targetCode + | |
| "&format=text" + | |
| "&q=" + encodeURI(options.text) + | |
| "&callback=google.language." + callbackName; | |
| newScript.src = source; | |
| document.getElementsByTagName('head')[0].appendChild(newScript); | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment