首页 文章

从Google翻译活动中返回翻译的文本

提问于
浏览
3

我开发了一个Android应用程序,它使用以下代码启动Google Translate Activity:

...
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
}

我的应用程序从Google Translate Activity调用父onActivityResult(),但 data 为null . 因此,我认为无法将任何翻译后的文本从Google Translate返回到我的应用程序中 . 它是否正确?

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

如果谷歌开发人员(员工)碰巧看到这个并且可能会有所影响,我将不胜感激 . 我真的在寻找官方回应 .

谢谢!

1 回答

  • 1

    我也很想知道这是否可行 .

    同时,如果你需要翻译简单的文本here一个包装Google Translate Http Service的类 .

    我从未在 生产环境 环境中使用它(阅读已发布的应用程序)因为我不确定它是否合法 .

    因此,如果Google(员工)会回答您的问题 . 如果这种方法合法,我们可以告诉我们 .

    为方便起见,这里包含了Google Translate Http Service的简单AsyncTask:

    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的包装器,你可以找到source code here

    问候,

    卢卡

相关问题