use of com.google.cloud.security.privateca.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method batchTranslateText.
// [END translate_translate_text_beta]
/**
* Translates a batch of texts on GCS and stores the result in a GCS location.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param sourceUri - Google Cloud Storage URI. Location where text is stored.
* @param destinationUri - Google Cloud Storage URI where result will be stored.
*/
// [START translate_batch_translate_text_beta]
static BatchTranslateResponse batchTranslateText(String projectId, String location, String sourceUri, String destinationUri) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build();
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateTextRequest batchTranslateTextRequest = BatchTranslateTextRequest.newBuilder().setParent(locationName.toString()).setSourceLanguageCode("en").addTargetLanguageCodes("sr").addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
// Call the API
BatchTranslateResponse response = translationServiceClient.batchTranslateTextAsync(batchTranslateTextRequest).get(300, TimeUnit.SECONDS);
System.out.printf("Total Characters: %d\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.security.privateca.v1.LocationName 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);
}
}
use of com.google.cloud.security.privateca.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method listGlossary.
// [END translate_create_glossary_beta]
/**
* Lists all the glossaries for a given project.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param filter - criteria for listing glossaries.
*/
// [START translate_list_glossary_beta]
static ListGlossariesPagedResponse listGlossary(String projectId, String location, String filter) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
ListGlossariesPagedResponse response = translationServiceClient.listGlossaries(locationName.toString(), filter);
// Call the API
for (Glossary element : response.iterateAll()) {
System.out.format("Name: %s\n", element.getName());
System.out.format("Language Codes Set:\n");
System.out.format("Source Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(0));
System.out.format("Target Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(1));
System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.security.privateca.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method listSupportedLanguagesWithTarget.
// [END translate_list_codes_beta]
/**
* Lists all the supported language names and codes.
*
* @param projectId - Id of the project.
* @param location - location name.
*/
// [START translate_list_language_names_beta]
static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).setDisplayLanguageCode("en-US").build();
// Call the API
ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
SupportedLanguages response = future.get();
List<SupportedLanguage> languages = response.getLanguagesList();
for (SupportedLanguage language : languages) {
System.out.printf("Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.security.privateca.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method listSupportedLanguages.
/**
* Lists all the supported language codes.
*
* @param projectId - Id of the project.
* @param location - location name.
*/
// [START translate_list_codes_beta]
static SupportedLanguages listSupportedLanguages(String projectId, String location) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build();
// Call the API
ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
SupportedLanguages response = future.get();
List<SupportedLanguage> languages = response.getLanguagesList();
for (SupportedLanguage language : languages) {
System.out.printf("Code: %s\n", language.getLanguageCode());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
Aggregations