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
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
Locales can be written in textual form
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
class does however have methods
to display locale information using locales themselves
to guide the display
import java.util.Locale;
public class LocaleDisplay {
public static void main(String[] args) {
Locale defaultLocale = Locale.getDefault();
System.out.println("Default locale is " + defaultLocale.toString());
Locale[] locales = new Locale[] {
Locale.US,
Locale.FRANCE,
new Locale("fr", "BE"),
new Locale("fr", "LU"),
new Locale("de", "CH"),
new Locale("fr", "CH"),
new Locale("it", "CH"),
Locale.TRADITIONAL_CHINESE
};
for (int n = 0; n < locales.length; n++) {
Locale.setDefault(defaultLocale);
System.out.println("--------------\nIn default locale for " +
locales[n].toString());
System.out.println(" Country: " + locales[n].getDisplayCountry());
System.out.println(" Language: " + locales[n].getDisplayLanguage());
System.out.println("In locale " +
locales[n].getLanguage() + "_" +
locales[n].getCountry());
Locale.setDefault(locales[n]);
System.out.println(" Country: " + locales[n].getDisplayCountry());
System.out.println(" Language: " + locales[n].getDisplayLanguage());
}
}
}
java LocaleDisplay
is
Default locale is en_US
--------------
In default locale for en_US
Country: United States
Language: English
In locale en_US
Country: United States
Language: English
--------------
In default locale for fr_FR
Country: France
Language: French
In locale fr_FR
Country: France
Language: français
java -Duser.language=fr -Duser.country=FR LocaleDisplay
Default locale is fr_FR
--------------
In default locale for en_US
Country: Etats-Unis
Language: anglais
In locale en_US
Country: United States
Language: English
--------------
In default locale for fr_FR
Country: France
Language: français
In locale fr_FR
Country: France
Language: français
where the default locale print statements have resulted in different output
JAVA_SRC/sun/text/resources
such as
LocaleElements.java
, LocaleElements_fr.java
and LocaleElements_fr_CA.java
locale.h
defines structures, functions and constants
for i18n
lconv
holds all i18n related information for number and money
formatting
struct lconv
/* Describes formatting of monetary and other numeric values: */
char* decimal_point;
/* decimal point for non-monetary values */
char* grouping;
/* sizes of digit groups for non-monetary values */
char* thousands_sep;
/* separator for digit groups for non-monetary values (left of "decimal point") */
char* currency_symbol;
/* currency symbol */
char* int_curr_symbol;
/* international currency symbol */
char* mon_decimal_point;
/* decimal point for monetary values */
char* mon_grouping;
/* sizes of digit groups for monetary values */
char* mon_thousands_sep;
/* separator for digit groups for monetary values (left of "decimal point") */
char* negative_sign;
/* negative sign for monetary values */
char* positive_sign;
/* positive sign for monetary values */
char frac_digits;
/* number of digits to be displayed to right of "decimal point" for monetary values */
char int_frac_digits;
/* number of digits to be displayed to right of "decimal point" for international monetary values */
char n_cs_precedes;
/* whether currency symbol precedes (1) or follows (0) negative monetary values */
char n_sep_by_space;
/* whether currency symbol is (1) or is not (0) separated by space from negative monetary values */
char n_sign_posn;
/* format for negative monetary values:
0
parentheses surround quantity and currency symbol
1
sign precedes quantity and currency symbol
2
sign follows quantity and currency symbol
3
sign immediately precedes currency symbol
4
sign immediately follows currency symbol */
char p_cs_precedes;
/* whether currency symbol precedes (1) or follows (0) positive monetary values */
char p_sep_by_space;
/* whether currency symbol is (1) or is not (0) separated by space from non-negative monetary values */
char p_sign_posn;
/* format for non-negative monetary values, with values as for n_sign_posn */
}
LC_ALL
category argument for all categories
LC_NUMERIC
category for numeric formatting information
LC_MONETARY
category for monetary formatting information
LC_COLLATE
category for information affecting collating functions
LC_CTYPE
category for information affecting character class tests functions
LC_TIME
category for information affecting time conversions functions
struct lconv* localeconv(void);
/* returns pointer to formatting information for current locale */
char* setlocale(int category, const char* locale);
LC_MESSAGES
LC_PAPER
LC_NAME
LC_ADDRESS
LC_TELEPHONE
LC_MEASUREMENT
LC_IDENTIFICATION
POSIX
equivalent to the C locale
Locale
class is used to define a locale
Collator
and DateFormat