如何使用谷歌翻译应用程序

pra*_*w_z 9 android google-translate

我编写了关于字典句子的程序,我想在我的应用程序中有"google translator"应用程序的功能

我该如何使用它,我应该导入任何东西吗?

Fel*_*lix 7

据我所知,谷歌翻译Android应用程序没有公开Intent你可以使用的任何标准(这是一个很小的,但它同时很奇怪.你认为谷歌会鼓励应用程序之间的这种类型的交互..无论如何).

但是,Google似乎已经通过网络服务打开了翻译API .这样,您就可以自己进行翻译并在应用中显示.这是一项更多的工作,但它应该做的工作.

如果你不想编写API包装器,你可以查看google-api-translate-java.

  • 谷歌翻译现在是一项付费服务​​...所以能够"打电话"翻译应用程序的服务会很好...... (2认同)

小智 7

我也有同样的问题.最初,我尝试使用谷歌翻译Ajax API,但由于谷歌已弃用API版本1并将版本2作为付费服务,我的代码停止工作.然后,我反编译了谷歌翻译应用程序,查看了Smali代码并得到了一些关于其内部逻辑的提示.使用此代码,它适用于我:

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);
}
Run Code Online (Sandbox Code Playgroud)


nic*_*oon 6

Phi Van Ngoc的答案太棒了,谢谢你.

然而它最初对我来说并不起作用,在调查了Translate apk后,看起来它们稍微修改了它们的文件结构,因此ComponentName现在应该是:

i.setComponent(
    new ComponentName(
        "com.google.android.apps.translate",
        "com.google.android.apps.translate.translation.TranslateActivity"));
Run Code Online (Sandbox Code Playgroud)

区别在于"TranslateActivity"之前添加了"翻译"

所以我的最终版本,包括从西班牙语到英语的硬编码翻译,是:

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);
Run Code Online (Sandbox Code Playgroud)


184*_*615 5

我的天啊!他们又一次改变了!它们使它看起来更合理,但与之前的版本不兼容.

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"));
Run Code Online (Sandbox Code Playgroud)

看起来这是一个SEND意图,有两个额外的(BTW,可选)参数,"to"和"from".

有一个问题:"key_text_input"优先于Intent.EXTRA_TEXT,"to"和"from"只能使用"key_text_input".

对于那些使用每个新版本更改API的人来说,将"key_text_input"重命名为"text_input"只是合理的,所以我们期待下一个版本......

为了安全起见,我建议将Intent.EXTRA_TEXT和"key_text_input"设置为相同的值.