Google Translate Activity not working anymore
Asked Answered
P

4

6

I wrote a program that invokes Google Translator android application via Intent.ACTION_VIEW. The problem is that invoking the Google Translator App does not work anymore, although it did once.

The code is identical to the code given here:

Returning Translated Text from Google Translate Activity

(yes, I tried to replace my code by that code, the Google Translator App behaves as if it does not receive any data.)

Currently I cannot specify the text and the two languages. The best I can do is to use ACTION_SEND, but it ignores the two languages:

        Intent i = new Intent();
        i.setAction(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_TEXT, "What is going on?");
        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"));

What actually happened when I ran this code was: the Google Translator asked me if I want to translate from English and translated "What is going on?" to French.

So: how do I pass the languages to the Google Translate App now?

Pipsissewa answered 2/10, 2013 at 8:1 Comment(0)
P
14

They have changed it once again:

            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.setPackage("com.google.android.apps.translate");

            intent.putExtra(Intent.EXTRA_TEXT, text);

UPDATE: It is possible to pass the languages if you pack the text and the languages into an URI:

            intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setPackage("com.google.android.apps.translate");

            Uri uri = new Uri.Builder()
                    .scheme("http")
                    .authority("translate.google.com")
                    .path("/m/translate")
                    .appendQueryParameter("q", "c'est l'meunier Mathurin qui caresse les filles au tic-tac du moulin")
                    .appendQueryParameter("tl", "pl") // target language
                    .appendQueryParameter("sl", "fr") // source language
                    .build();
            //intent.setType("text/plain"); //not needed, but possible
            intent.setData(uri);
Pipsissewa answered 2/12, 2013 at 5:15 Comment(2)
Thanks.. tried all the previous solutions and they didn't work, it's frustrating that they keep on changing the API and very much thanks for keep updating us!!Calve
The only solution that actually works is using that uri.Hurd
P
1

UPDATE:

The following code works with the new version of Google Translate Application:

        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! What is going on here?");
        //i.putExtra("key_text_output", "");
        i.putExtra("from", "en");
        i.putExtra("to", "zh-CN");
        //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.HomeActivity"));

As you can see, this is the standard ACTION_SEND with additional parameters "to" and "from".

There's a gotcha: "key_text_input" takes preference over Intent.EXTRA_TEXT, and "to" and "from" work only with "key_text_input".

If you have an impression that no data is passed (at all), maybe it is because you use 3-character language codes instead of 2-character ones. But the codes for Chinese are zh-CN and zh-TW.

My previous post:

The action and the parameter names have changed.

        Intent i = new Intent();
        i.setAction("com.google.android.apps.translate.action.QUERY");
        i.putExtra("key_text_input", "Oh my God! What is going on?");
        i.putExtra("key_text_output", "");
        i.putExtra("from", "en");
        i.putExtra("to", "zh-CN");
        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"));
Pipsissewa answered 3/10, 2013 at 13:40 Comment(0)
A
0

The other answers will open Google Translate app as a full-screen activity. I wanted to open it as a floating window above my current app.

Turns out you can do this with the Intent action of "android.intent.action.PROCESS_TEXT" e.g.

translateIntent.setComponent(new ComponentName(
    "com.google.android.apps.translate",
    "com.google.android.apps.translate.QuickTranslateActivity"
));
translateIntent.setAction(Intent.ACTION_PROCESS_TEXT);
translateIntent.putExtra(Intent.EXTRA_PROCESS_TEXT, textToTranslate);

Note: I didn't actually use this method as I pulled the intent of the system context menu using onActionStarted but I sniffed the intent for you so I don't see why this wouldn't work if created manually.

Adventurism answered 12/2, 2021 at 10:16 Comment(2)
I have been using this, but how to specify input/output language? The "from" or "key_language_from" do not seem to work. There is no official documentation for this calling intent and one has to "sniff" the intent to figure out the supported parameters?Austronesia
Got android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.apps.translate/com.google.android.apps.translate.QuickTranslateActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared <intent-filter>? on device with Android 12Kizer
J
0

Here are the complete and working Java solutions as for September 2024 (note that these are general solutions for passing some text to Google Translate app and answer the question in a broader sense):

The First Version

This is a simple version that will open Google Translate app and translate the text from language and to language recently used by the user inside Google Translate itself.

String textToTranslate = editText.getText().toString().trim();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setPackage("com.google.android.apps.translate");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, textToTranslate);
startActivity(intent);

If you don't specify the setPackage() method, the user's definied default app that can handle simple text will be opened - which can be Google Translate, but doesn't have to.

The Second Version

This version is more advanced since it lets you specify languages from which and to which translate the text (in this case from Italian to Spanish).

String textToTranslate = editText.getText().toString().trim();
String sourceLanguageCode = "it";
String targetLanguageCode = "es";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.apps.translate");
Uri uri = new Uri.Builder()
        .scheme("https")
        .authority("translate.google.com")
        .path("/m/translate")
        .appendQueryParameter("q", textToTranslate)
        .appendQueryParameter("sl", sourceLanguageCode)
        .appendQueryParameter("tl", targetLanguageCode)
        .build();
intent.setData(uri);
startActivity(intent);

IMPORTANT NOTE: please remember that these solutions works offline only if the user has downloaded and installed the necessary language packs.

Jake answered 11/9 at 9:10 Comment(2)
Your 1st version does not mention "com.google.android.apps.translate" or "translate.google.com", is it ok?Pipsissewa
No, it's not ok. Thanks for pointing this out. You are right. I just saw in my codebase that I indeed use setPackage() but for some reason I forgot to paste it in my original answer. So the package has to be set and it is "com.google.android.apps.translate". However, "translate.google.com" is not valid in this case since it's a website.Jake

© 2022 - 2024 — McMap. All rights reserved.