Permalink
Browse files

Porting to paid Google translate API and API v2

  • Loading branch information...
pamelafox committed Jun 18, 2012
1 parent d20d468 commit af34dd2a5316efe12562ba73a4e4204b6738c188
Showing with 188 additions and 11 deletions.
  1. +180 −0 static/js/googlelang.js
  2. +3 −6 static/js/script.js
  3. +3 −3 templates/base.html
  4. +2 −2 templates/index.html
View
@@ -0,0 +1,180 @@
var google = window.google || {};
/* http://javascriptweblog.wordpress.com/2010/11/29/json-and-jsonp/ */
google.jsonp = {
callbackCounter: 0,
fetch: function(url, callback) {
var fn = 'JSONPCallback_' + this.callbackCounter++;
window[fn] = this.evalJSONP(callback);
url = url.replace('=JSONPCallback', '=' + fn);
var scriptTag = document.createElement('SCRIPT');
scriptTag.src = url;
document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
},
evalJSONP: function(callback) {
return function(data) {
var validJSON = false;
if (typeof data == 'object') {
validJSON = data;
} else if (typeof data == "string") {
try {
validJSON = JSON.parse(data);
} catch (e) {
/*invalid JSON*/
}
}
if (validJSON) {
callback(validJSON);
} else {
throw("JSONP call returned invalid or empty JSON");
}
}
}
};
google.language = {};
google.language.translate = function (query, srcLang, destLang, callback) {
function processJSON(json) {
var result = {};
if (json.data && json.data.translations && json.data.translations.length) {
result.translation = json.data.translations[0].translatedText;
} else {
result.error = 'Error';
}
callback(result);
}
var url = 'https://www.googleapis.com/language/translate/v2?key=' + google.API_KEY + '&source=' + srcLang + '&target=' + destLang + '&callback=JSONPCallback&q=' + query;
google.jsonp.fetch(url, processJSON);
};
google.language.detect = function(query, callback) {
function processJSON(json) {
var result = {};
if (json.data && json.data.detections && json.data.detections.length) {
result.language = json.data.detections[0].language;
} else {
result.error = 'Error';
}
callback(result);
}
var url = 'https://www.googleapis.com/language/translate/v2/detect?key=' + google.API_KEY + '&callback=JSONPCallback&q=' + query;
google.jsonp.fetch(url, processJSON);
};
google.language.Languages = {
"AFRIKAANS": "af",
"ALBANIAN": "sq",
"AMHARIC": "am",
"ARABIC": "ar",
"ARMENIAN": "hy",
"AZERBAIJANI": "az",
"BASQUE": "eu",
"BELARUSIAN": "be",
"BENGALI": "bn",
"BIHARI": "bh",
"BULGARIAN": "bg",
"BURMESE": "my",
"BRETON": "br",
"CATALAN": "ca",
"CHEROKEE": "chr",
"CHINESE": "zh",
"CHINESE_SIMPLIFIED": "zh-CN",
"CHINESE_TRADITIONAL": "zh-TW",
"CORSICAN": "co",
"CROATIAN": "hr",
"CZECH": "cs",
"DANISH": "da",
"DHIVEHI": "dv",
"DUTCH": "nl",
"ENGLISH": "en",
"ESPERANTO": "eo",
"ESTONIAN": "et",
"FAROESE": "fo",
"FILIPINO": "tl",
"FINNISH": "fi",
"FRENCH": "fr",
"FRISIAN": "fy",
"GALICIAN": "gl",
"GEORGIAN": "ka",
"GERMAN": "de",
"GREEK": "el",
"GUJARATI": "gu",
"HAITIAN_CREOLE": "ht",
"HEBREW": "iw",
"HINDI": "hi",
"HUNGARIAN": "hu",
"ICELANDIC": "is",
"INDONESIAN": "id",
"INUKTITUT": "iu",
"IRISH": "ga",
"ITALIAN": "it",
"JAPANESE": "ja",
"JAVANESE": "jw",
"KANNADA": "kn",
"KAZAKH": "kk",
"KHMER": "km",
"KOREAN": "ko",
"KURDISH": "ku",
"KYRGYZ": "ky",
"LAO": "lo",
"LAOTHIAN": "lo",
"LATIN": "la",
"LATVIAN": "lv",
"LITHUANIAN": "lt",
"LUXEMBOURGISH": "lb",
"MACEDONIAN": "mk",
"MALAY": "ms",
"MALAYALAM": "ml",
"MALTESE": "mt",
"MAORI": "mi",
"MARATHI": "mr",
"MONGOLIAN": "mn",
"NEPALI": "ne",
"NORWEGIAN": "no",
"OCCITAN": "oc",
"ORIYA": "or",
"PASHTO": "ps",
"PERSIAN": "fa",
"POLISH": "pl",
"PORTUGUESE": "pt",
"PORTUGUESE_PORTUGAL": "pt-PT",
"PUNJABI": "pa",
"QUECHUA": "qu",
"ROMANIAN": "ro",
"RUSSIAN": "ru",
"SANSKRIT": "sa",
"SCOTS_GAELIC": "gd",
"SERBIAN": "sr",
"SINDHI": "sd",
"SINHALESE": "si",
"SLOVAK": "sk",
"SLOVENIAN": "sl",
"SPANISH": "es",
"SUNDANESE": "su",
"SWAHILI": "sw",
"SWEDISH": "sv",
"SYRIAC": "syr",
"TAJIK": "tg",
"TAMIL": "ta",
"TAGALOG": "tl",
"TATAR": "tt",
"TELUGU": "te",
"THAI": "th",
"TIBETAN": "bo",
"TONGA": "to",
"TURKISH": "tr",
"UKRAINIAN": "uk",
"URDU": "ur",
"UZBEK": "uz",
"UIGHUR": "ug",
"VIETNAMESE": "vi",
"WELSH": "cy",
"YIDDISH": "yi",
"YORUBA": "yo",
"UNKNOWN": ""
};
View
@@ -1,5 +1,3 @@
google.load("language", "1");
// Valid languages for translation pairs
var allLangs = [
'AFRIKAANS',
@@ -318,7 +316,6 @@ function shareTwitter() {
'?url=' + tweetUrl.replace('#', '%23');
//'&text=Check+out+this+funny+translation';
//replace('#', '%23');
console.log(url);
window.open(url,
'_blank', 'resizable=0,scrollbars=0,width=690,height=415');
}
@@ -356,9 +353,8 @@ function initMain() {
} else if (id.length > 0) {
loadRound(id);
}
google.language.getBranding('branding');
getRounds('-date', $('#recent'), 3);
getRounds('-views', $('#popular'), 3);
getYours(3);
@@ -383,3 +379,4 @@ function initYours() {
getYours(1000);
}
google.API_KEY = 'AIzaSyBLBeqjz4y-yYybCig6p1PMKnt9g4PLLNU';
View
@@ -55,8 +55,8 @@ <h1><a href="/">Translation Telephone</a></h1>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/script.js?v=25"></script>
<script type="text/javascript" src="js/googlelang.js?v=26"></script>
<script type="text/javascript" src="js/script.js?v=26"></script>
{% block script %}{% endblock %}
<script type="text/javascript">
@@ -72,4 +72,4 @@ <h1><a href="/">Translation Telephone</a></h1>
</script>
</body>
</html>
</html>
View
@@ -61,6 +61,6 @@ <h3>Yours:</h3>
{% endblock %}
{% block script %}
<script>
google.setOnLoadCallback(initMain);
initMain();
</script>
{% endblock %}
{% endblock %}

0 comments on commit af34dd2

Please sign in to comment.