Skip to content

Commit

Permalink
added supportedd languages for target language
Browse files Browse the repository at this point in the history
  • Loading branch information
puneithk committed Oct 7, 2016
1 parent c1b48cb commit 58d76b4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
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;
import com.google.common.collect.ImmutableList;

import java.io.PrintStream;
import java.util.List;
import java.util.Optional;

public class TranslateText {
/**
Expand Down Expand Up @@ -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<String> tgtLang) {
Translate translate = createTranslateService();
List<Language> languages = translate.listSupportedLanguages();
LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en"));
List<Language> languages = translate.listSupportedLanguages(target);

for (Language language : languages) {
out.printf("Name: %s, Code: %s\n", language.name(), language.code());
Expand Down Expand Up @@ -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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> 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();
Expand Down

0 comments on commit 58d76b4

Please sign in to comment.