import io.grpc.Channel;
import io.grpc.ChannelCredentials;
import io.grpc.ClientInterceptor;
import io.grpc.ClientInterceptors;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.TlsChannelCredentials;
// [END translate_v3_translate_text_0]
public class TranslateText {
public static void main(String args[]) throws Exception {
translateText();
}
// [START translate_v3_translate_text_1]
// Set and pass variables to overloaded translateText() method for translation.
public static void translateText() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "gcp-spsf-dev";
// Supported Languages:
https://cloud.google.com/translate/docs/languages String targetLanguage = "hi";
String text = "Hello Danial, How are you?";
System.out.println("Calling translaton");
translateText(projectId, targetLanguage, text);
}
// [END translate_v3_translate_text_1]
// [START translate_v3_translate_text_2]
// Translate text to target language.
public static void translateText(String projectId, String targetLanguage, String text)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
TranslationServiceSettings tsSettings = TranslationServiceSettings.newBuilder()
//.setEndpoint("
proxy.ups.com:443")
//.setEndpoint("
translate-u.p.googleapis.com:443")
//.setEndpoint("localhost:443")
//.setCredentialsProvider(FixedCredentialsProvider
.build();
TranslationServiceClient translationServiceClient = TranslationServiceClient.create(tsSettings);
//try (TranslationServiceClient client = TranslationServiceClient.create()) {
try (TranslationServiceClient client = translationServiceClient) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
LocationName parent = LocationName.of(projectId, "global");
/*
* ChannelCredentials credentials; credentials =
* TlsChannelCredentials.newBuilder() .trustManager(new
* File("<path-to-file>/cacert.pem")) .build(); ManagedChannel originChannel =
* Grpc.newChannelBuilderForAddress("
proxy.ups.com", 8080, credentials).build();
* ClientInterceptor interceptor = new HeaderClientInterceptor(); Channel
* channel = ClientInterceptors.intercept(originChannel, interceptor);
*
* stub = LogStreamerGrpc.newStub(channel);
*/
// Supported Mime Types:
https://cloud.google.com/translate/docs/supported-formats TranslateTextRequest request =
TranslateTextRequest.newBuilder()
.setParent(parent.toString())
.setMimeType("text/plain")
.setTargetLanguageCode(targetLanguage)
.addContents(text)
.build();
TranslateTextResponse response = client.translateText(request);
// Display the translation for each input text provided
for (Translation translation : response.getTranslationsList()) {
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
}
}
}
// [END translate_v3_translate_text_2]
}
// [END translate_v3_translate_text]
exception in thread "main" com.google.api.gax.rpc.UnavailableException: io.grpc.StatusRuntimeException: UNAVAILABLE: Failed ALPN negotiation: Unable to find compatible protocol