Search in sources :

Example 11 with Translate

use of com.google.cloud.translate.Translate in project getting-started-java by GoogleCloudPlatform.

the class BackgroundContextListener method contextInitialized.

@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);
    }
    String topicId = System.getenv("PUBSUB_TOPIC");
    TopicName topicName = TopicName.of(firestoreProjectId, topicId);
    Publisher publisher = (Publisher) event.getServletContext().getAttribute("publisher");
    if (publisher == null) {
        try {
            publisher = Publisher.newBuilder(topicName).build();
            event.getServletContext().setAttribute("publisher", publisher);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Firestore(com.google.cloud.firestore.Firestore) Publisher(com.google.cloud.pubsub.v1.Publisher) IOException(java.io.IOException) Translate(com.google.cloud.translate.Translate) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

Translate (com.google.cloud.translate.Translate)11 Translation (com.google.cloud.translate.Translation)7 Firestore (com.google.cloud.firestore.Firestore)2 Detection (com.google.cloud.translate.Detection)2 TranslateOption (com.google.cloud.translate.Translate.TranslateOption)2 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Language (com.google.cloud.translate.Language)1 LanguageListOption (com.google.cloud.translate.Translate.LanguageListOption)1 TranslateException (com.google.cloud.translate.TranslateException)1 TranslateOptions (com.google.cloud.translate.TranslateOptions)1 TopicName (com.google.pubsub.v1.TopicName)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1