Sunday, 19 June 2011

Listing Locale Information

In writing a global application - if there is such a thing - you’ll often need to add locale specific information. From a Java perspective, a locale is an arbitrary combination of language and country information held in several different ways. This can then be used as a key to describe a whole bunch of other information such as number formatting, currency details and dates and times.

Accessing locale information in Java is a matter of using the Locale class. It comes with a number of pre-defined convenient constants that cover commonly used locales. There are 22 of these and include basic countries such as: FRANCE, UK, US and JAPAN and basic languages such as: FRENCH, ENGLISH and JAPANESE.

It is possible, however, to list all the locales supported by your JVM using the LocaleServiceProvider class as demonstrated by the following code snippet.

    System.out.println("All Locale Information");
    System.out.println
("\nDisplayName,Country,Display Country,ISO3 Country Code,Language,"
       
+ "Display Language,ISO3 Language Code,Display Varient,Varient, File Suffix");

    Locale
[] locales = Locale.getAvailableLocales();
   
for (Locale locale : locales) {

     
String country = locale.getCountry();
      String displayCountry = locale.getDisplayCountry
();
      String displayLanguage = locale.getDisplayLanguage
();
      String displayName = locale.getDisplayName
();
      String displayVarient = locale.getDisplayVariant
();
      String iso3CountryCode = locale.getISO3Country
();
      String iso3LanguageCode = locale.getISO3Language
();
      String language = locale.getLanguage
();
      String varient = locale.getVariant
();
      String separator =
",";
      System.out.println
(displayName + separator + country + separator + displayCountry
          + separator + iso3CountryCode + separator + language + separator
          + displayLanguage + separator + iso3LanguageCode + separator
          + displayVarient + separator + varient + separator + locale.toString
());
   
}

If you want to see this code’s output, which are the locales supported by my machine, then it’s available on a this page.

No comments: