7 package com.google.appinventor.components.runtime;
9 import android.app.Activity;
11 import android.text.TextUtils;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
31 import java.net.HttpURLConnection;
33 import java.net.URLEncoder;
35 import org.json.JSONException;
36 import org.json.JSONObject;
54 @DesignerComponent(version = YaVersion.YANDEX_COMPONENT_VERSION,
55 description =
"Use this component to translate words and sentences between different " +
56 "languages. This component needs Internet access, as it will request " +
57 "translations to the Yandex.Translate service. Specify the source and " +
58 "target language in the form source-target using two letter language " +
59 "codes. So\"en-es\" will translate from English to Spanish while " +
60 "\"es-ru\" will translate from Spanish to Russian. If you leave " +
61 "out the source language, the service will attempt to detect the source " +
62 "language. So providing just \"es\" will attempt to detect the source " +
63 "language and translate it to Spanish.<p /> This component is powered by the " +
64 "Yandex translation service. See http://api.yandex.com/translate/ " +
65 "for more information, including the list of available languages and the " +
66 "meanings of the language codes and status codes. " +
67 "<p />Note: Translation happens asynchronously in the background. When the " +
68 "translation is complete, the \"GotTranslation\" event is triggered.",
69 category = ComponentCategory.MEDIA,
71 iconName =
"images/yandex.png")
72 @UsesPermissions(permissionNames =
"android.permission.INTERNET")
77 "https://translate.yandex.net/api/v1.5/tr.json/translate?key=";
78 private final String yandexKey;
79 private String userYandexKey =
"DEFAULT";
80 private final Activity activity;
88 super(container.
$form());
91 form.setYandexTranslateTagline();
109 @
SimpleFunction(description =
"By providing a target language to translate to (for instance, " +
110 "'es' for Spanish, 'en' for English, or 'ru' for Russian), and a word or sentence to " +
111 "translate, this method will request a translation to the Yandex.Translate service.\n" +
112 "Once the text is translated by the external service, the event GotTranslation will be " +
113 "executed.\nNote: Yandex.Translate will attempt to detect the source language. You can " +
114 "also specify prepending it to the language translation. I.e., es-ru will specify Spanish " +
115 "to Russian translation.")
117 final String textToTranslate) {
119 if (TextUtils.isEmpty(yandexKey) &&
120 (TextUtils.isEmpty(userYandexKey) || TextUtils.equals(userYandexKey,
"DEFAULT"))) {
130 performRequest(languageToTranslateTo, textToTranslate);
131 }
catch (IOException e) {
134 }
catch (JSONException je) {
150 private void performRequest(String languageToTranslateTo, String textToTranslate)
151 throws IOException, JSONException {
154 ((TextUtils.equals(this.userYandexKey,
"DEFAULT") ||
155 TextUtils.isEmpty(this.userYandexKey))?this.yandexKey:this.userYandexKey) +
156 "&lang=" + languageToTranslateTo +
157 "&text=" + URLEncoder.encode(textToTranslate,
"UTF-8");
159 URL url =
new URL(finalURL);
160 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
161 if (connection !=
null) {
163 final String responseContent = getResponseContent(connection);
165 JSONObject jsonResponse =
new JSONObject(responseContent);
167 final String responseCode = jsonResponse.getString(
"code");
170 org.json.JSONArray response = jsonResponse.getJSONArray(
"text");
171 final String translation = (String)response.get(0);
174 activity.runOnUiThread(
new Runnable() {
182 connection.disconnect();
193 private static String getResponseContent(HttpURLConnection connection)
throws IOException {
195 String encoding = connection.getContentEncoding();
196 if (encoding ==
null) {
199 InputStreamReader reader =
new InputStreamReader(connection.getInputStream(), encoding);
201 int contentLength = connection.getContentLength();
202 StringBuilder sb = (contentLength != -1)
203 ?
new StringBuilder(contentLength)
204 :
new StringBuilder();
205 char[] buf =
new char[1024];
207 while ((read = reader.read(buf)) != -1) {
208 sb.append(buf, 0, read);
210 return sb.toString();
222 @SimpleEvent(description =
"Event triggered when the Yandex.Translate service returns the " +
223 "translated text. This event also provides a response code for error handling. If the " +
224 "responseCode is not 200, then something went wrong with the call, and the translation will " +
238 defaultValue =
"DEFAULT")
239 @
SimpleProperty(description =
"Set the API Key to use with Yandex. " +
240 "You do not need to set this if you are using the MIT system because " +
241 "MIT has its own key builtin. If set, the key provided here will be " +
244 this.userYandexKey = apiKey;