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