Search in sources :

Example 1 with DetectLanguageRequest

use of com.google.cloud.translate.v3beta1.DetectLanguageRequest 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);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) DetectLanguageResponse(com.google.cloud.translate.v3beta1.DetectLanguageResponse) DetectLanguageRequest(com.google.cloud.translate.v3beta1.DetectLanguageRequest) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

DetectLanguageRequest (com.google.cloud.translate.v3beta1.DetectLanguageRequest)1 DetectLanguageResponse (com.google.cloud.translate.v3beta1.DetectLanguageResponse)1 LocationName (com.google.cloud.translate.v3beta1.LocationName)1 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)1