/ocr/translate/src/com/google/marvin/translate/GoogleTranslate.java
Java | 137 lines | 80 code | 26 blank | 31 comment | 7 complexity | 44c0d313e654408438ede9e4b6518f34 MD5 | raw file
1/* 2 * Copyright (C) 2009 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16package com.google.marvin.translate; 17 18import android.util.Log; 19 20import org.json.JSONException; 21import org.json.JSONObject; 22 23import java.io.BufferedReader; 24import java.io.IOException; 25import java.io.InputStreamReader; 26import java.net.HttpURLConnection; 27import java.net.URL; 28import java.net.URLEncoder; 29 30/** 31 * Connects to the AJAX-accessible Google Translate backend for online 32 * translation. 33 * 34 * @author alanv@google.com (Alan Viverette) 35 */ 36public class GoogleTranslate extends Thread { 37 private static final String TAG = "GoogleTranslate"; 38 39 private static final String TRANSLATE_URL = 40 "http://ajax.googleapis.com/ajax/services/language/translate"; 41 42 private static final String QUERY_PARAM = "q"; 43 private static final String VERSION_PARAM = "v"; 44 private static final String LANGPAIR_PARAM = "langpair"; 45 private static final String LANGPAIR_SEPARATOR = "%7C"; 46 private static final String VERSION = "1.0"; 47 48 private static final int TIMEOUT = 5000; 49 50 private String mStrUrl; 51 private TranslationListener mCallback; 52 53 public interface TranslationListener { 54 public void onComplete(String result); 55 56 public void onError(int code, String error); 57 } 58 59 /** 60 * Translates a query from a source language to a destination language in a 61 * separate thread. Takes a TranslationListener that returns the translation 62 * result (or error status). 63 * 64 * @param query The text to translate. 65 * @param source The source language code. 66 * @param target The target language code. 67 * @param onComplete The translation completion callback. 68 */ 69 public GoogleTranslate(String query, String source, String target, TranslationListener onComplete) { 70 71 query = URLEncoder.encode(query); 72 source = URLEncoder.encode(source); 73 target = URLEncoder.encode(target); 74 75 String strUrl = TRANSLATE_URL + "?"; 76 strUrl += QUERY_PARAM + "=" + query + "&"; 77 strUrl += VERSION_PARAM + "=" + VERSION + "&"; 78 strUrl += LANGPAIR_PARAM + "=" + source + LANGPAIR_SEPARATOR + target; 79 80 mStrUrl = strUrl; 81 mCallback = onComplete; 82 } 83 84 @Override 85 public void run() { 86 if (mCallback == null) { 87 return; 88 } 89 90 try { 91 Log.i(TAG, "Connecting to " + mStrUrl); 92 URL url = new URL(mStrUrl); 93 94 Log.i(TAG, "Opening connection with " + TIMEOUT + "ms timeout"); 95 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 96 conn.setRequestMethod("GET"); 97 conn.setDoOutput(true); 98 conn.setReadTimeout(TIMEOUT); 99 conn.connect(); 100 101 Log.i(TAG, "Reading content"); 102 InputStreamReader reader = new InputStreamReader(conn.getInputStream()); 103 BufferedReader buffer = new BufferedReader(reader); 104 String content = ""; 105 String line; 106 107 while ((line = buffer.readLine()) != null) { 108 content += line; 109 } 110 111 Log.i(TAG, "Received content:\n" + content); 112 113 conn.disconnect(); 114 115 Log.i(TAG, "Interpreting JSON content"); 116 JSONObject json = new JSONObject(content.toString()); 117 int responseStatus = json.getInt("responseStatus"); 118 119 if (responseStatus == 200) { 120 JSONObject responseData = json.getJSONObject("responseData"); 121 String translatedText = responseData.getString("translatedText"); 122 123 mCallback.onComplete(translatedText); 124 } else { 125 String responseDetails = json.getString("responseDetails"); 126 127 mCallback.onError(responseStatus, responseDetails); 128 } 129 } catch (IOException e) { 130 mCallback.onError(0, e.toString()); 131 } catch (JSONException e) { 132 mCallback.onError(0, e.toString()); 133 } 134 135 mCallback = null; 136 } 137}