Friday, 18 September 2015

Display list of languages in Java

In this post, I am going to explain how to display list of countries in Java. In many web applications, it is required to display list of languages as drop down list. You can get all the languages in following ways.
a.   By maintaining a list of languages in a file/database
b.   By hardcoding list of languages in Java file itself.
c.    By using Java Locale class

I am going to show you, how to get list of languages using Locale class.

“LocaleUtil’ class following methods to get list of languages and their codes.

Method
Description
public static List<Language> getAllLanguages()
Return list of languages.
public static List<Language> getAllLanguages(Locale locale)
Return list of for given locale.

public class Language {
 private String languaneCode;
 private String languageName;

 public Language(String languaneCode, String languageName) {
  super();
  this.languaneCode = languaneCode;
  this.languageName = languageName;
 }

 public String getLanguaneCode() {
  return languaneCode;
 }

 public void setLanguaneCode(String languaneCode) {
  this.languaneCode = languaneCode;
 }

 public String getLanguageName() {
  return languageName;
 }

 public void setLanguageName(String languageName) {
  this.languageName = languageName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Language [languaneCode=").append(languaneCode)
    .append(", languageName=").append(languageName).append("]");
  return builder.toString();
 }

}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public class LanguageUtil {
 private static List<Language> languages = new ArrayList<>();
 private static Map<Locale, List<Language>> languagesMap = new HashMap<>();

 /**
  * @return list of all @{link Language} objects
  */
 public static List<Language> getAllLanguages() {
  if (!languages.isEmpty())
   return languages;

  /* Get language codes */
  String[] languageCodes = Locale.getISOLanguages();

  for (String languageCode : languageCodes) {
   Locale obj = new Locale(languageCode);
   Language language = new Language(languageCode,
     obj.getDisplayLanguage());
   languages.add(language);
  }

  return languages;
 }

 /**
  * @param locale
  *            User specific locale
  * @return list of all @{link Language} objects in user specific language
  */
 public static List<Language> getAllLanguages(Locale locale) {
  Objects.nonNull(locale);
  if (languagesMap.containsKey(locale))
   return languagesMap.get(locale);

  List<Language> temp = new ArrayList<>();

  /* Get language codes */
  String[] languageCodes = Locale.getISOLanguages();

  for (String languageCode : languageCodes) {
   Locale obj = new Locale(languageCode);
   Language language = new Language(languageCode,
     obj.getDisplayLanguage(locale));
   temp.add(language);
  }

  languagesMap.put(locale, temp);
  return languagesMap.get(locale);
 }
}

import java.util.List;
import java.util.Locale;

public class LanguageUtilTest {

