diff options
author | Michael Koch <konqueror@gmx.de> | 2004-12-19 19:12:09 +0000 |
---|---|---|
committer | Michael Koch <konqueror@gmx.de> | 2004-12-19 19:12:09 +0000 |
commit | ece79c7090a11e3054ccb9eb05d01d5b529f7ea8 (patch) | |
tree | e9262ddcb4452378ae498e9ab6c3b29a0d1d0d3f | |
parent | 69a8c73acc173389aa9efde0dbcaf4df6f864362 (diff) | |
download | classpath-ece79c7090a11e3054ccb9eb05d01d5b529f7ea8.tar.gz |
2004-12-19 Michael Koch <konqueror@gmx.de>
* java/util/Locale.java: Make sure all locales get only created once.
(availableLocales): New static field.
(localeMap): Likewise.
(getLocale): New private static method.
(getAvailableLocales): Re-implemented.
(getISOCountries): Fixed typo in javadoc.
(getDisplayLanguage): Load iso639.properties from correct package.
(getDisplayCountry): Load iso3166.properties from correct package.
* scripts/generate-locale-list.sh: New script.
* lib/Makefile.am: Call scripts/generate-locale-list.sh to generate
java/util/LocaleData.java.
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | java/util/Locale.java | 130 | ||||
-rw-r--r-- | lib/Makefile.am | 1 | ||||
-rwxr-xr-x | scripts/generate-locale-list.sh | 59 |
4 files changed, 169 insertions, 35 deletions
@@ -1,5 +1,19 @@ 2004-12-19 Michael Koch <konqueror@gmx.de> + * java/util/Locale.java: Make sure all locales get only created once. + (availableLocales): New static field. + (localeMap): Likewise. + (getLocale): New private static method. + (getAvailableLocales): Re-implemented. + (getISOCountries): Fixed typo in javadoc. + (getDisplayLanguage): Load iso639.properties from correct package. + (getDisplayCountry): Load iso3166.properties from correct package. + * scripts/generate-locale-list.sh: New script. + * lib/Makefile.am: Call scripts/generate-locale-list.sh to generate + java/util/LocaleData.java. + +2004-12-19 Michael Koch <konqueror@gmx.de> + * gnu/java/locale/LocaleInformation_aa.java, gnu/java/locale/LocaleInformation_aa_DJ.java, gnu/java/locale/LocaleInformation_aa_ER.java, diff --git a/java/util/Locale.java b/java/util/Locale.java index 7228514ad..c14393d41 100644 --- a/java/util/Locale.java +++ b/java/util/Locale.java @@ -35,6 +35,7 @@ this exception to your version of the library, but you are not 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 @@ import java.io.Serializable; * @see java.text.Collator * @author Jochen Hoenicke * @author Paul Fisher - * @author Eric Blake <ebb9@email.byu.edu> + * @author Eric Blake (ebb9@email.byu.edu) * @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 final class Locale implements Serializable, Cloneable 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 @@ public final class Locale implements Serializable, Cloneable 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 @@ public final class Locale implements Serializable, Cloneable * * @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 @@ public final class Locale implements Serializable, Cloneable 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 @@ public final class Locale implements Serializable, Cloneable try { ResourceBundle bundle = - ResourceBundle.getBundle("gnu.java.locale.iso3166", locale); + ResourceBundle.getBundle("java.util.iso3166", locale); return bundle.getString(country); } catch (MissingResourceException ex) diff --git a/lib/Makefile.am b/lib/Makefile.am index cff05f44a..fcee18061 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -79,6 +79,7 @@ resources: 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 diff --git a/scripts/generate-locale-list.sh b/scripts/generate-locale-list.sh new file mode 100755 index 000000000..f77ed0e31 --- /dev/null +++ b/scripts/generate-locale-list.sh @@ -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 "}" |