xref: /aosp_15_r20/external/google-cloud-java/java-translate/README.md (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1*55e87721SMatt Gilbride# Google Cloud Translation Client for Java
2*55e87721SMatt Gilbride
3*55e87721SMatt GilbrideJava idiomatic client for [Cloud Translation][product-docs].
4*55e87721SMatt Gilbride
5*55e87721SMatt Gilbride[![Maven][maven-version-image]][maven-version-link]
6*55e87721SMatt Gilbride![Stability][stability-image]
7*55e87721SMatt Gilbride
8*55e87721SMatt Gilbride- [Product Documentation][product-docs]
9*55e87721SMatt Gilbride- [Client Library Documentation][javadocs]
10*55e87721SMatt Gilbride
11*55e87721SMatt Gilbride
12*55e87721SMatt Gilbride## Quickstart
13*55e87721SMatt Gilbride
14*55e87721SMatt Gilbride
15*55e87721SMatt GilbrideIf you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
16*55e87721SMatt Gilbride
17*55e87721SMatt Gilbride```xml
18*55e87721SMatt Gilbride<dependencyManagement>
19*55e87721SMatt Gilbride  <dependencies>
20*55e87721SMatt Gilbride    <dependency>
21*55e87721SMatt Gilbride      <groupId>com.google.cloud</groupId>
22*55e87721SMatt Gilbride      <artifactId>libraries-bom</artifactId>
23*55e87721SMatt Gilbride      <version>26.12.0</version>
24*55e87721SMatt Gilbride      <type>pom</type>
25*55e87721SMatt Gilbride      <scope>import</scope>
26*55e87721SMatt Gilbride    </dependency>
27*55e87721SMatt Gilbride  </dependencies>
28*55e87721SMatt Gilbride</dependencyManagement>
29*55e87721SMatt Gilbride
30*55e87721SMatt Gilbride<dependencies>
31*55e87721SMatt Gilbride  <dependency>
32*55e87721SMatt Gilbride    <groupId>com.google.cloud</groupId>
33*55e87721SMatt Gilbride    <artifactId>google-cloud-translate</artifactId>
34*55e87721SMatt Gilbride  </dependency>
35*55e87721SMatt Gilbride```
36*55e87721SMatt Gilbride
37*55e87721SMatt GilbrideIf you are using Maven without the BOM, add this to your dependencies:
38*55e87721SMatt Gilbride
39*55e87721SMatt Gilbride<!-- {x-version-update-start:google-cloud-translate:released} -->
40*55e87721SMatt Gilbride
41*55e87721SMatt Gilbride```xml
42*55e87721SMatt Gilbride<dependency>
43*55e87721SMatt Gilbride  <groupId>com.google.cloud</groupId>
44*55e87721SMatt Gilbride  <artifactId>google-cloud-translate</artifactId>
45*55e87721SMatt Gilbride  <version>2.17.0</version>
46*55e87721SMatt Gilbride</dependency>
47*55e87721SMatt Gilbride```
48*55e87721SMatt Gilbride
49*55e87721SMatt GilbrideIf you are using Gradle without BOM, add this to your dependencies:
50*55e87721SMatt Gilbride
51*55e87721SMatt Gilbride```Groovy
52*55e87721SMatt Gilbrideimplementation 'com.google.cloud:google-cloud-translate:2.17.0'
53*55e87721SMatt Gilbride```
54*55e87721SMatt Gilbride
55*55e87721SMatt GilbrideIf you are using SBT, add this to your dependencies:
56*55e87721SMatt Gilbride
57*55e87721SMatt Gilbride```Scala
58*55e87721SMatt GilbridelibraryDependencies += "com.google.cloud" % "google-cloud-translate" % "2.17.0"
59*55e87721SMatt Gilbride```
60*55e87721SMatt Gilbride<!-- {x-version-update-end} -->
61*55e87721SMatt Gilbride
62*55e87721SMatt Gilbride## Authentication
63*55e87721SMatt Gilbride
64*55e87721SMatt GilbrideSee the [Authentication][authentication] section in the base directory's README.
65*55e87721SMatt Gilbride
66*55e87721SMatt Gilbride## Authorization
67*55e87721SMatt Gilbride
68*55e87721SMatt GilbrideThe client application making API calls must be granted [authorization scopes][auth-scopes] required for the desired Cloud Translation APIs, and the authenticated principal must have the [IAM role(s)][predefined-iam-roles] required to access GCP resources using the Cloud Translation API calls.
69*55e87721SMatt Gilbride
70*55e87721SMatt Gilbride## Getting Started
71*55e87721SMatt Gilbride
72*55e87721SMatt Gilbride### Prerequisites
73*55e87721SMatt Gilbride
74*55e87721SMatt GilbrideYou will need a [Google Cloud Platform Console][developer-console] project with the Cloud Translation [API enabled][enable-api].
75*55e87721SMatt GilbrideYou will need to [enable billing][enable-billing] to use Google Cloud Translation.
76*55e87721SMatt Gilbride[Follow these instructions][create-project] to get your project set up. You will also need to set up the local development environment by
77*55e87721SMatt Gilbride[installing the Google Cloud Command Line Interface][cloud-cli] and running the following commands in command line:
78*55e87721SMatt Gilbride`gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.
79*55e87721SMatt Gilbride
80*55e87721SMatt Gilbride### Installation and setup
81*55e87721SMatt Gilbride
82*55e87721SMatt GilbrideYou'll need to obtain the `google-cloud-translate` library.  See the [Quickstart](#quickstart) section
83*55e87721SMatt Gilbrideto add `google-cloud-translate` as a dependency in your code.
84*55e87721SMatt Gilbride
85*55e87721SMatt Gilbride## About Cloud Translation
86*55e87721SMatt Gilbride
87*55e87721SMatt Gilbride
88*55e87721SMatt Gilbride[Cloud Translation][product-docs] can dynamically translate text between thousands of language pairs. Translation lets websites and programs programmatically integrate with the translation service.
89*55e87721SMatt Gilbride
90*55e87721SMatt GilbrideSee the [Cloud Translation client library docs][javadocs] to learn how to
91*55e87721SMatt Gilbrideuse this Cloud Translation Client Library.
92*55e87721SMatt Gilbride
93*55e87721SMatt Gilbride
94*55e87721SMatt Gilbride### Example Application
95*55e87721SMatt Gilbride
96*55e87721SMatt Gilbride[`TranslateExample`](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/TranslateExample.java)
97*55e87721SMatt Gilbrideis a simple command line interface that provides some of Google Translation's functionality.
98*55e87721SMatt Gilbride
99*55e87721SMatt Gilbride#### Creating an authorized service object
100*55e87721SMatt GilbrideTo make authenticated requests to Google Translation, you must create a service object with
101*55e87721SMatt Gilbridecredentials or use an API key. The simplest way to authenticate is to use
102*55e87721SMatt Gilbride[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
103*55e87721SMatt GilbrideThese credentials are automatically inferred from your environment, so you only need the following
104*55e87721SMatt Gilbridecode to create your service object:
105*55e87721SMatt Gilbride
106*55e87721SMatt Gilbride```java
107*55e87721SMatt Gilbrideimport com.google.cloud.translate.Translate;
108*55e87721SMatt Gilbrideimport com.google.cloud.translate.TranslateOptions;
109*55e87721SMatt Gilbride
110*55e87721SMatt GilbrideTranslate translate = TranslateOptions.getDefaultInstance().getService();
111*55e87721SMatt Gilbride```
112*55e87721SMatt Gilbride
113*55e87721SMatt GilbrideNotice that this code can be also used with an API key. By default, an API key is looked for in the
114*55e87721SMatt Gilbride`GOOGLE_API_KEY` environment variable. Once the API key is set, you can make API calls by invoking
115*55e87721SMatt Gilbridemethods on the Translation service created via `TranslateOptions.getDefaultInstance().getService()`.
116*55e87721SMatt Gilbride
117*55e87721SMatt GilbrideYou can also explicitly set the API key as follows:
118*55e87721SMatt Gilbride```java
119*55e87721SMatt GilbrideTranslate translate = TranslateOptions.newBuilder().setApiKey("myKey").build().getService();
120*55e87721SMatt Gilbride```
121*55e87721SMatt Gilbride
122*55e87721SMatt Gilbride#### Detecting language
123*55e87721SMatt GilbrideWith Google Translation you can detect the language of some text. The service will provide you with
124*55e87721SMatt Gilbridethe code of the detected language and a level of confidence.
125*55e87721SMatt Gilbride
126*55e87721SMatt GilbrideAdd the following import at the top of your file:
127*55e87721SMatt Gilbride
128*55e87721SMatt Gilbride```java
129*55e87721SMatt Gilbrideimport com.google.cloud.translate.Detection;
130*55e87721SMatt Gilbride```
131*55e87721SMatt Gilbride
132*55e87721SMatt GilbrideThen pick a text sample:
133*55e87721SMatt Gilbride
134*55e87721SMatt Gilbride```java
135*55e87721SMatt Gilbridefinal String mysteriousText = "Hola Mundo";
136*55e87721SMatt Gilbride```
137*55e87721SMatt Gilbride
138*55e87721SMatt GilbrideThen add the following code to detect the text's language:
139*55e87721SMatt Gilbride
140*55e87721SMatt Gilbride```java
141*55e87721SMatt GilbrideDetection detection = translate.detect(mysteriousText);
142*55e87721SMatt GilbrideString detectedLanguage = detection.getLanguage();
143*55e87721SMatt Gilbride```
144*55e87721SMatt Gilbride#### Translating text
145*55e87721SMatt Gilbride
146*55e87721SMatt GilbrideGoogle translation allows you to translate some text. When translating one or more texts you can
147*55e87721SMatt Gilbrideeither provide the source language or let the service detect it for you.
148*55e87721SMatt Gilbride
149*55e87721SMatt GilbrideAdd the following imports at the top of your file:
150*55e87721SMatt Gilbride
151*55e87721SMatt Gilbride```java
152*55e87721SMatt Gilbrideimport com.google.cloud.translate.Translate.TranslateOption;
153*55e87721SMatt Gilbrideimport com.google.cloud.translate.Translation;
154*55e87721SMatt Gilbride```
155*55e87721SMatt Gilbride
156*55e87721SMatt GilbrideThen add the following code to translate the text, specifying the previously detected language (`detectedLanguage`) as its source language and English as the target language (providing the source language is optional, if it is not specified the service will try to detect it automatically):
157*55e87721SMatt Gilbride
158*55e87721SMatt Gilbride```java
159*55e87721SMatt GilbrideTranslation translation = translate.translate(
160*55e87721SMatt Gilbride    mysteriousText,
161*55e87721SMatt Gilbride    TranslateOption.sourceLanguage(detectedLanguage),
162*55e87721SMatt Gilbride    TranslateOption.targetLanguage("en"));
163*55e87721SMatt Gilbride```
164*55e87721SMatt Gilbride
165*55e87721SMatt Gilbride#### Complete source code
166*55e87721SMatt Gilbride
167*55e87721SMatt GilbrideIn
168*55e87721SMatt Gilbride[DetectLanguageAndTranslate.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/DetectLanguageAndTranslate.java)
169*55e87721SMatt Gilbridewe put together all the code shown above into one program. The program assumes that either Application
170*55e87721SMatt GilbrideDefault Credentials or a valid API key are available.
171*55e87721SMatt Gilbride
172*55e87721SMatt Gilbride
173*55e87721SMatt Gilbride
174*55e87721SMatt Gilbride
175*55e87721SMatt Gilbride## Troubleshooting
176*55e87721SMatt Gilbride
177*55e87721SMatt GilbrideTo get help, follow the instructions in the [shared Troubleshooting document][troubleshooting].
178*55e87721SMatt Gilbride
179*55e87721SMatt Gilbride## Transport
180*55e87721SMatt Gilbride
181*55e87721SMatt GilbrideCloud Translation uses both gRPC and HTTP/JSON for the transport layer.
182*55e87721SMatt Gilbride
183*55e87721SMatt Gilbride## Supported Java Versions
184*55e87721SMatt Gilbride
185*55e87721SMatt GilbrideJava 8 or above is required for using this client.
186*55e87721SMatt Gilbride
187*55e87721SMatt GilbrideGoogle's Java client libraries,
188*55e87721SMatt Gilbride[Google Cloud Client Libraries][cloudlibs]
189*55e87721SMatt Gilbrideand
190*55e87721SMatt Gilbride[Google Cloud API Libraries][apilibs],
191*55e87721SMatt Gilbridefollow the
192*55e87721SMatt Gilbride[Oracle Java SE support roadmap][oracle]
193*55e87721SMatt Gilbride(see the Oracle Java SE Product Releases section).
194*55e87721SMatt Gilbride
195*55e87721SMatt Gilbride### For new development
196*55e87721SMatt Gilbride
197*55e87721SMatt GilbrideIn general, new feature development occurs with support for the lowest Java
198*55e87721SMatt GilbrideLTS version covered by  Oracle's Premier Support (which typically lasts 5 years
199*55e87721SMatt Gilbridefrom initial General Availability). If the minimum required JVM for a given
200*55e87721SMatt Gilbridelibrary is changed, it is accompanied by a [semver][semver] major release.
201*55e87721SMatt Gilbride
202*55e87721SMatt GilbrideJava 11 and (in September 2021) Java 17 are the best choices for new
203*55e87721SMatt Gilbridedevelopment.
204*55e87721SMatt Gilbride
205*55e87721SMatt Gilbride### Keeping production systems current
206*55e87721SMatt Gilbride
207*55e87721SMatt GilbrideGoogle tests its client libraries with all current LTS versions covered by
208*55e87721SMatt GilbrideOracle's Extended Support (which typically lasts 8 years from initial
209*55e87721SMatt GilbrideGeneral Availability).
210*55e87721SMatt Gilbride
211*55e87721SMatt Gilbride#### Legacy support
212*55e87721SMatt Gilbride
213*55e87721SMatt GilbrideGoogle's client libraries support legacy versions of Java runtimes with long
214*55e87721SMatt Gilbrideterm stable libraries that don't receive feature updates on a best efforts basis
215*55e87721SMatt Gilbrideas it may not be possible to backport all patches.
216*55e87721SMatt Gilbride
217*55e87721SMatt GilbrideGoogle provides updates on a best efforts basis to apps that continue to use
218*55e87721SMatt GilbrideJava 7, though apps might need to upgrade to current versions of the library
219*55e87721SMatt Gilbridethat supports their JVM.
220*55e87721SMatt Gilbride
221*55e87721SMatt Gilbride#### Where to find specific information
222*55e87721SMatt Gilbride
223*55e87721SMatt GilbrideThe latest versions and the supported Java versions are identified on
224*55e87721SMatt Gilbridethe individual GitHub repository `github.com/GoogleAPIs/java-SERVICENAME`
225*55e87721SMatt Gilbrideand on [google-cloud-java][g-c-j].
226*55e87721SMatt Gilbride
227*55e87721SMatt Gilbride## Versioning
228*55e87721SMatt Gilbride
229*55e87721SMatt Gilbride
230*55e87721SMatt GilbrideThis library follows [Semantic Versioning](http://semver.org/).
231*55e87721SMatt Gilbride
232*55e87721SMatt Gilbride
233*55e87721SMatt Gilbride
234*55e87721SMatt Gilbride## Contributing
235*55e87721SMatt Gilbride
236*55e87721SMatt Gilbride
237*55e87721SMatt GilbrideContributions to this library are always welcome and highly encouraged.
238*55e87721SMatt Gilbride
239*55e87721SMatt GilbrideSee [CONTRIBUTING][contributing] for more information how to get started.
240*55e87721SMatt Gilbride
241*55e87721SMatt GilbridePlease note that this project is released with a Contributor Code of Conduct. By participating in
242*55e87721SMatt Gilbridethis project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more
243*55e87721SMatt Gilbrideinformation.
244*55e87721SMatt Gilbride
245*55e87721SMatt Gilbride
246*55e87721SMatt Gilbride## License
247*55e87721SMatt Gilbride
248*55e87721SMatt GilbrideApache 2.0 - See [LICENSE][license] for more information.
249*55e87721SMatt Gilbride
250*55e87721SMatt Gilbride## CI Status
251*55e87721SMatt Gilbride
252*55e87721SMatt GilbrideJava Version | Status
253*55e87721SMatt Gilbride------------ | ------
254*55e87721SMatt GilbrideJava 8 | [![Kokoro CI][kokoro-badge-image-2]][kokoro-badge-link-2]
255*55e87721SMatt GilbrideJava 8 OSX | [![Kokoro CI][kokoro-badge-image-3]][kokoro-badge-link-3]
256*55e87721SMatt GilbrideJava 8 Windows | [![Kokoro CI][kokoro-badge-image-4]][kokoro-badge-link-4]
257*55e87721SMatt GilbrideJava 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5]
258*55e87721SMatt Gilbride
259*55e87721SMatt GilbrideJava is a registered trademark of Oracle and/or its affiliates.
260*55e87721SMatt Gilbride
261*55e87721SMatt Gilbride[product-docs]: https://cloud.google.com/translate/docs/
262*55e87721SMatt Gilbride[javadocs]: https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/overview
263*55e87721SMatt Gilbride[kokoro-badge-image-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.svg
264*55e87721SMatt Gilbride[kokoro-badge-link-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.html
265*55e87721SMatt Gilbride[kokoro-badge-image-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.svg
266*55e87721SMatt Gilbride[kokoro-badge-link-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.html
267*55e87721SMatt Gilbride[kokoro-badge-image-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.svg
268*55e87721SMatt Gilbride[kokoro-badge-link-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.html
269*55e87721SMatt Gilbride[kokoro-badge-image-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.svg
270*55e87721SMatt Gilbride[kokoro-badge-link-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.html
271*55e87721SMatt Gilbride[kokoro-badge-image-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.svg
272*55e87721SMatt Gilbride[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.html
273*55e87721SMatt Gilbride[stability-image]: https://img.shields.io/badge/stability-stable-green
274*55e87721SMatt Gilbride[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-translate.svg
275*55e87721SMatt Gilbride[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-translate/2.15.0
276*55e87721SMatt Gilbride[authentication]: https://github.com/googleapis/google-cloud-java#authentication
277*55e87721SMatt Gilbride[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
278*55e87721SMatt Gilbride[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
279*55e87721SMatt Gilbride[iam-policy]: https://cloud.google.com/iam/docs/overview#cloud-iam-policy
280*55e87721SMatt Gilbride[developer-console]: https://console.developers.google.com/
281*55e87721SMatt Gilbride[create-project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects
282*55e87721SMatt Gilbride[cloud-cli]: https://cloud.google.com/cli
283*55e87721SMatt Gilbride[troubleshooting]: https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md
284*55e87721SMatt Gilbride[contributing]: https://github.com/googleapis/google-cloud-java/blob/main/CONTRIBUTING.md
285*55e87721SMatt Gilbride[code-of-conduct]: https://github.com/googleapis/google-cloud-java/blob/main/CODE_OF_CONDUCT.md#contributor-code-of-conduct
286*55e87721SMatt Gilbride[license]: https://github.com/googleapis/google-cloud-java/blob/main/LICENSE
287*55e87721SMatt Gilbride[enable-billing]: https://cloud.google.com/apis/docs/getting-started#enabling_billing
288*55e87721SMatt Gilbride[enable-api]: https://console.cloud.google.com/flows/enableapi?apiid=translation.googleapis.com
289*55e87721SMatt Gilbride[libraries-bom]: https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM
290*55e87721SMatt Gilbride[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
291*55e87721SMatt Gilbride
292*55e87721SMatt Gilbride[semver]: https://semver.org/
293*55e87721SMatt Gilbride[cloudlibs]: https://cloud.google.com/apis/docs/client-libraries-explained
294*55e87721SMatt Gilbride[apilibs]: https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries
295*55e87721SMatt Gilbride[oracle]: https://www.oracle.com/java/technologies/java-se-support-roadmap.html
296*55e87721SMatt Gilbride[g-c-j]: http://github.com/googleapis/google-cloud-java
297