Skip to content Skip to sidebar Skip to footer

How To Use Google Translator App

I coded program about dictionary sentence and I want to have function to go to 'google translator' application in my app How can I use it , Should I import anything?

Solution 1:

From what I can tell, the Google Translate Android app does not expose any standard Intents that you could use (it's a pitty, but it's weird at the same time. You'd think Google would encourage this type of interaction between apps.. anyway).

However, it seems Google have opened up the translate API via a web service. This way, you can do the translation yourself and show it within your app. It's a bit more work, but it should do the job.

You could look at google-api-translate-java if you want to spare yourself from writing an API wrapper.

Solution 2:

I have the same problem. Initially, I tried to use Google Translate Ajax API, but since Google have deprecated API version 1 and make version 2 as paid service, my code stops working. Then, I decompiled Google Translate App, looked into the Smali code and got some hint about the logic inside it. Use this code, it works for me:

privatevoidcallGoogleTranslateApps(String word, String fromLang, String toLang) {
    Intent i = newIntent();

    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(newComponentName("com.google.android.apps.translate", "com.google.android.apps.translate.TranslateActivity"));
    startActivity(i);
}

Solution 3:

Phi Van Ngoc's answer was fantastic, thanks for that.

However it didn't work initially for me and after investigating the Translate apk, it looks like they've modified their file structure slightly, so the intent ComponentName should now be:

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

The difference is that "translation" has been added before "TranslateActivity"

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

Intenti=newIntent();
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(
    newComponentName(
        "com.google.android.apps.translate",
        "com.google.android.apps.translate.translation.TranslateActivity"));
startActivity(i);

Solution 4:

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

Intenti=newIntent();
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(newComponentName("com.google.android.apps.translate",
                "com.google.android.apps.translate.HomeActivity"));

Looks like this is a SEND intent with two additional (BTW, optional) 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".

For people that change the API with each new version it may look only reasonable to rename "key_text_input" to, say, just "text_input", so we will look forward to the next release...

To be on the safe side, I'd propose to set both Intent.EXTRA_TEXT and "key_text_input" to the same value.

Solution 5:

The Google Translate activity names tend to change over time which makes the code fragile if you hardcode them.

Here is an approach that works with the current version of google translate and will likely keep working with future updates (as long as the package name stays the same):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    for (ResolveInfo resolveInfo : getPackageManager().queryIntentActivities(newIntent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain"), 0)) {
        if (resolveInfo.activityInfo.packageName.equals("com.google.android.apps.translate")) {
            StringactivityName= resolveInfo.activityInfo.name;
            StringpackageName= resolveInfo.activityInfo.packageName;

            Intentintent=newIntent().setPackage(packageName)
                    .setClassName(packageName, activityName)
                    .setAction(Intent.ACTION_PROCESS_TEXT)
                    .setType("text/plain")
                    .putExtra(Intent.EXTRA_PROCESS_TEXT, "Nobody expects the Spanish Inquisition!")
                    .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, true);

            startActivity(intent);
        }
    }
} else {
    // >>> deprecated code from other answers goes here <<<
}

Post a Comment for "How To Use Google Translator App"