Skip to content

Instantly share code, notes, and snippets.

@techstreams
Last active January 26, 2016 20:15
Show Gist options
  • Save techstreams/c99f4f0c88f9c62eadee to your computer and use it in GitHub Desktop.
Save techstreams/c99f4f0c88f9c62eadee to your computer and use it in GitHub Desktop.
Simple Google Apps Script Translation Content Server for iOS Workflow App. See blog post - https://goo.gl/2oWDFU
/**
* Copyright (c), Laura Taylor. (MIT Licensed)
* https://github.com/techstreams
*/
/**
* Simple Google Apps Script Content Server
* @returns {String}
*/
function doGet(e) {
var langKeys = [],
json = {},
toLang,
fromLang,
content,
langs = {
afrikaans:'af',albanian:'sq',arabic:'ar',armenian:'hy',azerbaijani:'az',basque:'eu',belarusian:'be',
bengali:'bn',bosnian:'bs',bulgarian:'bg',catalan:'ca',cebuano:'ceb',chinese:'zh',croatian:'hr',
czech:'cs',danish:'da',dutch:'nl',english:'en',esperanto:'eo',estonian:'et',filipino:'tl',finnish:'fi',
french:'fr',galician:'gl',georgian:'ka',german:'de',greek:'el',gujarati:'gu',haitian:'ht',hausa:'ha',
hebrew:'iw',hindi:'hi',hmong:'hmn',hungarian:'hu',icelandic:'is',igbo:'ig',indonesian:'id',irish:'ga',
italian:'it',japanese:'ja',javanese:'jv',kannada:'kn',kazakh:'kk',khmer:'km',korean:'ko',lao:'lo',
latin:'la',latvian:'lv',lithuanian:'lt',macedonian:'mk',malagasy:'mg',malay:'ms',malayalam:'ml',
maltese:'mt',maori:'mi',marathi:'mr',mongolian:'mn',nepali:'ne',norwegian:'no',nyanja:'ny',
persian:'fa',polish:'pl',portuguese:'pt',punjabi:'pa',romanian:'ro',russian:'ru',serbian:'sr',sinhala:'si',
slovak:'sk',slovenian:'sl',somali:'so',spanish:'es',sudanese:'su',swahili:'sw',swedish:'sv',
tajik:'tg',tamil:'ta',telugu:'te',thai:'th',turkish:'tr',ukrainian:'uk',urdu:'ur',uzbek:'uz',vietnamese:'vi',
welsh:'cy',yiddish:'yi',yoruba:'yo',zulu:'zu'
}, // List of translation languages and codes
nativeLang = 'english', // Content origin language - Change this value as needed
templateNames = ["Example1,Example2"]; // Comma separated list of content template names
// If we detect 'lang' and 'template' parameters, attempt to translate selected template
// Otherwise, return a list of translation languages
if (e.parameter.lang && e.parameter.template) {
try {
toLang = langs[e.parameter.lang.trim().toLowerCase()];
fromLang = langs[nativeLang];
if (toLang && fromLang) {
json.langs = e.parameter.lang.trim();
content = HtmlService.createHtmlOutputFromFile(e.parameter.template.trim()).getContent();
if (toLang == fromLang) {
json.content = content;
} else {
json.content = LanguageApp.translate(content, fromLang, toLang, {contentType:'html'});
}
} else {
throw new Error(); // Throw error if invalid origin or translation language detected
}
} catch(e) {
json.error = 500; // Return an error code if error detected while trying to translate content
}
} else {
Object.keys(langs).forEach(function(key) {
langKeys.push(key.charAt(0).toUpperCase() + key.slice(1));
});
json.langs = langKeys.join(',');
json.content = templateNames;
}
return ContentService.createTextOutput(JSON.stringify(json)); // Return response to Workflow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment