Skip to content Skip to sidebar Skip to footer

Returning Translated Text From Google Translate Activity

I have developed an Android application that launches a Google Translate Activity using the following code: ... Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); i.putExtra

Solution 1:

I also am curious to know if this is possible.

Meanwhile, if you need to translate simple text here a class that wrap the Google Translate Http Service.

I've never used it in a production enviroment (read a released app) becouse I'm not sure if it's legal.

So if a Google (employee) will answer to your question..maybe could let me know if this approach is legal.

For convenience here the simple AsyncTask that wrap the Google Translate Http Service:

import java.io.IOException;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Locale;
    import java.util.Random;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;

    import com.lus.android.net.RESTClient;

    publicclassTranslatorTaskextendsAsyncTask<Bundle, Void, String> {

        staticfinalprivateStringTAG="TRANSLATOR";

        staticfinalprivate String[] UA_LIST = {
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0",
            "Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2",
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:22.0) Gecko/20130328 Firefox/22.0",
            "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; InfoPath.1; SV1; .NET CLR 3.8.36217; WOW64; en-US)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; .NET CLR 2.7.58687; SLCC2; Media Center PC 5.0; Zune 3.4; Tablet PC 3.6; InfoPath.3)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322)"
        };


        publicinterfaceOnTranslatorTaskListener {
            voidonTextTranslated(String value);
        };

        privateOnTranslatorTaskListenermCallback=null;


        publicvoidsetOnTranslatorTaskListener(OnTranslatorTaskListener listener) {
            mCallback = listener;
        }

        @Overrideprotected String doInBackground(Bundle... params) {
            if (params == null) returnnull;

            Collections.shuffle(Arrays.asList(UA_LIST));

            Stringjson=null;

            RESTClientrest=null;
            try {
                rest = newRESTClient("http://translate.google.com/translate_a/t");
                rest.header("User-Agent", UA_LIST[newRandom().nextInt(UA_LIST.length)]);
                rest.data("client", "j"); //t = TEXT
                rest.data("ie", "UTF-8");
                rest.data("oe", "UTF-8");

                rest.data("hl", params[0].getString("hl")); //, Locale.getDefault().getLanguage()));
                rest.data("sl", params[0].getString("sl")); 
                rest.data("tl", params[0].getString("tl"));
                rest.data("q", params[0].getString("text"));

                json = rest.execute();

            } catch (IOException ioe) {
                Log.e(TAG, ioe.getMessage(), ioe);

            } finally {
                if (rest != null) rest.shutdown();
            }

            if (json == null) returnnull;

            StringBufferresult=newStringBuffer();
            try {
                JSONObjectjo=newJSONObject(json);
                if (jo.has("sentences")) {
                    JSONArrayja= jo.getJSONArray("sentences");
                    for (inti=0; i < ja.length(); i++) {
                        JSONObjectitem= ja.getJSONObject(i);
                        if (item.has("trans"))
                            result.append(item.getString("trans"));
                    }
                }
            } catch (JSONException je) {
                Log.e(TAG, je.getMessage(), je);
            }

            return result.toString();
        }


        @OverrideprotectedvoidonPostExecute(String result) {
            if (result != null && mCallback != null)
                mCallback.onTextTranslated(result);
        }
    };

The [RESTClient class] is a wrapper to HttpClient, you can found the source code here

Regards,

luca

Post a Comment for "Returning Translated Text From Google Translate Activity"