[Google Translation API v2 for Java]

简宏义
2023-12-01
Reference:
https://cloud.google.com/translate/docs/reference/libraries#java-resources

QuickstartSample:
 1 // Imports the Google Cloud client library
 2 import com.google.cloud.translate.Translate;
 3 import com.google.cloud.translate.Translate.TranslateOption;
 4 import com.google.cloud.translate.TranslateOptions;
 5 import com.google.cloud.translate.Translation;
 6 
 7 public class QuickstartSample {
 8   public static void main(String... args) throws Exception {
 9     // Instantiates a client
10     Translate translate = TranslateOptions.builder().apiKey("YOUR_API_KEY").build().service();
11 
12     // The text to translate
13     String text = "Hello, world!";
14 
15     // Translates some text into Russian
16     Translation translation = translate.translate(
17       text,
18       TranslateOption.sourceLanguage("en"),
19       TranslateOption.targetLanguage("ru")
20     );
21 
22     System.out.printf("Text: %s%n", text);
23     System.out.printf("Translation: %s%n", translation.translatedText());
24   }
25 }

 

maven:

1 <dependency>
2     <groupId>com.google.cloud</groupId>
3     <artifactId>google-cloud-translate</artifactId>
4     <version>0.4.0</version>
5 </dependency>

 

 

转载于:https://www.cnblogs.com/XBWer/p/5955246.html

 类似资料:

相关阅读

相关文章

相关问答