xref: /aosp_15_r20/external/google-cloud-java/java-translate/.readme-partials.yaml (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1custom_content: |
2  ### Example Application
3
4  [`TranslateExample`](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/TranslateExample.java)
5  is a simple command line interface that provides some of Google Translation's functionality.
6
7  #### Creating an authorized service object
8  To make authenticated requests to Google Translation, you must create a service object with
9  credentials or use an API key. The simplest way to authenticate is to use
10  [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
11  These credentials are automatically inferred from your environment, so you only need the following
12  code to create your service object:
13
14  ```java
15  import com.google.cloud.translate.Translate;
16  import com.google.cloud.translate.TranslateOptions;
17
18  Translate translate = TranslateOptions.getDefaultInstance().getService();
19  ```
20
21  Notice that this code can be also used with an API key. By default, an API key is looked for in the
22  `GOOGLE_API_KEY` environment variable. Once the API key is set, you can make API calls by invoking
23  methods on the Translation service created via `TranslateOptions.getDefaultInstance().getService()`.
24
25  You can also explicitly set the API key as follows:
26  ```java
27  Translate translate = TranslateOptions.newBuilder().setApiKey("myKey").build().getService();
28  ```
29
30  #### Detecting language
31  With Google Translation you can detect the language of some text. The service will provide you with
32  the code of the detected language and a level of confidence.
33
34  Add the following import at the top of your file:
35
36  ```java
37  import com.google.cloud.translate.Detection;
38  ```
39
40  Then pick a text sample:
41
42  ```java
43  final String mysteriousText = "Hola Mundo";
44  ```
45
46  Then add the following code to detect the text's language:
47
48  ```java
49  Detection detection = translate.detect(mysteriousText);
50  String detectedLanguage = detection.getLanguage();
51  ```
52  #### Translating text
53
54  Google translation allows you to translate some text. When translating one or more texts you can
55  either provide the source language or let the service detect it for you.
56
57  Add the following imports at the top of your file:
58
59  ```java
60  import com.google.cloud.translate.Translate.TranslateOption;
61  import com.google.cloud.translate.Translation;
62  ```
63
64  Then 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):
65
66  ```java
67  Translation translation = translate.translate(
68      mysteriousText,
69      TranslateOption.sourceLanguage(detectedLanguage),
70      TranslateOption.targetLanguage("en"));
71  ```
72
73  #### Complete source code
74
75  In
76  [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)
77  we put together all the code shown above into one program. The program assumes that either Application
78  Default Credentials or a valid API key are available.