|
| 1 | +Google Cloud Java Client for Translate |
| 2 | +==================================== |
| 3 | + |
| 4 | +Java idiomatic client for [Google Translate](https://cloud.google.com/translate/). |
| 5 | + |
| 6 | +[](https://travis-ci.org/GoogleCloudPlatform/gcloud-java) |
| 7 | +[](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master) |
| 8 | +[]( https://img.shields.io/maven-central/v/com.google.cloud/gcloud-java-translate.svg) |
| 9 | +[](https://www.codacy.com/app/mziccard/gcloud-java) |
| 10 | +[](https://www.versioneye.com/user/projects/56bd8ee72a29ed002d2b0969) |
| 11 | + |
| 12 | +- [Homepage](https://googlecloudplatform.github.io/gcloud-java/) |
| 13 | +- [API Documentation](http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/translate/package-summary.html) |
| 14 | + |
| 15 | +> Note: This client is a work-in-progress, and may occasionally |
| 16 | +> make backwards-incompatible changes. |
| 17 | +
|
| 18 | +Quickstart |
| 19 | +---------- |
| 20 | +If you are using Maven, add this to your pom.xml file |
| 21 | +```xml |
| 22 | +<dependency> |
| 23 | + <groupId>com.google.cloud</groupId> |
| 24 | + <artifactId>gcloud-java-translate</artifactId> |
| 25 | + <version>0.2.7</version> |
| 26 | +</dependency> |
| 27 | +``` |
| 28 | +If you are using Gradle, add this to your dependencies |
| 29 | +```Groovy |
| 30 | +compile 'com.google.cloud:gcloud-java-translate:0.2.7' |
| 31 | +``` |
| 32 | +If you are using SBT, add this to your dependencies |
| 33 | +```Scala |
| 34 | +libraryDependencies += "com.google.cloud" % "gcloud-java-translate" % "0.2.7" |
| 35 | +``` |
| 36 | + |
| 37 | +Example Application |
| 38 | +------------------- |
| 39 | + |
| 40 | +[`TranslateExample`](../gcloud-java-examples/src/main/java/com/google/cloud/examples/translate/TranslateExample.java) is a simple command line interface that provides some of Google Translate's functionality. Read more about using the application on the [`TranslateExample` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/cloud/examples/translate/TranslateExample.html). |
| 41 | + |
| 42 | +Authentication |
| 43 | +-------------- |
| 44 | + |
| 45 | +Google Translate requires an API key to be passed with every request. For instructions on how to |
| 46 | +get an API key follow the [Translate quickstart](https://cloud.google.com/translate/v2/quickstart). |
| 47 | + |
| 48 | +About Google Translate |
| 49 | +-------------------- |
| 50 | + |
| 51 | +[Google Translate][google-translate] provides a simple programmatic interface for translating an |
| 52 | +arbitrary string into any supported language. Translate is highly responsive, so websites and |
| 53 | +applications can integrate with Translate API for fast, dynamic translation of source text from the |
| 54 | +source language to a target language (e.g., French to English). Language detection is also |
| 55 | +available In cases where the source language is unknown. |
| 56 | + |
| 57 | +See the [Translate quickstart](https://cloud.google.com/translate/v2/quickstart) for more details |
| 58 | +on how to activate Google Translate for your project. |
| 59 | + |
| 60 | +See the ``gcloud-java`` API [Translate documentation][translate-api] to learn how to interact with |
| 61 | +the Google Translate using this Client Library. |
| 62 | + |
| 63 | +Getting Started |
| 64 | +--------------- |
| 65 | +#### Prerequisites |
| 66 | +For this tutorial, you need a [Google Developers Console](https://console.developers.google.com/) |
| 67 | +project with "Translate API" enabled via the console's API Manager. You will also need a to enable |
| 68 | +billing via the [Google Developers Console](https://console.developers.google.com/) project and to |
| 69 | +retrieve an API key. See [Translate quickstart](https://cloud.google.com/translate/v2/quickstart) |
| 70 | +for more details. |
| 71 | + |
| 72 | +#### Installation and setup |
| 73 | +You'll need to obtain the `gcloud-java-translate` library. See the [Quickstart](#quickstart) |
| 74 | +section to add `gcloud-java-translate` as a dependency in your code. |
| 75 | + |
| 76 | +#### Creating an authorized service object |
| 77 | +To make authenticated requests to Google Translates, you must create a service object with an API |
| 78 | +key. By default, API key is looked for in the `GOOGLE_API_KEY` environment variable. Once the API |
| 79 | +key is set, you can make API calls by invoking methods on the Translate service object. To create a |
| 80 | +service object, given that `GOOGLE_API_KEY` is set, use the following code: |
| 81 | + |
| 82 | +```java |
| 83 | +import com.google.cloud.translate.Translate; |
| 84 | +import com.google.cloud.translate.TranslateOptions; |
| 85 | + |
| 86 | +Translate translate = TranslateOptions.defaultInstance().service(); |
| 87 | +``` |
| 88 | + |
| 89 | +Or you can explicitly set the API key as follows: |
| 90 | +```java |
| 91 | +Translate translate = TranslateOptions.builder().apiKey("myKey").service(); |
| 92 | +``` |
| 93 | + |
| 94 | +#### Detecting language |
| 95 | +With Google Translate you can detect the language of some text. The service will provide you with |
| 96 | +the code of the detected language and a level of confidence. |
| 97 | + |
| 98 | +Add the following import at the top of your file: |
| 99 | + |
| 100 | +```java |
| 101 | +import com.google.cloud.translate.Detection; |
| 102 | +``` |
| 103 | + |
| 104 | +Then add the following code to detect the text's language: |
| 105 | + |
| 106 | +```java |
| 107 | +String detectedLanguage = detection.language(); |
| 108 | +``` |
| 109 | +#### Translating text |
| 110 | + |
| 111 | +Google translate allows you to translate some text. When translating one or more texts you can |
| 112 | +either provide the source language or let the service detect it for you. |
| 113 | + |
| 114 | +Add the following imports at the top of your file: |
| 115 | + |
| 116 | +```java |
| 117 | +import com.google.cloud.translate.Translate.TranslateOption; |
| 118 | +import com.google.cloud.translate.Translation; |
| 119 | +``` |
| 120 | + |
| 121 | +Then add the following code to translate a text (specifying its source language): |
| 122 | + |
| 123 | +```java |
| 124 | +Translation translation = translate.translate( |
| 125 | + "World", |
| 126 | + TranslateOption.sourceLanguage("en"), |
| 127 | + TranslateOption.targetLanguage(detectedLanguage)); |
| 128 | +``` |
| 129 | + |
| 130 | +#### Complete source code |
| 131 | + |
| 132 | +In |
| 133 | +[DetectLanguageAndTranslate.java](../gcloud-java-examples/src/main/java/com/google/cloud/examples/translate/snippets/DetectLanguageAndTranslate.java) |
| 134 | +we put together all the code shown above into one program. The program assumes that a valid api key |
| 135 | +is available. |
| 136 | + |
| 137 | +Troubleshooting |
| 138 | +--------------- |
| 139 | + |
| 140 | +To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting). |
| 141 | + |
| 142 | +Java Versions |
| 143 | +------------- |
| 144 | + |
| 145 | +Java 7 or above is required for using this client. |
| 146 | + |
| 147 | +Testing |
| 148 | +------- |
| 149 | + |
| 150 | +This library has tools to help make tests for code using Cloud Translate. |
| 151 | + |
| 152 | +See [TESTING] to read more about testing. |
| 153 | + |
| 154 | +Versioning |
| 155 | +---------- |
| 156 | + |
| 157 | +This library follows [Semantic Versioning](http://semver.org/). |
| 158 | + |
| 159 | +It is currently in major version zero (``0.y.z``), which means that anything |
| 160 | +may change at any time and the public API should not be considered |
| 161 | +stable. |
| 162 | + |
| 163 | +Contributing |
| 164 | +------------ |
| 165 | + |
| 166 | +Contributions to this library are always welcome and highly encouraged. |
| 167 | + |
| 168 | +See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started. |
| 169 | + |
| 170 | +Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more information. |
| 171 | + |
| 172 | +License |
| 173 | +------- |
| 174 | + |
| 175 | +Apache 2.0 - See [LICENSE] for more information. |
| 176 | + |
| 177 | +[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md |
| 178 | +[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct |
| 179 | +[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE |
| 180 | +[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-translate |
| 181 | +[cloud-platform]: https://cloud.google.com/ |
| 182 | + |
| 183 | +[google-translate]: https://cloud.google.com/translate/ |
| 184 | +[google-translate-docs]: https://cloud.google.com/translate/docs/ |
| 185 | +[translate-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/translate/package-summary.html |
0 commit comments