 public static void main(String args[]) {
  List<Language> languages = LanguageUtil.getAllLanguages();
  System.out.println("List of languages");
  languages.forEach(language -> System.out.println(language));

  languages = LanguageUtil.getAllLanguages(Locale.CHINESE);
  System.out.println("List of languages in Chinese language");
  languages.forEach(language -> System.out.println(language));
 }
}


Output
List of languages
Language [languaneCode=aa, languageName=Afar]
Language [languaneCode=ab, languageName=Abkhazian]
Language [languaneCode=ae, languageName=Avestan]
Language [languaneCode=af, languageName=Afrikaans]
Language [languaneCode=ak, languageName=Akan]
Language [languaneCode=am, languageName=Amharic]
Language [languaneCode=an, languageName=Aragonese]
Language [languaneCode=ar, languageName=Arabic]
Language [languaneCode=as, languageName=Assamese]
Language [languaneCode=av, languageName=Avaric]
Language [languaneCode=ay, languageName=Aymara]
Language [languaneCode=az, languageName=Azerbaijani]
Language [languaneCode=ba, languageName=Bashkir]
Language [languaneCode=be, languageName=Belarusian]
Language [languaneCode=bg, languageName=Bulgarian]
Language [languaneCode=bh, languageName=Bihari]
Language [languaneCode=bi, languageName=Bislama]
Language [languaneCode=bm, languageName=Bambara]
Language [languaneCode=bn, languageName=Bengali]
Language [languaneCode=bo, languageName=Tibetan]
Language [languaneCode=br, languageName=Breton]
Language [languaneCode=bs, languageName=Bosnian]
Language [languaneCode=ca, languageName=Catalan]
Language [languaneCode=ce, languageName=Chechen]
Language [languaneCode=ch, languageName=Chamorro]
Language [languaneCode=co, languageName=Corsican]
Language [languaneCode=cr, languageName=Cree]
Language [languaneCode=cs, languageName=Czech]
Language [languaneCode=cu, languageName=Church Slavic]
Language [languaneCode=cv, languageName=Chuvash]
Language [languaneCode=cy, languageName=Welsh]
Language [languaneCode=da, languageName=Danish]
Language [languaneCode=de, languageName=German]
Language [languaneCode=dv, languageName=Divehi]
Language [languaneCode=dz, languageName=Dzongkha]
Language [languaneCode=ee, languageName=Ewe]
Language [languaneCode=el, languageName=Greek]
Language [languaneCode=en, languageName=English]
Language [languaneCode=eo, languageName=Esperanto]
Language [languaneCode=es, languageName=Spanish]
Language [languaneCode=et, languageName=Estonian]
Language [languaneCode=eu, languageName=Basque]
Language [languaneCode=fa, languageName=Persian]
Language [languaneCode=ff, languageName=Fulah]
Language [languaneCode=fi, languageName=Finnish]
Language [languaneCode=fj, languageName=Fijian]
Language [languaneCode=fo, languageName=Faroese]
Language [languaneCode=fr, languageName=French]
Language [languaneCode=fy, languageName=Frisian]
Language [languaneCode=ga, languageName=Irish]
Language [languaneCode=gd, languageName=Scottish Gaelic]
Language [languaneCode=gl, languageName=Gallegan]
Language [languaneCode=gn, languageName=Guarani]
Language [languaneCode=gu, languageName=Gujarati]
Language [languaneCode=gv, languageName=Manx]
Language [languaneCode=ha, languageName=Hausa]
Language [languaneCode=he, languageName=Hebrew]
Language [languaneCode=hi, languageName=Hindi]
Language [languaneCode=ho, languageName=Hiri Motu]
Language [languaneCode=hr, languageName=Croatian]
Language [languaneCode=ht, languageName=Haitian]
Language [languaneCode=hu, languageName=Hungarian]
Language [languaneCode=hy, languageName=Armenian]
Language [languaneCode=hz, languageName=Herero]
Language [languaneCode=ia, languageName=Interlingua]
Language [languaneCode=id, languageName=Indonesian]
Language [languaneCode=ie, languageName=Interlingue]
Language [languaneCode=ig, languageName=Igbo]
Language [languaneCode=ii, languageName=Sichuan Yi]
Language [languaneCode=ik, languageName=Inupiaq]
Language [languaneCode=in, languageName=Indonesian]
Language [languaneCode=io, languageName=Ido]
Language [languaneCode=is, languageName=Icelandic]
Language [languaneCode=it, languageName=Italian]
Language [languaneCode=iu, languageName=Inuktitut]
Language [languaneCode=iw, languageName=Hebrew]
Language [languaneCode=ja, languageName=Japanese]
Language [languaneCode=ji, languageName=Yiddish]
Language [languaneCode=jv, languageName=Javanese]
Language [languaneCode=ka, languageName=Georgian]
Language [languaneCode=kg, languageName=Kongo]
Language [languaneCode=ki, languageName=Kikuyu]
Language [languaneCode=kj, languageName=Kwanyama]
Language [languaneCode=kk, languageName=Kazakh]
Language [languaneCode=kl, languageName=Greenlandic]
Language [languaneCode=km, languageName=Khmer]
Language [languaneCode=kn, languageName=Kannada]
Language [languaneCode=ko, languageName=Korean]
Language [languaneCode=kr, languageName=Kanuri]
Language [languaneCode=ks, languageName=Kashmiri]
Language [languaneCode=ku, languageName=Kurdish]
Language [languaneCode=kv, languageName=Komi]
Language [languaneCode=kw, languageName=Cornish]
Language [languaneCode=ky, languageName=Kirghiz]
Language [languaneCode=la, languageName=Latin]
Language [languaneCode=lb, languageName=Luxembourgish]
Language [languaneCode=lg, languageName=Ganda]
Language [languaneCode=li, languageName=Limburgish]
Language [languaneCode=ln, languageName=Lingala]
Language [languaneCode=lo, languageName=Lao]
Language [languaneCode=lt, languageName=Lithuanian]
Language [languaneCode=lu, languageName=Luba-Katanga]
Language [languaneCode=lv, languageName=Latvian]
Language [languaneCode=mg, languageName=Malagasy]
Language [languaneCode=mh, languageName=Marshallese]
Language [languaneCode=mi, languageName=Maori]
Language [languaneCode=mk, languageName=Macedonian]
Language [languaneCode=ml, languageName=Malayalam]
Language [languaneCode=mn, languageName=Mongolian]
Language [languaneCode=mo, languageName=Moldavian]
Language [languaneCode=mr, languageName=Marathi]
Language [languaneCode=ms, languageName=Malay]
Language [languaneCode=mt, languageName=Maltese]
Language [languaneCode=my, languageName=Burmese]
Language [languaneCode=na, languageName=Nauru]
Language [languaneCode=nb, languageName=Norwegian Bokmål]
Language [languaneCode=nd, languageName=North Ndebele]
Language [languaneCode=ne, languageName=Nepali]
Language [languaneCode=ng, languageName=Ndonga]
Language [languaneCode=nl, languageName=Dutch]
Language [languaneCode=nn, languageName=Norwegian Nynorsk]
Language [languaneCode=no, languageName=Norwegian]
Language [languaneCode=nr, languageName=South Ndebele]
Language [languaneCode=nv, languageName=Navajo]
Language [languaneCode=ny, languageName=Nyanja]
Language [languaneCode=oc, languageName=Occitan]
Language [languaneCode=oj, languageName=Ojibwa]
Language [languaneCode=om, languageName=Oromo]
Language [languaneCode=or, languageName=Oriya]
Language [languaneCode=os, languageName=Ossetian]
Language [languaneCode=pa, languageName=Panjabi]
Language [languaneCode=pi, languageName=Pali]
Language [languaneCode=pl, languageName=Polish]
Language [languaneCode=ps, languageName=Pushto]
Language [languaneCode=pt, languageName=Portuguese]
Language [languaneCode=qu, languageName=Quechua]
Language [languaneCode=rm, languageName=Raeto-Romance]
Language [languaneCode=rn, languageName=Rundi]
Language [languaneCode=ro, languageName=Romanian]
Language [languaneCode=ru, languageName=Russian]
Language [languaneCode=rw, languageName=Kinyarwanda]
Language [languaneCode=sa, languageName=Sanskrit]
Language [languaneCode=sc, languageName=Sardinian]
Language [languaneCode=sd, languageName=Sindhi]
Language [languaneCode=se, languageName=Northern Sami]
Language [languaneCode=sg, languageName=Sango]
Language [languaneCode=si, languageName=Sinhalese]
Language [languaneCode=sk, languageName=Slovak]
Language [languaneCode=sl, languageName=Slovenian]
Language [languaneCode=sm, languageName=Samoan]
Language [languaneCode=sn, languageName=Shona]
Language [languaneCode=so, languageName=Somali]
Language [languaneCode=sq, languageName=Albanian]
Language [languaneCode=sr, languageName=Serbian]
Language [languaneCode=ss, languageName=Swati]
Language [languaneCode=st, languageName=Southern Sotho]
Language [languaneCode=su, languageName=Sundanese]
Language [languaneCode=sv, languageName=Swedish]
Language [languaneCode=sw, languageName=Swahili]
Language [languaneCode=ta, languageName=Tamil]
Language [languaneCode=te, languageName=Telugu]
Language [languaneCode=tg, languageName=Tajik]
Language [languaneCode=th, languageName=Thai]
Language [languaneCode=ti, languageName=Tigrinya]
Language [languaneCode=tk, languageName=Turkmen]
Language [languaneCode=tl, languageName=Tagalog]
Language [languaneCode=tn, languageName=Tswana]
Language [languaneCode=to, languageName=Tonga]
Language [languaneCode=tr, languageName=Turkish]
Language [languaneCode=ts, languageName=Tsonga]
Language [languaneCode=tt, languageName=Tatar]
Language [languaneCode=tw, languageName=Twi]
Language [languaneCode=ty, languageName=Tahitian]
Language [languaneCode=ug, languageName=Uighur]
Language [languaneCode=uk, languageName=Ukrainian]
Language [languaneCode=ur, languageName=Urdu]
Language [languaneCode=uz, languageName=Uzbek]
Language [languaneCode=ve, languageName=Venda]
Language [languaneCode=vi, languageName=Vietnamese]
Language [languaneCode=vo, languageName=Volapük]
Language [languaneCode=wa, languageName=Walloon]
Language [languaneCode=wo, languageName=Wolof]
Language [languaneCode=xh, languageName=Xhosa]
Language [languaneCode=yi, languageName=Yiddish]
Language [languaneCode=yo, languageName=Yoruba]
Language [languaneCode=za, languageName=Zhuang]
Language [languaneCode=zh, languageName=Chinese]
Language [languaneCode=zu, languageName=Zulu]
List of languages in Chinese language
Language [languaneCode=aa, languageName=阿法文]
Language [languaneCode=ab, languageName=阿布哈西亚文]
Language [languaneCode=ae, languageName=阿维斯陀文]
Language [languaneCode=af, languageName=南非荷兰文]
Language [languaneCode=ak, languageName=库阿文]
Language [languaneCode=am, languageName=阿姆哈拉文]
Language [languaneCode=an, languageName=阿拉贡文]
Language [languaneCode=ar, languageName=阿拉伯文]
Language [languaneCode=as, languageName=阿萨姆文]
Language [languaneCode=av, languageName=阿瓦尔文]
Language [languaneCode=ay, languageName=艾马拉文]
Language [languaneCode=az, languageName=阿塞拜疆文]
Language [languaneCode=ba, languageName=巴什客尔文]
Language [languaneCode=be, languageName=白俄罗斯文]
Language [languaneCode=bg, languageName=保加利亚文]
Language [languaneCode=bh, languageName=比哈尔文]
Language [languaneCode=bi, languageName=比斯拉马文]
Language [languaneCode=bm, languageName=班巴拉文]
Language [languaneCode=bn, languageName=孟加拉文]
Language [languaneCode=bo, languageName=西藏文]
Language [languaneCode=br, languageName=布里多尼文]
Language [languaneCode=bs, languageName=波斯尼亚文]
Language [languaneCode=ca, languageName=加泰罗尼亚文]
Language [languaneCode=ce, languageName=车臣文]
Language [languaneCode=ch, languageName=查莫罗文]
Language [languaneCode=co, languageName=科西嘉文]
Language [languaneCode=cr, languageName=克里文]
Language [languaneCode=cs, languageName=捷克文]
Language [languaneCode=cu, languageName=教会斯拉夫文]
Language [languaneCode=cv, languageName=楚瓦什文]
Language [languaneCode=cy, languageName=威尔士文]
Language [languaneCode=da, languageName=丹麦文]
Language [languaneCode=de, languageName=德文]
Language [languaneCode=dv, languageName=迪维希文]
Language [languaneCode=dz, languageName=不丹文]
Language [languaneCode=ee, languageName=埃维文]
Language [languaneCode=el, languageName=希腊文]
Language [languaneCode=en, languageName=英文]
Language [languaneCode=eo, languageName=世界文]
Language [languaneCode=es, languageName=西班牙文]
Language [languaneCode=et, languageName=爱沙尼亚文]
Language [languaneCode=eu, languageName=巴斯克文]
Language [languaneCode=fa, languageName=波斯文]
Language [languaneCode=ff, languageName=富拉文]
Language [languaneCode=fi, languageName=芬兰文]
Language [languaneCode=fj, languageName=斐济文]
Language [languaneCode=fo, languageName=法罗文]
Language [languaneCode=fr, languageName=法文]
Language [languaneCode=fy, languageName=弗里斯兰文]
Language [languaneCode=ga, languageName=爱尔兰文]
Language [languaneCode=gd, languageName=苏格兰- 盖尔文]
Language [languaneCode=gl, languageName=加利西亚文]
Language [languaneCode=gn, languageName=瓜拉尼文]
Language [languaneCode=gu, languageName=古加拉提文]
Language [languaneCode=gv, languageName=马恩文]
Language [languaneCode=ha, languageName=豪撒文]
Language [languaneCode=he, languageName=希伯来文]
Language [languaneCode=hi, languageName=印地文]
Language [languaneCode=ho, languageName=新里木托文]
Language [languaneCode=hr, languageName=克罗地亚文]
Language [languaneCode=ht, languageName=海地文]
Language [languaneCode=hu, languageName=匈牙利文]
Language [languaneCode=hy, languageName=亚美尼亚文]
Language [languaneCode=hz, languageName=赫雷罗文]
Language [languaneCode=ia, languageName=拉丁国际文]
Language [languaneCode=id, languageName=印度尼西亚文]
Language [languaneCode=ie, languageName=拉丁国际文]
Language [languaneCode=ig, languageName=伊博文]
Language [languaneCode=ii, languageName=四川彝文]
Language [languaneCode=ik, languageName=依奴皮维克文]
Language [languaneCode=in, languageName=印度尼西亚文]
Language [languaneCode=io, languageName=伊多文]
Language [languaneCode=is, languageName=冰岛文]
Language [languaneCode=it, languageName=意大利文]
Language [languaneCode=iu, languageName=爱斯基摩文]
Language [languaneCode=iw, languageName=希伯来文]
Language [languaneCode=ja, languageName=日文]
Language [languaneCode=ji, languageName=依地文]
Language [languaneCode=jv, languageName=爪哇文]
Language [languaneCode=ka, languageName=格鲁吉亚文]
Language [languaneCode=kg, languageName=刚果文]
Language [languaneCode=ki, languageName=吉库尤文]
Language [languaneCode=kj, languageName=卡湾亚马文]
Language [languaneCode=kk, languageName=哈萨克文]
Language [languaneCode=kl, languageName=格陵兰文]
Language [languaneCode=km, languageName=柬埔寨文]
Language [languaneCode=kn, languageName=埃纳德文]
Language [languaneCode=ko, languageName=朝鲜文]
Language [languaneCode=kr, languageName=卡努里文]
Language [languaneCode=ks, languageName=克什米尔文]
Language [languaneCode=ku, languageName=库尔德文]
Language [languaneCode=kv, languageName=科米文]
Language [languaneCode=kw, languageName=康沃尔文]
Language [languaneCode=ky, languageName=吉尔吉斯文]
Language [languaneCode=la, languageName=拉丁文]
Language [languaneCode=lb, languageName=卢森堡文]
Language [languaneCode=lg, languageName=干达文]
Language [languaneCode=li, languageName=林堡文]
Language [languaneCode=ln, languageName=林加拉文]
Language [languaneCode=lo, languageName=老挝文]
Language [languaneCode=lt, languageName=立陶宛文]
Language [languaneCode=lu, languageName=卢巴-加丹加]
Language [languaneCode=lv, languageName=拉托维亚文(列托)]
Language [languaneCode=mg, languageName=马尔加什文]
Language [languaneCode=mh, languageName=马绍尔文]
Language [languaneCode=mi, languageName=毛利文]
Language [languaneCode=mk, languageName=马其顿文]
Language [languaneCode=ml, languageName=马来亚拉姆文]
Language [languaneCode=mn, languageName=蒙古文]
Language [languaneCode=mo, languageName=摩尔多瓦文]
Language [languaneCode=mr, languageName=马拉地文]
Language [languaneCode=ms, languageName=马来文]
Language [languaneCode=mt, languageName=马耳他文]
Language [languaneCode=my, languageName=缅甸文]
Language [languaneCode=na, languageName=瑙鲁文]
Language [languaneCode=nb, languageName=挪威博克马尔文]
Language [languaneCode=nd, languageName=北恩德比利文]
Language [languaneCode=ne, languageName=尼泊尔文]
Language [languaneCode=ng, languageName=恩东加文]
Language [languaneCode=nl, languageName=荷兰文]
Language [languaneCode=nn, languageName=挪威尼诺斯克文]
Language [languaneCode=no, languageName=挪威文]
Language [languaneCode=nr, languageName=南恩德比利文]
Language [languaneCode=nv, languageName=纳瓦霍文]
Language [languaneCode=ny, languageName=尼扬扎文]
Language [languaneCode=oc, languageName=奥西坦文]
Language [languaneCode=oj, languageName=奥吉布瓦文]
Language [languaneCode=om, languageName=阿曼文]
Language [languaneCode=or, languageName=欧里亚文]
Language [languaneCode=os, languageName=奥塞梯文]
Language [languaneCode=pa, languageName=旁遮普文]
Language [languaneCode=pi, languageName=巴利文]
Language [languaneCode=pl, languageName=波兰文]
Language [languaneCode=ps, languageName=普什图文]
Language [languaneCode=pt, languageName=葡萄牙文]
Language [languaneCode=qu, languageName=盖丘亚文]
Language [languaneCode=rm, languageName=里托罗曼斯文]
Language [languaneCode=rn, languageName=基隆迪文]
Language [languaneCode=ro, languageName=罗马尼亚文]
Language [languaneCode=ru, languageName=俄文]
Language [languaneCode=rw, languageName=卢旺达文]
Language [languaneCode=sa, languageName=梵文]
Language [languaneCode=sc, languageName=撒丁文]
Language [languaneCode=sd, languageName=苏丹文]
Language [languaneCode=se, languageName=北沙密文]
Language [languaneCode=sg, languageName=桑戈文]
Language [languaneCode=si, languageName=僧伽罗文]
Language [languaneCode=sk, languageName=斯洛伐克文]
Language [languaneCode=sl, languageName=斯洛文尼亚文]
Language [languaneCode=sm, languageName=萨摩亚文]
Language [languaneCode=sn, languageName=塞内加尔文]
Language [languaneCode=so, languageName=索马里文]
Language [languaneCode=sq, languageName=阿尔巴尼亚文]
Language [languaneCode=sr, languageName=塞尔维亚文]
Language [languaneCode=ss, languageName=辛辛那提文]
Language [languaneCode=st, languageName=塞索托文]
Language [languaneCode=su, languageName=苏丹文]
Language [languaneCode=sv, languageName=瑞典文]
Language [languaneCode=sw, languageName=斯瓦希里文]
Language [languaneCode=ta, languageName=泰米尔文]
Language [languaneCode=te, languageName=泰卢固文]
Language [languaneCode=tg, languageName=塔吉克文]
Language [languaneCode=th, languageName=泰文]
Language [languaneCode=ti, languageName=提格里尼亚文]
Language [languaneCode=tk, languageName=土库曼文]
Language [languaneCode=tl, languageName=塔加路族文]
Language [languaneCode=tn, languageName=突尼斯文]
Language [languaneCode=to, languageName=汤加文]
Language [languaneCode=tr, languageName=土耳其文]
Language [languaneCode=ts, languageName=特松加文]
Language [languaneCode=tt, languageName=鞑靼文]
Language [languaneCode=tw, languageName=契维文]
Language [languaneCode=ty, languageName=塔希提文]
Language [languaneCode=ug, languageName=维吾尔文]
Language [languaneCode=uk, languageName=乌克兰文]
Language [languaneCode=ur, languageName=乌尔都文]
Language [languaneCode=uz, languageName=乌兹别克文]
Language [languaneCode=ve, languageName=文达文]
Language [languaneCode=vi, languageName=越南文]
Language [languaneCode=vo, languageName=沃拉普克文]
Language [languaneCode=wa, languageName=瓦龙文]
Language [languaneCode=wo, languageName=沃尔夫文]
Language [languaneCode=xh, languageName=班图文]
Language [languaneCode=yi, languageName=依地文]
Language [languaneCode=yo, languageName=约鲁巴文]
Language [languaneCode=za, languageName=藏文]
Language [languaneCode=zh, languageName=中文]
Language [languaneCode=zu, languageName=祖鲁文]



No comments:

Post a Comment