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