Skip to content

Commit 9d16b79

Browse files
committed
added language detect code
1 parent 78bf1f6 commit 9d16b79

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java

+30-4
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,47 @@
1616

1717
package com.google.cloud.translate.samples;
1818

19+
import java.util.List;
20+
import com.google.common.collect.ImmutableList;
21+
22+
import com.google.cloud.translate.Detection;
1923
import com.google.cloud.translate.Translate;
2024
import com.google.cloud.translate.Translation;
2125
import com.google.cloud.translate.testing.RemoteTranslateHelper;
2226

2327
public class TranslateText {
2428
private static final Translate TRANSLATE = RemoteTranslateHelper.create().options().service();
2529

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+
}
2740
}
2841

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) {
3046
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());
3249
}
50+
3351
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+
}
3561
}
3662
}

0 commit comments

Comments
 (0)