How to use Google Cloud Translation API in Android Studio?
Asked Answered
C

2

6

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>

Corneous answered 12/10, 2017 at 7:33 Comment(0)
F
9

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

But this doesn't work on Android as it's 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' }

Fierro answered 12/10, 2017 at 7:59 Comment(10)
So we can't use Google Translate API in android app???? But do they support simple Java application fileCorneous
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???Corneous
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.Fierro
Yes. it should work. but you have to manually make an HTTP request to the API. com.google.cloud.translate.Translation wont work.Fierro
Can u maybe give me any sort of way to research this HTTP request. Like any web address or tutorial??Corneous
Try this link. It will get you started. Or - you can use the Retrofit Library which will be a bit hard at first but is very convenient once you get the hang of it.Fierro
All right man Thanks a lot. I will try these things and see. Thanks againCorneous
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 keyCulbert
have a look at this answerFierro
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 fileCorneous
R
0

You can use ML Kit to translate text between languages. ML Kit can translation between more than 50 languages.

This API supports on demand dynamic model downloads. See this guide for more information.

Check out these links

https://developers.google.com/ml-kit/language/translation/android

and

https://youtu.be/_dWX0kk2JmA?si=mgUL6oBUfseWa1Zb

Raquelraquela answered 5/10, 2023 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.