diff --git a/bom/pom.xml b/bom/pom.xml index 1fcb42a8..63eeeea0 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -91,7 +91,17 @@ quarkus-google-cloud-storage-deployment ${project.version} - + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate + ${project.version} + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate-deployment + ${project.version} + + io.quarkiverse.googlecloudservices quarkus-google-cloud-pubsub ${project.version} diff --git a/docs/modules/ROOT/pages/translate.adoc b/docs/modules/ROOT/pages/translate.adoc new file mode 100644 index 00000000..f4cb3f1f --- /dev/null +++ b/docs/modules/ROOT/pages/translate.adoc @@ -0,0 +1,81 @@ += Google Cloud Services - Translate + +This extension allows to inject a `com.google.cloud.translate.Translate` object inside your Quarkus application. + +== Bootstrapping the project + +First, we need a new project. Create a new project with the following command (replace the version placeholders with the correct ones): + +[source, shell script] +---- +mvn io.quarkus:quarkus-maven-plugin:${quarkusVersion}:create \ + -DprojectGroupId=org.acme \ + -DprojectArtifactId=translate-quickstart \ + -Dextensions="resteasy-jackson,io.quarkiverse.googlecloudservices:quarkus-google-cloud-translate:${googleCloudServicesVersion}" +cd translate-quickstart +---- + +This command generates a Maven project, importing the Google Cloud Translate extension. + +If you already have your Quarkus project configured, you can add the `quarkus-google-cloud-translate` extension to your project by running the following command in your project base directory: +[source, shell script] +---- +./mvnw quarkus:add-extension -Dextensions="io.quarkiverse.googlecloudservices:quarkus-google-cloud-translate:${googleCloudServicesVersion}" +---- + +This will add the following to your pom.xml: + +[source, xml] +---- + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate + ${googleCloudServicesVersion} + +---- + +== Some example + +This is an example usage of the extension: we create a REST resource with a single endpoint that queries the Translate API +with some english text and returns the French translation. + +[source, java] +---- +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import com.google.cloud.translate.Translate; +import com.google.cloud.translate.Translate.TranslateOption; +import com.google.cloud.translate.Translation; + +@Path("/translate") +public class TranslateResource { + @Inject + Translate translate; // Inject Translate + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String translate() { + + String content = "Quarkus World Tour 2021." + + "A virtual tour of Java User Groups creating a unique hands-on experience " + + "with access to Quarkus experts designed to help you get started with Java " + + "in a Kubernetes world."; + + Translation translation = + translate.translate( + content, + TranslateOption.sourceLanguage("en"), + TranslateOption.targetLanguage("fr"); + + return translation.getTranslatedText(); + } +} +---- diff --git a/pom.xml b/pom.xml index 0fa85eef..a019b2c4 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,8 @@ pubsub spanner storage - firestore + translate + firestore bigtable secret-manager integration-tests diff --git a/translate/README.md b/translate/README.md new file mode 100644 index 00000000..645224c6 --- /dev/null +++ b/translate/README.md @@ -0,0 +1,70 @@ +# Quarkiverse - Google Cloud Services - Translate + +This extension allows to inject a `com.google.cloud.translate.Translate` object inside your Quarkus application. + +## Bootstrapping the project + +First, we need a new project. Create a new project with the following command (replace the version placeholders with the correct ones): + +```shell script +mvn io.quarkus:quarkus-maven-plugin::create \ + -DprojectGroupId=org.acme \ + -DprojectArtifactId=bigquery-quickstart \ + -Dextensions="resteasy-jackson,io.quarkiverse.googlecloudservices:quarkus-google-cloud-translate:${googleCloudServicesVersion}" +cd translate-quickstart +``` + +This command generates a Maven project, importing the Google Cloud Translate extension. + +If you already have your Quarkus project configured, you can add the `quarkus-google-cloud-translate` extension to your project by running the following command in your project base directory: +```shell script +./mvnw quarkus:add-extension -Dextensions="io.quarkiverse.googlecloudservices:quarkus-google-cloud-translate:${googleCloudServicesVersion}" +``` + +This will add the following to your pom.xml: + +```xml + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate + ${googleCloudServicesVersion} + +``` + +## Some example + +This is an example usage of the extension: we create a REST resource with a single endpoint that gets the translation for `Quarkus says hello world!` sentence in french and returns it. + +```java +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import com.google.cloud.translate.Translate; +import com.google.cloud.translate.Translate.TranslateOption; +import com.google.cloud.translate.TranslateOptions; +import com.google.cloud.translate.Translation; + +@Path("/translate") +public class TranslateResource { + + @Inject + Translate translate;// Inject Translate + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String translate() { + + Translation translation = + translate.translate( + "Quarkus says hello world!", + TranslateOption.sourceLanguage("en"), + TranslateOption.targetLanguage("fr"); + + return translation.getTranslatedText(); // Return its content + } + +} +``` diff --git a/translate/deployment/pom.xml b/translate/deployment/pom.xml new file mode 100644 index 00000000..6bcae12a --- /dev/null +++ b/translate/deployment/pom.xml @@ -0,0 +1,46 @@ + + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate-parent + 0.12.0-SNAPSHOT + ../pom.xml + + 4.0.0 + + quarkus-google-cloud-translate-deployment + Quarkus - Google Cloud Services - Translate - Deployment + + + + io.quarkus + quarkus-core-deployment + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-common-deployment + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate + + + + + + + maven-compiler-plugin + + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + + \ No newline at end of file diff --git a/translate/deployment/src/main/java/io/quarkiverse/googlecloudservices/translate/deployment/TranslateBuildSteps.java b/translate/deployment/src/main/java/io/quarkiverse/googlecloudservices/translate/deployment/TranslateBuildSteps.java new file mode 100644 index 00000000..3d3ec35d --- /dev/null +++ b/translate/deployment/src/main/java/io/quarkiverse/googlecloudservices/translate/deployment/TranslateBuildSteps.java @@ -0,0 +1,20 @@ +package io.quarkiverse.googlecloudservices.translate.deployment; + +import io.quarkiverse.googlecloudservices.translate.runtime.TranslateProducer; +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.builditem.FeatureBuildItem; + +public class TranslateBuildSteps { + private static final String FEATURE = "google-cloud-translate"; + + @BuildStep + public FeatureBuildItem feature() { + return new FeatureBuildItem(FEATURE); + } + + @BuildStep + public AdditionalBeanBuildItem producer() { + return new AdditionalBeanBuildItem(TranslateProducer.class); + } +} diff --git a/translate/pom.xml b/translate/pom.xml new file mode 100644 index 00000000..c57f8b5b --- /dev/null +++ b/translate/pom.xml @@ -0,0 +1,21 @@ + + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-services-parent + 0.12.0-SNAPSHOT + ../pom.xml + + 4.0.0 + + quarkus-google-cloud-translate-parent + Quarkus - Google Cloud Services - Translation + pom + + + runtime + deployment + + + + \ No newline at end of file diff --git a/translate/runtime/pom.xml b/translate/runtime/pom.xml new file mode 100644 index 00000000..f836dfb8 --- /dev/null +++ b/translate/runtime/pom.xml @@ -0,0 +1,74 @@ + + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-translate-parent + 0.12.0-SNAPSHOT + ../pom.xml + + 4.0.0 + + quarkus-google-cloud-translate + Quarkus - Google Cloud Services - Translate - Runtime + Use Google Cloud Translate object translate service + + + + io.quarkiverse.googlecloudservices + quarkus-google-cloud-common + + + com.google.cloud + google-cloud-translate + + + commons-logging + commons-logging + + + javax.annotation + javax.annotation-api + + + org.checkerframework + checker-qual + + + + + + + + + io.quarkus + quarkus-bootstrap-maven-plugin + ${quarkus.version} + + + + extension-descriptor + + compile + + ${project.groupId}:${project.artifactId}-deployment:${project.version} + + + + + + + maven-compiler-plugin + + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + + \ No newline at end of file diff --git a/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateConfiguration.java b/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateConfiguration.java new file mode 100644 index 00000000..3234d57e --- /dev/null +++ b/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateConfiguration.java @@ -0,0 +1,17 @@ +package io.quarkiverse.googlecloudservices.translate.runtime; + +import java.util.Optional; + +import io.quarkus.runtime.annotations.ConfigItem; +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; + +@ConfigRoot(name = "google.cloud.translate", phase = ConfigPhase.RUN_TIME) +public class TranslateConfiguration { + /** + * Overrides the default service host. + * This is most commonly used for development or testing activities with a local Google Cloud Translate emulator instance. + */ + @ConfigItem + public Optional hostOverride; +} \ No newline at end of file diff --git a/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateProducer.java b/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateProducer.java new file mode 100644 index 00000000..a9482708 --- /dev/null +++ b/translate/runtime/src/main/java/io/quarkiverse/googlecloudservices/translate/runtime/TranslateProducer.java @@ -0,0 +1,22 @@ +package io.quarkiverse.googlecloudservices.translate.runtime; + +import java.io.IOException; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Default; +import javax.enterprise.inject.Produces; +import javax.inject.Singleton; + +import com.google.cloud.translate.Translate; +import com.google.cloud.translate.TranslateOptions; + +@ApplicationScoped +public class TranslateProducer { + + @Produces + @Singleton + @Default + public Translate translate() throws IOException { + return TranslateOptions.getDefaultInstance().getService(); + } +} diff --git a/translate/runtime/src/main/resources/META-INF/quarkus-extension.yaml b/translate/runtime/src/main/resources/META-INF/quarkus-extension.yaml new file mode 100644 index 00000000..ab811751 --- /dev/null +++ b/translate/runtime/src/main/resources/META-INF/quarkus-extension.yaml @@ -0,0 +1,10 @@ +--- +name: "Google Cloud Translate" +metadata: + keywords: + - "translate" + - "google cloud" + - "gcloud" + categories: + - "cloud" + status: "experimental"