use of com.google.cloud.translate.v3beta1.DetectLanguageResponse in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method detectLanguageOfText.
// [END translate_list_language_names_beta]
/**
* Detects the language of a given text.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param text - Text for detection
*/
// [START translate_detect_language_beta]
static DetectLanguageResponse detectLanguageOfText(String projectId, String location, String text) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
DetectLanguageRequest detectLanguageRequest = DetectLanguageRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setContent(text).build();
// Call the API
DetectLanguageResponse response = translationServiceClient.detectLanguage(detectLanguageRequest);
System.out.format("Detected Language Code: %s", response.getLanguages(0).getLanguageCode());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
Aggregations