Microsoft Azure Translator Text API In Android
In this tutorial, we’ll be using Microsoft Azure translator text API.
The Microsoft Azure Translator Text API allows developers to integrate text translation into their applications. In this article, we will discuss how to use the Translator Text API in an Android application.
To get started, we first need to create an Azure account and set up the Translator Text API service. Follow these steps:
- Go to the Azure portal and sign in with your credentials.
- Create a new Translator Text API resource.
- Once the resource is created, go to the “Keys and Endpoint” tab to retrieve the API key and endpoint URL.
Now, let’s integrate the Translator Text API into our Android application. Follow these steps:
- Open your Android Studio project and add the following dependencies to your app-level build. gradle file:
dependencies {
implementation 'com.microsoft.azure.cognitiveservices:azure-cognitiveservices-translatortext:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
}
2. In your Java class, import the required packages:
import com.microsoft.azure.cognitiveservices.translatortext.TranslatorTextClient;
import com.microsoft.azure.cognitiveservices.translatortext.TranslatorTextClientImpl;
import com.microsoft.azure.cognitiveservices.translatortext.models.TranslationResult;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
3. In the same Java class, add the following code to initialize the Translator Text API client:
final String subscriptionKey = "YOUR_SUBSCRIPTION_KEY_HERE";
final String endpoint = "YOUR_ENDPOINT_URL_HERE";
final TranslatorTextClient client = new TranslatorTextClientImpl(endpoint,
Credentials.basic("", subscriptionKey));
4. To translate a piece of text, add the following code:
String inputText = "Hello, world!";
String fromLanguage = "en";
String toLanguage = "fr";
client.translate(null, inputText, toLanguage, fromLanguage, null, null,
new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
ResponseBody responseBody = response.body();
if (responseBody != null) {
String jsonResponse = responseBody.string();
TranslationResult[] results = client.deserialize(jsonResponse,
TranslationResult[].class);
for (TranslationResult result : results) {
System.out.println(result.getTranslations().get(0).getText());
}
}
}
});
5. Run your application and test the translation feature.
In this article, we discussed how to integrate the Microsoft Azure Translator Text API into an Android application. With this API, developers can easily add text translation functionality to their Android apps.
Any suggestions and comments would be greatly appreciated. If you found this article helpful, please like and share it with others. Don’t forget to follow me to stay up-to-date on my latest articles. Thank you for taking the time to read this!