Index: java/util/Locale.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/util/Locale.java,v retrieving revision 1.21 diff -u -r1.21 Locale.java --- java/util/Locale.java 6 Dec 2004 20:43:13 -0000 1.21 +++ java/util/Locale.java 19 Dec 2004 19:11:47 -0000 @@ -35,6 +35,7 @@ obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.util; import gnu.classpath.SystemProperties; @@ -76,56 +77,56 @@ * @see java.text.Collator * @author Jochen Hoenicke * @author Paul Fisher - * @author Eric Blake + * @author Eric Blake (address@hidden) * @since 1.1 * @status updated to 1.4 */ public final class Locale implements Serializable, Cloneable { /** Locale which represents the English language. */ - public static final Locale ENGLISH = new Locale("en"); + public static final Locale ENGLISH = getLocale("en"); /** Locale which represents the French language. */ - public static final Locale FRENCH = new Locale("fr"); + public static final Locale FRENCH = getLocale("fr"); /** Locale which represents the German language. */ - public static final Locale GERMAN = new Locale("de"); + public static final Locale GERMAN = getLocale("de"); /** Locale which represents the Italian language. */ - public static final Locale ITALIAN = new Locale("it"); + public static final Locale ITALIAN = getLocale("it"); /** Locale which represents the Japanese language. */ - public static final Locale JAPANESE = new Locale("ja"); + public static final Locale JAPANESE = getLocale("ja"); /** Locale which represents the Korean language. */ - public static final Locale KOREAN = new Locale("ko"); + public static final Locale KOREAN = getLocale("ko"); /** Locale which represents the Chinese language. */ - public static final Locale CHINESE = new Locale("zh"); + public static final Locale CHINESE = getLocale("zh"); /** Locale which represents the Chinese language as used in China. */ - public static final Locale SIMPLIFIED_CHINESE = new Locale("zh", "CN"); + public static final Locale SIMPLIFIED_CHINESE = getLocale("zh", "CN"); /** * Locale which represents the Chinese language as used in Taiwan. * Same as TAIWAN Locale. */ - public static final Locale TRADITIONAL_CHINESE = new Locale("zh", "TW"); + public static final Locale TRADITIONAL_CHINESE = getLocale("zh", "TW"); /** Locale which represents France. */ - public static final Locale FRANCE = new Locale("fr", "FR"); + public static final Locale FRANCE = getLocale("fr", "FR"); /** Locale which represents Germany. */ - public static final Locale GERMANY = new Locale("de", "DE"); + public static final Locale GERMANY = getLocale("de", "DE"); /** Locale which represents Italy. */ - public static final Locale ITALY = new Locale("it", "IT"); + public static final Locale ITALY = getLocale("it", "IT"); /** Locale which represents Japan. */ - public static final Locale JAPAN = new Locale("ja", "JP"); + public static final Locale JAPAN = getLocale("ja", "JP"); /** Locale which represents Korea. */ - public static final Locale KOREA = new Locale("ko", "KR"); + public static final Locale KOREA = getLocale("ko", "KR"); /** * Locale which represents China. @@ -146,16 +147,16 @@ public static final Locale TAIWAN = TRADITIONAL_CHINESE; /** Locale which represents the United Kingdom. */ - public static final Locale UK = new Locale("en", "GB"); + public static final Locale UK = getLocale("en", "GB"); /** Locale which represents the United States. */ - public static final Locale US = new Locale("en", "US"); + public static final Locale US = getLocale("en", "US"); /** Locale which represents the English speaking portion of Canada. */ - public static final Locale CANADA = new Locale("en", "CA"); + public static final Locale CANADA = getLocale("en", "CA"); /** Locale which represents the French speaking portion of Canada. */ - public static final Locale CANADA_FRENCH = new Locale("fr", "CA"); + public static final Locale CANADA_FRENCH = getLocale("fr", "CA"); /** * Compatible with JDK 1.1+. @@ -191,15 +192,54 @@ private transient int hashcode; /** + * Array storing all available locales. + */ + private transient static Locale[] availableLocales; + + /** + * Locale cache. Only created locale objects are stored. + * Contains all supported locales when getAvailableLocales() + * got called. + */ + private transient static HashMap localeMap; + + /** * The default locale. Except for during bootstrapping, this should never be * null. Note the logic in the main constructor, to detect when * bootstrapping has completed. */ private static Locale defaultLocale = - new Locale(SystemProperties.getProperty("user.language", "en"), - SystemProperties.getProperty("user.region", ""), - SystemProperties.getProperty("user.variant", "")); + getLocale(SystemProperties.getProperty("user.language", "en"), + SystemProperties.getProperty("user.region", ""), + SystemProperties.getProperty("user.variant", "")); + + private static Locale getLocale(String language) + { + return getLocale(language, "", ""); + } + + private static Locale getLocale(String language, String region) + { + return getLocale(language, region, ""); + } + + private static Locale getLocale(String language, String region, String variant) + { + if (localeMap == null) + localeMap = new HashMap(256); + String name = language + "_" + region + "_" + variant; + Locale locale = (Locale) localeMap.get(name); + + if (locale == null) + { + locale = new Locale(language, region, variant); + localeMap.put(name, locale); + } + + return locale; + } + /** * Convert new iso639 codes to the old ones. * @@ -306,24 +346,44 @@ * * @return the installed locales */ - public static Locale[] getAvailableLocales() + public synchronized static Locale[] getAvailableLocales() { - /* I only return those for which localized language - * or country information exists. - * XXX - remove hard coded list, and implement more locales (Sun's JDK 1.4 - * has 148 installed locales!). - */ - return new Locale[] - { - ENGLISH, FRENCH, GERMAN, new Locale("ga", "") - }; + if (availableLocales == null) + { + String[] localeNames = LocaleData.localeNames; + availableLocales = new Locale[localeNames.length]; + + for (int i = 0; i < localeNames.length; i++) + { + String language; + String region = ""; + String variant = ""; + String name = localeNames[i]; + + language = name.substring(0, 2); + + if (name.length() > 2) + region = name.substring(3); + + int index = region.indexOf("_"); + if (index > 0) + { + variant = region.substring(index + 1); + region = region.substring(0, index - 1); + } + + availableLocales[i] = getLocale(language, region, variant); + } + } + + return availableLocales; } /** * Returns a list of all 2-letter uppercase country codes as defined * in ISO 3166. * - * @return a list of acceptible country codes + * @return a list of acceptable country codes */ public static String[] getISOCountries() { @@ -542,7 +602,7 @@ try { ResourceBundle bundle - = ResourceBundle.getBundle("gnu.java.locale.iso639", locale); + = ResourceBundle.getBundle("java.util.iso639", locale); return bundle.getString(language); } catch (MissingResourceException ex) @@ -580,7 +640,7 @@ try { ResourceBundle bundle = - ResourceBundle.getBundle("gnu.java.locale.iso3166", locale); + ResourceBundle.getBundle("java.util.iso3166", locale); return bundle.getString(country); } catch (MissingResourceException ex) Index: scripts/generate-locale-list.sh =================================================================== RCS file: scripts/generate-locale-list.sh diff -N scripts/generate-locale-list.sh --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/generate-locale-list.sh 19 Dec 2004 19:11:47 -0000 @@ -0,0 +1,59 @@ +#!/bin/sh + +CLASSPATH_SRCDIR=`dirname $0`/.. + +cd $CLASSPATH_SRCDIR/gnu/java/locale + +echo "/* LocaleData.java --" +echo " Copyright (C) 2004 Free Software Foundation, Inc." +echo +echo "This file is part of GNU Classpath." +echo +echo "GNU Classpath is free software; you can redistribute it and/or modify" +echo "it under the terms of the GNU General Public License as published by" +echo "the Free Software Foundation; either version 2, or (at your option)" +echo "any later version." +echo +echo "GNU Classpath is distributed in the hope that it will be useful, but" +echo "WITHOUT ANY WARRANTY; without even the implied warranty of" +echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU" +echo "General Public License for more details." +echo +echo "You should have received a copy of the GNU General Public License" +echo "along with GNU Classpath; see the file COPYING. If not, write to the" +echo "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA" +echo "02111-1307 USA." +echo +echo "Linking this library statically or dynamically with other modules is" +echo "making a combined work based on this library. Thus, the terms and" +echo "conditions of the GNU General Public License cover the whole" +echo "combination." +echo +echo "As a special exception, the copyright holders of this library give you" +echo "permission to link this library with independent modules to produce an" +echo "executable, regardless of the license terms of these independent" +echo "modules, and to copy and distribute the resulting executable under" +echo "terms of your choice, provided that you also meet, for each linked" +echo "independent module, the terms and conditions of the license of that" +echo "module. An independent module is a module which is not derived from" +echo "or based on this library. If you modify this library, you may extend" +echo "this exception to your version of the library, but you are not" +echo "obligated to do so. If you do not wish to do so, delete this" +echo "exception statement from your version. */" +echo +echo +echo "package java.util;" +echo +echo "// This file was automatically generated by scripts/generate-locale-list.sh" +echo +echo +echo "class LocaleData" +echo "{" +echo " public static String[] localeNames =" +echo " {" + +ls LocaleInformation_*.java | xargs -n 1 echo | sed -e 's/LocaleInformation_\(.*\)\.java/\1/' | +while read locale ; do echo " \"$locale\"," ; done + +echo " };" +echo "}" Index: lib/Makefile.am =================================================================== RCS file: /cvsroot/classpath/classpath/lib/Makefile.am,v retrieving revision 1.70 diff -u -r1.70 Makefile.am --- lib/Makefile.am 17 Dec 2004 16:00:33 -0000 1.70 +++ lib/Makefile.am 19 Dec 2004 19:11:47 -0000 @@ -79,6 +79,7 @@ classes: genclasses genclasses: gen-classlist.sh standard.omit + $(top_srcdir)/scripts/generate-locale-list.sh > $(top_srcdir)/java/util/LocaleData.java top_builddir=$(top_builddir) $(SHELL) ./gen-classlist.sh standard $(JAVA_DEPEND): genclasses