5

I am making an android app for language translation and so far I have used voice recognizer intent to get the voice input into a string. Now I want to translate that string into another language and speak the translated text using the TTS Engine. I have created a separate translate_test file just for test use. I have been researching and know that API Key is required in Android Studio. So I have created the API Key and enabled the Google Cloud Translation API. Now I'm trying to import com.google.cloud.translate.Translation in my MainActivity but I'm getting this error:

error

I NEED 10 REPUTATION POINTS TO ALLOW THE IMAGE TO BE SHOWN.So all I can say is that the imported file does not exist.

I need help on how to include the cloud files. I have been researching online but still not able to find a tutorial or any information on how to include the cloud files in Android Studio. I have even read the docs. I need help and will be pleased if someone can give me some simple steps.

This is my MainActivity.java file:

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);
    }
}

This is my AndroidManifest.xml file:

<?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>

ShahiM
  • 3,068
  • 1
  • 27
  • 54
Amandeep Singh
  • 31
  • 1
  • 10

1 Answers1

6

I guess you followed the steps outlined here: Google Translate API Docs

But this doesn't work on Android as its mentioned in the docs:

Note: Google Cloud Java client libraries do not currently support Android.

So in order to get in working on Android, you have to make a REST API call using an HTTP request.

Read this for more info: Authenticating to the Cloud Translation API

Excerpts:

When making any Translation API request, pass your key as the value of a key parameter. For example:

POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY

Use a separate q parameter for each string. This example HTTP POST request sends two strings for translation:

POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY

{ 'q': 'Hello world', 'q': 'My name is Jeff', 'target': 'de' }

ShahiM
  • 3,068
  • 1
  • 27
  • 54
  • So we can't use Google Translate API in android app???? But do they support simple Java application file – Amandeep Singh Oct 12 '17 at 08:01
  • Like I'm thinking of creating a separate java file and translate in it and extract the string from that file and use it in the main activity to show it. Will this work??? – Amandeep Singh Oct 12 '17 at 08:07
  • The are referring to *Core Java* Applications. *Java on Android* is only a subset of *Core Java*. So all Java libraries may not work on Android. They have mentioned in the docs as a footnote that it does not support android as of now. So a HTTP request is your only option. – ShahiM Oct 12 '17 at 08:08
  • Yes. it should work. but you have to manually make an HTTP request to the API. `com.google.cloud.translate.Translation` wont work. – ShahiM Oct 12 '17 at 08:09
  • Can u maybe give me any sort of way to research this HTTP request. Like any web address or tutorial?? – Amandeep Singh Oct 12 '17 at 08:11
  • Try [this link](https://developer.android.com/training/volley/simple.html). It will get you started. Or - you can use the [Retrofit Library](http://square.github.io/retrofit) which will be a bit hard at first but is very convenient once you get the hang of it. – ShahiM Oct 12 '17 at 08:15
  • All right man Thanks a lot. I will try these things and see. Thanks again – Amandeep Singh Oct 12 '17 at 08:17
  • So do we need to write api key into code? How can we hide it? Because, they can get codes from apk to find the api key – hasbi Oct 12 '17 at 08:23
  • have a look at [this answer](https://stackoverflow.com/a/14572051/3894781) – ShahiM Oct 12 '17 at 08:44
  • dude I have been researching on this http request. I dont understand the use of json file. Like do we have to create one of our own and if we do how do we connect it with the java file – Amandeep Singh Oct 16 '17 at 05:13