diff --git a/translate/README.md b/translate/README.md index ff2e9131d2e..a1d663c46e5 100644 --- a/translate/README.md +++ b/translate/README.md @@ -54,6 +54,11 @@ Translate API Features: java -jar $JAR_FILE langsupport ``` + * List the languages supported for given target language + ``` + java -jar $JAR_FILE langsupport $TARGET_LANG + ``` + * Detect input text language ``` java -jar $JAR_FILE detect "$INPUT" diff --git a/translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java b/translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java index 3e0e46532dc..90e8cb20930 100644 --- a/translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java +++ b/translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java @@ -20,6 +20,7 @@ import com.google.cloud.translate.Detection; import com.google.cloud.translate.Language; import com.google.cloud.translate.Translate; +import com.google.cloud.translate.Translate.LanguageListOption; import com.google.cloud.translate.Translate.TranslateOption; import com.google.cloud.translate.TranslateOptions; import com.google.cloud.translate.Translation; @@ -27,6 +28,7 @@ import java.io.PrintStream; import java.util.List; +import java.util.Optional; public class TranslateText { /** @@ -84,10 +86,12 @@ public static void translateTextWithOptions( * Displays a list of supported languages and codes. * * @param out print stream + * @param tgtLang optional target language */ - public static void displaySupportedLanguages(PrintStream out) { + public static void displaySupportedLanguages(PrintStream out, Optional tgtLang) { Translate translate = createTranslateService(); - List languages = translate.listSupportedLanguages(); + LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en")); + List languages = translate.listSupportedLanguages(target); for (Language language : languages) { out.printf("Name: %s, Code: %s\n", language.name(), language.code()); @@ -137,7 +141,12 @@ public static void main(String[] args) { TranslateText.translateText(text, System.out); } } else if (command.equals("langsupport")) { - TranslateText.displaySupportedLanguages(System.out); + try { + String target = args[1]; + TranslateText.displaySupportedLanguages(System.out, Optional.of(target)); + } catch (ArrayIndexOutOfBoundsException ex) { + TranslateText.displaySupportedLanguages(System.out, Optional.empty()); + } } } } diff --git a/translate/src/test/java/com/google/cloud/translate/samples/TranslateTextTest.java b/translate/src/test/java/com/google/cloud/translate/samples/TranslateTextTest.java index 88b63cbb792..679f3edc36e 100644 --- a/translate/src/test/java/com/google/cloud/translate/samples/TranslateTextTest.java +++ b/translate/src/test/java/com/google/cloud/translate/samples/TranslateTextTest.java @@ -26,9 +26,10 @@ import java.io.PrintStream; import java.util.Arrays; import java.util.List; +import java.util.Optional; /** - * Unit tests for {@link Analyze}. + * Unit tests for {@link TranslateText}. */ @RunWith(JUnit4.class) public class TranslateTextTest { @@ -56,7 +57,39 @@ public class TranslateTextTest { PrintStream out = new PrintStream(bout); // Act - TranslateText.displaySupportedLanguages(out); + TranslateText.displaySupportedLanguages(out, Optional.empty()); + + // Assert + String got = bout.toString(); + for (String language : languages) { + assertThat(got).contains(language); + } + } + + @Test public void testSupportedLanguagesTargetFrench() throws Exception { + //Supported languages + List languages = Arrays.asList( + "Afrikaans", "Albanais", "Allemand", "Amharique", "Anglais", "Arabe", "Arménien", + "Azéri", "Basque", "Bengali", "Biélorusse", "Birman", "Bosniaque", "Bulgare", "Catalan", + "Cebuano", "Chichewa", "Chinois (simplifié)", "Chinois (traditionnel)", "Cingalais", + "Coréen", "Corse", "Créole haïtien", "Croate", "Danois", "Espagnol", "Espéranto", + "Estonien", "Finnois", "Français", "Frison", "Gaélique (Écosse)", "Galicien", + "Gallois", "Géorgien", "Grec", "Gujarati", "Haoussa", "Hawaïen", "Hébreu", "Hindi", + "Hmong", "Hongrois", "Igbo", "Indonésien", "Irlandais", "Islandais", "Italien", + "Japonais", "Javanais", "Kannada", "Kazakh", "Khmer", "Kirghiz", "Kurde", "Laotien", + "Latin", "Letton", "Lituanien", "Luxembourgeois", "Macédonien", "Malaisien", "Malayalam", + "Malgache", "Maltais", "Maori", "Marathi", "Mongol", "Néerlandais", "Népalais", "Norvégien", + "Ouzbek", "Pachtô", "Panjabi", "Persan", "Polonais", "Portugais", "Roumain", "Russe", + "Samoan", "Serbe", "Sesotho", "Shona", "Sindhî", "Slovaque", "Slovène", "Somali", + "Soundanais", "Suédois", "Swahili", "Tadjik", "Tagalog", "Tamoul", "Tchèque", "Telugu", + "Thaï", "Turc", "Ukrainien", "Urdu", "Vietnamien", "Xhosa", "Yiddish", "Yorouba","Zoulou"); + + // Arrange + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + + // Act + TranslateText.displaySupportedLanguages(out, Optional.of("fr")); // Assert String got = bout.toString();