|
16 | 16 |
|
17 | 17 | package com.google.cloud.translate.samples;
|
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | + |
| 22 | +import com.google.cloud.translate.Detection; |
19 | 23 | import com.google.cloud.translate.Translate;
|
20 | 24 | import com.google.cloud.translate.Translation;
|
21 | 25 | import com.google.cloud.translate.testing.RemoteTranslateHelper;
|
22 | 26 |
|
23 | 27 | public class TranslateText {
|
24 | 28 | private static final Translate TRANSLATE = RemoteTranslateHelper.create().options().service();
|
25 | 29 |
|
26 |
| - public void detectLanguages() { |
| 30 | + /** |
| 31 | + * Detect the language of input text |
| 32 | + */ |
| 33 | + public static void detectLanguage(String sourceText) { |
| 34 | + List<Detection> detections = TRANSLATE.detect(ImmutableList.of(sourceText)); |
| 35 | + |
| 36 | + System.out.println("Language(s) detected:"); |
| 37 | + for(Detection detection : detections) { |
| 38 | + System.out.println("\t"+detection); |
| 39 | + } |
27 | 40 | }
|
28 | 41 |
|
29 |
| - public void translateText(String sourceText) { |
| 42 | + /** |
| 43 | + * Translates the source text in any language to english |
| 44 | + */ |
| 45 | + public static void translateText(String sourceText) { |
30 | 46 | Translation translation = TRANSLATE.translate(sourceText);
|
31 |
| - System.out.println(translation.translatedText()); |
| 47 | + System.out.println("Source Text:\n\t"+sourceText); |
| 48 | + System.out.println("Translated Text:\n\t"+translation.translatedText()); |
32 | 49 | }
|
| 50 | + |
33 | 51 | public static void main(String[] args) {
|
34 |
| - new TranslateText().translateText("Hola"); |
| 52 | + String command = args[0]; |
| 53 | + String text = args[1]; |
| 54 | + |
| 55 | + if(command.equals("detect")) { |
| 56 | + TranslateText.detectLanguage(text); |
| 57 | + } |
| 58 | + else if(command.equals("translate")) { |
| 59 | + TranslateText.translateText(text); |
| 60 | + } |
35 | 61 | }
|
36 | 62 | }
|
0 commit comments