Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Google Translate support #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@
<artifactId>quarkus-google-cloud-storage-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-pubsub</artifactId>
<version>${project.version}</version>
Expand Down
81 changes: 81 additions & 0 deletions docs/modules/ROOT/pages/translate.adoc
Original file line number Diff line number Diff line change
@@ -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]
----
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate</artifactId>
<version>${googleCloudServicesVersion}</version>
</dependency>
----

== 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();
}
}
----
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<module>pubsub</module>
<module>spanner</module>
<module>storage</module>
<module>firestore</module>
<module>translate</module>
<module>firestore</module>
<module>bigtable</module>
<module>secret-manager</module>
<module>integration-tests</module>
Expand Down
70 changes: 70 additions & 0 deletions translate/README.md
Original file line number Diff line number Diff line change
@@ -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:<quarkusVersion>: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
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate</artifactId>
<version>${googleCloudServicesVersion}</version>
</dependency>
```

## 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
}

}
```
46 changes: 46 additions & 0 deletions translate/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-google-cloud-translate-deployment</artifactId>
<name>Quarkus - Google Cloud Services - Translate - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-common-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions translate/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-services-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-google-cloud-translate-parent</artifactId>
<name>Quarkus - Google Cloud Services - Translation</name>
<packaging>pom</packaging>

<modules>
<module>runtime</module>
<module>deployment</module>
</modules>


</project>
74 changes: 74 additions & 0 deletions translate/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-translate-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-google-cloud-translate</artifactId>
<name>Quarkus - Google Cloud Services - Translate - Runtime</name>
<description>Use Google Cloud Translate object translate service</description>

<dependencies>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-common</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
<version>${quarkus.version}</version>
<executions>
<execution>
<goals>
<goal>extension-descriptor</goal>
</goals>
<phase>compile</phase>
<configuration>
<deployment>${project.groupId}:${project.artifactId}-deployment:${project.version}
</deployment>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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<String> hostOverride;
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading