How to use the Google Translator app

I have encoded a dictionary sentence program and I want to have a function to go to the google translator application in my application

How can I use it, should I import something?

+8
android google-translate
source share
5 answers

From what I can say, the Google Translate app for Android does not provide any standard Intent that you could use (it's creepy, but it is weird at the same time. Think Google will encourage this type of interaction between applications .. anyway).

However, it seems that Google has opened a translation API through a web service. Thus, you can do the translation yourself and show it in your application. This is a bit more work, but he has to do this work.

You can look at google-api-translate-java if you want to get rid of writing an API wrapper.

+7
source share

Phi Van Ngoc answer was fantastic, thanks for that.

However, at first this did not work for me, and after studying the Translate apk, it seems that they slightly changed the structure of their files, so now the name ComponentName should be:

 i.setComponent( new ComponentName( "com.google.android.apps.translate", "com.google.android.apps.translate.translation.TranslateActivity")); 

The difference is that before the translation of "TranslateActivity" a "translation" was added

So my final version, including the hard-coded translation from Spanish to English, is as follows:

 Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); i.putExtra("key_text_input", "Me gusta la cerveza"); i.putExtra("key_text_output", ""); i.putExtra("key_language_from", "es"); i.putExtra("key_language_to", "en"); 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")); startActivity(i); 
+6
source share

I have the same problem. I originally tried to use the Google Translate Ajax API, but since Google has deprecated API version 1 and made version 2 paid, my code stops working. Then I decompiled the Google Translate application, looked into the Smali code and got some hint of logic inside it. Use this code, it works for me:

 private void callGoogleTranslateApps(String word, String fromLang, String toLang) { Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); i.putExtra("key_text_input", word); i.putExtra("key_text_output", ""); i.putExtra("key_language_from", fromLang); i.putExtra("key_language_to", toLang); 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.TranslateActivity")); startActivity(i); } 
+5
source share

OMG! They changed it again! They made it more reasonable, but not compatible with the previous version.

 Intent i = new Intent(); i.setAction(Intent.ACTION_SEND); i.putExtra(Intent.EXTRA_TEXT, "What is going on?"); i.putExtra("key_text_input", "Oh my God!"); i.putExtra("from", "en"); i.putExtra("to", "zh-CN"); i.setComponent(new ComponentName("com.google.android.apps.translate", "com.google.android.apps.translate.HomeActivity")); 

This seems to be the intention of SEND with two additional parameters (BTW, optional), "to" and "from".

There is: "key_text_input" prefers Intent.EXTRA_TEXT, and "to" and "from" only works with "key_text_input".

For people who are changing the API with each new version, it might seem reasonable to rename "key_text_input", say, just "text_input", so we look forward to the next version ...

To be safe, I would suggest setting both Intent.EXTRA_TEXT and key_text_input to the same value.

+4
source share

To add the following answers:

It’s important that you pass the codes of the two letters . With 3 letter codes, it might seem that the google translate app is not receiving any data.

Alternatively, if Intent.ACTION_VIEW not working, you can use Intent.ACTION_SEND .

  intent = new Intent(); //intent.setAction(Intent.ACTION_VIEW); // this did not work for me initially intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, m_text); intent.putExtra("key_text_input", m_text); intent.putExtra("key_text_output", ""); intent.putExtra("key_language_from", m_language); intent.putExtra("key_language_to", lang_to); intent.putExtra("key_suggest_translation", ""); intent.putExtra("key_from_floating_window", false); intent.setComponent( new ComponentName( "com.google.android.apps.translate", "com.google.android.apps.translate.HomeActivity" )); //try { startActivityForResult(intent, REQUEST_CODE_TRANSLATE); //... 
+2
source share

All Articles