com.google.cloud.translate.Translate Java Examples
The following examples show how to use
com.google.cloud.translate.Translate.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DetectLanguageAndTranslate.java From google-cloud-java with Apache License 2.0 | 6 votes |
public static void main(String... args) { // Create a service object // // If no explicit credentials or API key are set, requests are authenticated using Application // Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY // environment variable Translate translate = TranslateOptions.getDefaultInstance().getService(); // Text of an "unknown" language to detect and then translate into English final String mysteriousText = "Hola Mundo"; // Detect the language of the mysterious text Detection detection = translate.detect(mysteriousText); String detectedLanguage = detection.getLanguage(); // Translate the mysterious text to English Translation translation = translate.translate( mysteriousText, TranslateOption.sourceLanguage(detectedLanguage), TranslateOption.targetLanguage("en")); System.out.println(translation.getTranslatedText()); }
Example #2
Source File: ITTranslateSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
@Test public void testTranslateTextWithModel() { // [START translate_text_with_model] Translation translation = translate.translate( "Hola Mundo!", Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de"), // Use "base" for standard edition, "nmt" for the premium model. Translate.TranslateOption.model("nmt")); System.out.printf("TranslatedText:\nText: %s\n", translation.getTranslatedText()); // [END translate_text_with_model] assertEquals("Hallo Welt!", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); }
Example #3
Source File: GoogleTranslateAuth.java From jbpm-work-items with Apache License 2.0 | 5 votes |
public Translate getTranslationService(String apiKey) throws Exception { try { return TranslateOptions.newBuilder().setApiKey(apiKey).build().getService(); } catch (Exception e) { throw new IllegalArgumentException(e); } }
Example #4
Source File: BackgroundContextListener.java From getting-started-java with Apache License 2.0 | 5 votes |
@Override public void contextInitialized(ServletContextEvent event) { String firestoreProjectId = System.getenv("FIRESTORE_CLOUD_PROJECT"); Firestore firestore = (Firestore) event.getServletContext().getAttribute("firestore"); if (firestore == null) { firestore = FirestoreOptions.getDefaultInstance().toBuilder() .setProjectId(firestoreProjectId) .build() .getService(); event.getServletContext().setAttribute("firestore", firestore); } Translate translate = (Translate) event.getServletContext().getAttribute("translate"); if (translate == null) { translate = TranslateOptions.getDefaultInstance().getService(); event.getServletContext().setAttribute("translate", translate); } Publisher publisher = (Publisher) event.getServletContext().getAttribute("publisher"); if (publisher == null) { try { String topicId = System.getenv("PUBSUB_TOPIC"); publisher = Publisher.newBuilder( ProjectTopicName.newBuilder() .setProject(firestoreProjectId) .setTopic(topicId) .build()) .build(); event.getServletContext().setAttribute("publisher", publisher); } catch (IOException e) { e.printStackTrace(); } } }
Example #5
Source File: QuickstartSample.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void main(String... args) throws Exception { // Instantiates a client Translate translate = TranslateOptions.getDefaultInstance().getService(); // The text to translate String text = "Hello, world!"; // Translates some text into Russian Translation translation = translate.translate( text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru")); System.out.printf("Text: %s%n", text); System.out.printf("Translation: %s%n", translation.getTranslatedText()); }
Example #6
Source File: TranslateExample.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Override public void run(Translate translate, Void arg) { List<Language> languages = translate.listSupportedLanguages(); System.out.println("Supported languages:"); for (Language language : languages) { System.out.println(language); } }
Example #7
Source File: TranslateExample.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Override public void run(Translate translate, List<String> texts) { List<Detection> detections = translate.detect(texts); if (texts.size() == 1) { System.out.println("Detected language is:"); } else { System.out.println("Detected languages are:"); } for (Detection detection : detections) { System.out.println(detection); } }
Example #8
Source File: TranslateExample.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Override public void run(Translate translate, List<String> texts) { List<Translation> translations = translate.translate(texts); if (texts.size() == 1) { System.out.println("Translation is:"); } else { System.out.println("Translations are:"); } for (Translation translation : translations) { System.out.println(translation); } }
Example #9
Source File: ITTranslateSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Test public void testTranslateTextListWithOptions() { // SNIPPET translateTextsWithOptions List<String> texts = new LinkedList<>(); texts.add("¡Hola Mundo!"); List<Translation> translations = translate.translate( texts, Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de")); // SNIPPET translateTextsWithOptions Translation translation = translations.get(0); assertEquals("Hallo Welt!", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); }
Example #10
Source File: TranslateServlet.java From getting-started-java with Apache License 2.0 | 4 votes |
/** * Handle a posted message from Pubsub. * * @param req The message Pubsub posts to this process. * @param resp Not used. */ @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // Block requests that don't contain the proper verification token. String pubsubVerificationToken = PUBSUB_VERIFICATION_TOKEN; if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } // [START getting_started_background_translate_string] String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator())); PubSubMessage pubsubMessage = gson.fromJson(body, PubSubMessage.class); TranslateMessage message = pubsubMessage.getMessage(); // Use Translate service client to translate the message. Translate translate = (Translate) this.getServletContext().getAttribute("translate"); message.setData(decode(message.getData())); Translation translation = translate.translate( message.getData(), Translate.TranslateOption.sourceLanguage(message.getAttributes().getSourceLang()), Translate.TranslateOption.targetLanguage(message.getAttributes().getTargetLang())); // [END getting_started_background_translate_string] message.setTranslatedText(translation.getTranslatedText()); try { // [START getting_started_background_translate] // Use Firestore service client to store the translation in Firestore. Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore"); CollectionReference translations = firestore.collection("translations"); ApiFuture<WriteResult> setFuture = translations.document().set(message, SetOptions.merge()); setFuture.get(); resp.getWriter().write(translation.getTranslatedText()); // [END getting_started_background_translate] } catch (InterruptedException | ExecutionException e) { throw new ServletException("Exception storing data in Firestore.", e); } }
Example #11
Source File: TranslateExample.java From google-cloud-java with Apache License 2.0 | votes |
abstract void run(Translate translate, T arg) throws Exception;