Locale

Intro


Countries


Java ISO country listing

The following program lists the 2-letter country codes and their names. It cannot handle 3-letter codes. It is not complete - it does not include all the countries in ISO 3166.


import java.util.Locale;

public class ISOCountries {

    public static void main(String[] args) {
        // get all countries
	String[] countries = Locale.getISOCountries();
	for (int n = 0; n < countries.length; n++) {
	    System.out.print(countries[n]);

            // for each country create a locale, don't care about language
	    Locale locale = new Locale("", countries[n]);
            // display friendly name for country
	    System.out.println("  " + locale.getDisplayCountry());
	}
    }
}
This information is hard-coded as an array of strings into the Locale.java source code, so the class definition is unstable


Language codes


Java ISO language listing

The following program lists the 2-letter language codes and their names. It cannot handle 3-letter codes. It is not complete.


import java.util.Locale;

public class ISOLanguages {

    public static void main(String[] args) {
	// get all ISO 2-letter language codes
	String[] languages = Locale.getISOLanguages();
	for (int n = 0; n < languages.length; n++) {
	    // print 2-letter code
	    System.out.print(languages[n]);

	    // construct locale with language
	    Locale locale = new Locale(languages[n]);
	    // print language name if known
	    System.out.println(" " + locale.getDisplayLanguage());
	}
    }
}
This information is hard-coded as an array of strings into the Locale.java source code, so the class definition is unstable


Variants


Locale representation - ASCII

Locales can be written in textual form


Locale listing

A Java program to list information about all known locales is


import java.util.Locale;

public class ListLocales {

    public static void main(String[] args) {
	Locale[] availableLocales = Locale.getAvailableLocales();
	for (int n = 0; n < availableLocales.length; n++) {
	    Locale locale = availableLocales[n];
	    String country = locale.getDisplayCountry();
	    String country2 = locale.getCountry();
	    String country3 = locale.getISO3Country();
	    String language = locale.getDisplayLanguage();
	    String language2 = locale.getLanguage();
	    String language3 = locale.getISO3Language();
	    System.out.println(language2 + "_" + country2 + 
                               "  (" + language3 + "_" + country3 + ")" +
                               "  " + language + "  " + country);
	}
    }
}
Supported locales for Java 1.4.2 are described in http://java.sun.com/j2se/1.4.2/docs/guide/intl/locale.doc.html


Locale display


Locales in ANSI C


C Locale


GNU C


Unix locale


Java


Locale names


Issues with locales


Summary


Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Fri Mar 11 22:03:36 EST 2005
Copyright ©Jan Newmarch
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.