How to make HashMap of Google Translate supported languages in JAVA?

405 Views Asked by At

I currently using Google Translate API V2 google-translate-api-v2-java-core-0.52.jar

Is there any way to get a list of supported languages and put them into HashMap, where key is a Language name, and value is language ISO Code.

Example

HashMap<String, String> langs = new HashMap<String, String>;
langs.put("English", "en");

So I need all of supported Google Translate languages in this HashMap. I tried to do this with Locale.getISOCountries() but I will get all languages in my HashMap and most of them are not supported by Google Translate.

1

There are 1 best solutions below

0
On BEST ANSWER
public static void displaySupportedLanguages(PrintStream out, Optional<String> tgtLang) {
  Translate translate = createTranslateService();
  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.getName(), language.getCode());
  }
}

refer:

  1. https://cloud.google.com/translate/docs/discovering-supported-languages
  2. https://translation.googleapis.com/language/translate/v2/languages?parameters

And, according to the source code of this jar file, it seems method Translator.languages(null) can also make it. (pass null as target language)