/ocr/translate/src/com/google/marvin/translate/GoogleTranslate.java

http://eyes-free.googlecode.com/ · 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. */
  16. package com.google.marvin.translate;
  17. import android.util.Log;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.net.HttpURLConnection;
  24. import java.net.URL;
  25. import java.net.URLEncoder;
  26. /**
  27. * Connects to the AJAX-accessible Google Translate backend for online
  28. * translation.
  29. *
  30. * @author alanv@google.com (Alan Viverette)
  31. */
  32. public class GoogleTranslate extends Thread {
  33. private static final String TAG = "GoogleTranslate";
  34. private static final String TRANSLATE_URL =
  35. "http://ajax.googleapis.com/ajax/services/language/translate";
  36. private static final String QUERY_PARAM = "q";
  37. private static final String VERSION_PARAM = "v";
  38. private static final String LANGPAIR_PARAM = "langpair";
  39. private static final String LANGPAIR_SEPARATOR = "%7C";
  40. private static final String VERSION = "1.0";
  41. private static final int TIMEOUT = 5000;
  42. private String mStrUrl;
  43. private TranslationListener mCallback;
  44. public interface TranslationListener {
  45. public void onComplete(String result);
  46. public void onError(int code, String error);
  47. }
  48. /**
  49. * Translates a query from a source language to a destination language in a
  50. * separate thread. Takes a TranslationListener that returns the translation
  51. * result (or error status).
  52. *
  53. * @param query The text to translate.
  54. * @param source The source language code.
  55. * @param target The target language code.
  56. * @param onComplete The translation completion callback.
  57. */
  58. public GoogleTranslate(String query, String source, String target, TranslationListener onComplete) {
  59. query = URLEncoder.encode(query);
  60. source = URLEncoder.encode(source);
  61. target = URLEncoder.encode(target);
  62. String strUrl = TRANSLATE_URL + "?";
  63. strUrl += QUERY_PARAM + "=" + query + "&";
  64. strUrl += VERSION_PARAM + "=" + VERSION + "&";
  65. strUrl += LANGPAIR_PARAM + "=" + source + LANGPAIR_SEPARATOR + target;
  66. mStrUrl = strUrl;
  67. mCallback = onComplete;
  68. }
  69. @Override
  70. public void run() {
  71. if (mCallback == null) {
  72. return;
  73. }
  74. try {
  75. Log.i(TAG, "Connecting to " + mStrUrl);
  76. URL url = new URL(mStrUrl);
  77. Log.i(TAG, "Opening connection with " + TIMEOUT + "ms timeout");
  78. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  79. conn.setRequestMethod("GET");
  80. conn.setDoOutput(true);
  81. conn.setReadTimeout(TIMEOUT);
  82. conn.connect();
  83. Log.i(TAG, "Reading content");
  84. InputStreamReader reader = new InputStreamReader(conn.getInputStream());
  85. BufferedReader buffer = new BufferedReader(reader);
  86. String content = "";
  87. String line;
  88. while ((line = buffer.readLine()) != null) {
  89. content += line;
  90. }
  91. Log.i(TAG, "Received content:\n" + content);
  92. conn.disconnect();
  93. Log.i(TAG, "Interpreting JSON content");
  94. JSONObject json = new JSONObject(content.toString());
  95. int responseStatus = json.getInt("responseStatus");
  96. if (responseStatus == 200) {
  97. JSONObject responseData = json.getJSONObject("responseData");
  98. String translatedText = responseData.getString("translatedText");
  99. mCallback.onComplete(translatedText);
  100. } else {
  101. String responseDetails = json.getString("responseDetails");
  102. mCallback.onError(responseStatus, responseDetails);
  103. }
  104. } catch (IOException e) {
  105. mCallback.onError(0, e.toString());
  106. } catch (JSONException e) {
  107. mCallback.onError(0, e.toString());
  108. }
  109. mCallback = null;
  110. }
  111. }