Created
December 29, 2012 18:47
-
-
Save rcknr/4408646 to your computer and use it in GitHub Desktop.
Google Apps Script wrapper for Translate API Limitations:
- Doesn't return detectedSourceLanguage when called without source parameter;
- Doesn't support prettyprint parameter;
- Reduced error messages that return HTTP 200 status;
- Doesn't support key parameter and billing ;)
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
function doGet(r) { | |
var translations = []; | |
var e = {error:{code:400}}; | |
try { | |
if(r.parameters.q) { | |
for(var n in r.parameters.q) { | |
translations.push({translatedText: | |
LanguageApp.translate( | |
r.parameters.q[n], | |
(r.parameters.source)?r.parameters.source:null, | |
r.parameters.target, | |
{contentType: (r.parameters.format=="text")?"text":"html"} | |
) | |
}); | |
var response = Utilities.jsonStringify({data:{translations:translations}}); | |
if(r.parameters.callback) { | |
if((/^[$A-Z_][0-9A-Z_$]*$/i).test(r.parameters.callback)) | |
response = r.parameters.callback+"("+response+");"; | |
else throw new Error("Invalid value for parameter callback: "+r.parameters.callback); | |
} | |
} | |
} | |
else throw new Error("Required parameter: q"); | |
} | |
catch(err) { | |
e.error.message = err.message; | |
response = Utilities.jsonStringify(e); | |
} | |
return ContentService.createTextOutput(response).setMimeType(ContentService.MimeType.JSON); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from where does the LanguageApp class comes from?