从谷歌翻译活动返回翻译文本 [英] Returning Translated Text from Google Translate Activity

查看:343
本文介绍了从谷歌翻译活动返回翻译文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个Android应用程序会启动谷歌使用以下code翻译活动:

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("key_text_input", "What time is it?");
i.putExtra("key_text_output", "");
i.putExtra("key_language_from", "en");
i.putExtra("key_language_to", "es");
i.putExtra("key_suggest_translation", "");
i.putExtra("key_from_floating_window", false);
i.setComponent(new ComponentName("com.google.android.apps.translate",
    "com.google.android.apps.translate.translation.TranslateActivity"));
startActivityForResult(i, 0);
...

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("yoyo", "in onActivityResult()");
    // data is null
}

父的onActivityResult()被调用我的应用程序从谷歌翻译活动,但数据为空。因此,我认为是没有办法返回任何翻译文本从谷歌翻译回我的应用程序。它是否正确?

The parent onActivityResult() is called in my application from the Google Translate Activity, but data is null. Therefore, I assume that there is no way to return any translated text from Google Translate back into my application. Is this correct?

此外,如果有办法做到这一点,那会是一个违反服务API的方式?如果使用的是谷歌翻译离线语言包/翻译它仍然会是违反?

Also, if there was a way to do this, would it be a violation of the API's terms of service? Would it still be a violation if using the Google Translate offline language packs/translation?

如果一个谷歌开发者(员工)恰好看到这一点,可以权衡,我会AP preciate它。我真的找一个官方的回应。

If a Google developer (employee) happens to see this and could weigh in, I would appreciate it. I'm really looking for an official response.

谢谢!

推荐答案

我也很想知道,如果这是可能的。

I also am curious to know if this is possible.

同时,如果您需要翻译简单的文本这里一类包裹的谷歌翻译HTTP服务

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

我从来没有用它在生产环境中(读取发布的应用程序)becouse我不知道这是否是合法的。

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

因此​​,如果谷歌(员工)会回答你的question..maybe可以让我知道,如果这种做法是合法的。

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

为了方便这里的简单的AsyncTask的包裹谷歌翻译HTTP服务:

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;

    public class TranslatorTask extends AsyncTask<Bundle, Void, String> {

        static final private String TAG = "TRANSLATOR";

        static final private 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)"
        };


        public interface OnTranslatorTaskListener {
            void onTextTranslated(String value);
        };

        private OnTranslatorTaskListener    mCallback = null;


        public void setOnTranslatorTaskListener(OnTranslatorTaskListener listener) {
            mCallback = listener;
        }

        @Override
        protected String doInBackground(Bundle... params) {
            if (params == null) return null;

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

            String json = null;

            RESTClient rest = null;
            try {
                rest = new RESTClient("http://translate.google.com/translate_a/t");
                rest.header("User-Agent", UA_LIST[new Random().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) return null;

            StringBuffer result = new StringBuffer();
            try {
                JSONObject jo = new JSONObject(json);
                if (jo.has("sentences")) {
                    JSONArray ja = jo.getJSONArray("sentences");
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject item = ja.getJSONObject(i);
                        if (item.has("trans"))
                            result.append(item.getString("trans"));
                    }
                }
            } catch (JSONException je) {
                Log.e(TAG, je.getMessage(), je);
            }

            return result.toString();
        }


        @Override
        protected void onPostExecute(String result) {
            if (result != null && mCallback != null)
                mCallback.onTextTranslated(result);
        }
    };

在[RESTClient实现类]是一个包装HttpClient的,你可以找到的来源$ C ​​$ C这里

问候,

卢卡

这篇关于从谷歌翻译活动返回翻译文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