Created
September 25, 2015 06:30
-
-
Save supersupermomonga/54f7b61638ac2896c75c to your computer and use it in GitHub Desktop.
Google Apps Script files: https://script.google.com/macros/s/AKfycbxYTTMS5XJY48xiVKkB1tWMXRowcjkUTuwkA93-4cU/dev
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
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script> | |
<script> | |
function download(blob, filename) { | |
var objectUrl = (window.URL || window.webkitURL).createObjectURL(blob), | |
elem = document.createElement('a'), | |
event = document.createEvent('MouseEvent'); | |
elem.download = filename; | |
elem.href = objectUrl; | |
event.initEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); | |
elem.dispatchEvent(event); | |
} | |
function toBlob(base64, contentType) { | |
var bin = atob(base64.replace(/^.*,/, '')), | |
buffer = new Uint8Array(bin.length); | |
for (var i = 0; i < bin.length; i++) { | |
buffer[i] = bin.charCodeAt(i); | |
}; | |
try { | |
return new Blob([buffer.buffer], { type: contentType }); | |
} catch (e){ | |
return false; | |
} | |
} | |
$(function() { | |
$('form').on('submit', function (e) { | |
e.preventDefault(); | |
var params = { | |
originalText: $('#originalText').val(), | |
translateInto: $('#translateInto').val(), | |
success: function (res) { | |
console.log(res); | |
download(toBlob(res.byte, res.contentType), res.name); | |
}, | |
failure: function (res) { | |
console.warn(res); | |
} | |
}; | |
google.script.run | |
.withSuccessHandler(params.success) | |
.withFailureHandler(params.failure) | |
.onFormSubmit(params.originalText, params.translateInto); | |
}); | |
}); | |
</script> |
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
<link rel="stylesheet" src="//normalize-css.googlecode.com/svn/trunk/normalize.css" /> | |
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> | |
<style> | |
html, body { | |
font-familiy: "Helvetica Neue",Helvetica,Arial; | |
-webkit-font-smoothing: antialiased; | |
} | |
textarea { | |
resize: vertical; | |
} | |
.navbar { | |
border-radius: 0 !important; | |
} | |
</style> |
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() { | |
return HtmlService.createTemplateFromFile('layout').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setTitle('Translate sentences in English'); | |
} | |
function include(filename) { | |
return HtmlService.createHtmlOutputFromFile(filename).getContent(); | |
} | |
function onFormSubmit(originalText, translateInto) { | |
try { | |
if (!originalText && !translateInto) throw new Error('Missing required arguments'); | |
var data = translateSentencesFromOriginalText(originalText, translateInto), | |
blob = generateCsv(data); | |
return { | |
byte: Utilities.base64Encode(blob.getBytes()), | |
name: blob.getName(), | |
contentType: blob.getContentType() | |
}; | |
} catch(ex) { | |
return ex.toString(); | |
} | |
} | |
function translateSentencesFromOriginalText(originalText, translateInto) { | |
var result = [['en', translateInto]], | |
sentences = originalText.replace(/(\.+|\:|\!|\?)(\"*|\'*|\)*|}*|]*)(\s|\n|\r|\r\n)/gm, '$1$2|').split('|'); | |
for(var i = 0; i < sentences.length; i++) { | |
var translatedSentence = LanguageApp.translate(sentences[i], 'en', translateInto); | |
result.push([sentences[i], translatedSentence]); | |
} | |
return result; | |
} | |
function generateCsv(data) { | |
var filename = 'translate_from_english_'+new Date().getTime()+'.csv', | |
csvString = Object.keys(data).map(function(i) { | |
return Object.keys(data[i]).map(function(j) { | |
return '"' + data[i][j] + '"'; | |
}).join(','); | |
}).join('\r\n'), | |
blob = Utilities.newBlob('', 'text/csv', filename).setDataFromString(csvString, 'utf-8'); | |
return blob; | |
} | |
function doPost(e) { | |
try { | |
if (!e) throw new Error('Missing required parameters'); | |
var data = translateSentencesFromOriginalText(e.parameter.originalText, e.parameter.translateInto), | |
blob = generateCsv(data); | |
return makeResponse(blob.getDataAsString(), ContentService.MimeType.CSV); | |
} catch(ex) { | |
return makeResponse(JSON.stringify({ message: ex.toString() }), ContentService.MimeType.JSON); | |
} | |
} | |
function makeResponse(content, mimeType) { | |
return ContentService.createTextOutput(content).setMimeType(mimeType); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
<?!= include('_stylesheet'); ?> | |
</head> | |
<body> | |
<nav class="navbar navbar-inverse"> | |
<div class="container-fluid"> | |
<div class="navbar-header"> | |
<a class="navbar-brand" href="#">Translate sentences in English</a> | |
</div> | |
</div> | |
</nav> | |
<div class="container-fluid"> | |
<form> | |
<div class="form-group"> | |
<label for="originalText">Original Text</label> | |
<textarea name="originalText" id="originalText" class="form-control" rows="10" placeholder="Please enter the text (in English) that you want to translate."></textarea> | |
</div> | |
<div class="form-group"> | |
<label for="translateInto">Translate into</label> | |
<select class="form-control" id="translateInto"> | |
<option value="ar">Arabic</option> | |
<option value="bg">Bulgarian</option> | |
<option value="bn">Bengali</option> | |
<option value="ca">Catalan</option> | |
<option value="cs">Czech</option> | |
<option value="da">Danish</option> | |
<option value="de">German</option> | |
<option value="el">Greek</option> | |
<option value="es">Spanish</option> | |
<option value="eu">Basque</option> | |
<option value="fa">Farsi</option> | |
<option value="fi">Finnish</option> | |
<option value="fil">Filipino</option> | |
<option value="fr">French</option> | |
<option value="gl">Galician</option> | |
<option value="gu">Gujarati</option> | |
<option value="hi">Hindi</option> | |
<option value="hr">Croatian</option> | |
<option value="hu">Hungarian</option> | |
<option value="id">Indonesian</option> | |
<option value="it">Italian</option> | |
<option value="iw">Hebrew</option> | |
<option value="ja" selected>Japanese</option> | |
<option value="kn">Kannada</option> | |
<option value="ko">Korean</option> | |
<option value="lt">Lithuanian</option> | |
<option value="lv">Latvian</option> | |
<option value="ml">Malayalam</option> | |
<option value="mr">Marathi</option> | |
<option value="nl">Dutch</option> | |
<option value="no">Norwegian</option> | |
<option value="or">Oriya</option> | |
<option value="pl">Polish</option> | |
<option value="pt">Portuguese</option> | |
<option value="ko">Korean</option> | |
<option value="rm">Romansch</option> | |
<option value="ro">Romanian</option> | |
<option value="ru">Russian</option> | |
<option value="sk">Slovak</option> | |
<option value="sl">Slovenian</option> | |
<option value="sr">Serbian</option> | |
<option value="sv">Swedish</option> | |
<option value="tl">Tagalog</option> | |
<option value="ta">Tamil</option> | |
<option value="te">Telugu</option> | |
<option value="th">Thai</option> | |
<option value="tr">Turkish</option> | |
<option value="uk">Ukrainian</option> | |
<option value="vi">Vietnamese</option> | |
<option value="zh-CN">Chinese (simplified)</option> | |
<option value="zh-TW">Chinese (traditional)</option> | |
</select> | |
</div> | |
<input type="submit" class="btn btn-primary" value="Translate & Download CSV" /> | |
</form> | |
</div> | |
<?!= include('_script'); ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment