我正在制作一个用于语言翻译的Android应用程序,到目前为止我已经使用语音识别器意图将语音输入转换为字符串。现在我想将该字符串翻译成另一种语言,并使用 TTS 引擎说出翻译后的文本。 我创建了一个单独的
translate_test
文件仅供测试使用。我一直在研究并知道Android Studio中需要API Key。因此,我创建了 API 密钥并启用了 Google Cloud Translation API。
现在我尝试在 MainActivity 中导入 com.google.cloud.translate.Translation
但出现此错误:
我需要 10 个信誉点才能显示图像。所以我只能说导入的文件不存在。
我需要有关如何包含云文件的帮助。我一直在网上研究,但仍然找不到有关如何在 Android Studio 中包含云文件的教程或任何信息。我什至读过文档。我需要帮助,如果有人能给我一些简单的步骤,我会很高兴。
这是我的 MainActivity.java 文件:
package com.example.aman.translate_test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.cloud.translate.Translation;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView);
}
}
这是我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aman.translate_test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-library android:name="com.google.cloud.translate.Translate" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/api_key"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我猜您遵循了此处概述的步骤:Google Translate API 文档
但这在 Android 上不起作用,正如文档中提到的:
注意:Google Cloud Java 客户端库当前不支持 Android。
因此,为了在 Android 上工作,您必须使用 HTTP 请求进行 REST API 调用。
阅读此内容以获取更多信息:向 Cloud Translation API 进行身份验证
摘录:
发出任何翻译 API 请求时,将您的密钥作为关键参数的值传递。例如:
POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY
为每个字符串使用单独的 q 参数。此示例 HTTP POST 请求发送两个字符串进行翻译:
POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY
{ 'q': 'Hello world', 'q': 'My name is Jeff', 'target': 'de' }
您可以使用 ML Kit 在语言之间翻译文本。 ML Kit 可以在 50 多种语言之间进行翻译。
该API支持按需动态模型下载。请参阅本指南了解更多信息。
查看这些链接
https://developers.google.com/ml-kit/language/translation/android
和