diff options
author | Michael Koch <konqueror@gmx.de> | 2005-05-06 06:52:44 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2005-05-06 06:52:44 +0000 |
commit | cb881fb176ce5cc2f4febe8ad21daa3b371d13cd (patch) | |
tree | 9d5a55604c5efcb5445f7133dc240640f82e6f11 /libjava/java/util | |
parent | 8108f99852f34a6b82184cff8fdb7640be46872b (diff) | |
download | gcc-cb881fb176ce5cc2f4febe8ad21daa3b371d13cd.zip gcc-cb881fb176ce5cc2f4febe8ad21daa3b371d13cd.tar.gz gcc-cb881fb176ce5cc2f4febe8ad21daa3b371d13cd.tar.bz2 |
2005-05-06 Michael Koch <konqueror@gmx.de>
* java/util/Locale.java
(defaultLocale): Use gnu.classpath.SystemProperties to get properties.
(getLocale): New methods. Use it everywhere where instances of Locales
are needed.
(getDisplayLanguage): Merged javadoc.
(getDisplayCountry): Likewise.
(getDisplayVariant): Likewise.
From-SVN: r99303
Diffstat (limited to 'libjava/java/util')
-rw-r--r-- | libjava/java/util/Locale.java | 181 |
1 files changed, 136 insertions, 45 deletions
diff --git a/libjava/java/util/Locale.java b/libjava/java/util/Locale.java index f4498c6..efa698c 100644 --- a/libjava/java/util/Locale.java +++ b/libjava/java/util/Locale.java @@ -1,5 +1,5 @@ /* Locale.java -- i18n locales - Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,6 +38,8 @@ exception statement from your version. */ package java.util; +import gnu.classpath.SystemProperties; + import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -76,55 +78,56 @@ import java.io.Serializable; * @author Jochen Hoenicke * @author Paul Fisher * @author Eric Blake (ebb9@email.byu.edu) + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) * @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. @@ -145,16 +148,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+. @@ -195,11 +198,49 @@ public final class Locale implements Serializable, Cloneable * bootstrapping has completed. */ private static Locale defaultLocale = - new Locale(System.getProperty("user.language", "en"), - System.getProperty("user.region", ""), - System.getProperty("user.variant", "")); + getLocale(SystemProperties.getProperty("user.language", "en"), + SystemProperties.getProperty("user.region", ""), + SystemProperties.getProperty("user.variant", "")); /** + * Retrieves the locale with the specified language from the cache. + * + * @param language the language of the locale to retrieve. + * @return the locale. + */ + private static Locale getLocale(String language) + { + return getLocale(language, "", ""); + } + + /** + * Retrieves the locale with the specified language and region + * from the cache. + * + * @param language the language of the locale to retrieve. + * @param region the region of the locale to retrieve. + * @return the locale. + */ + private static Locale getLocale(String language, String region) + { + return getLocale(language, region, ""); + } + + /** + * Retrieves the locale with the specified language, region + * and variant from the cache. + * + * @param language the language of the locale to retrieve. + * @param region the region of the locale to retrieve. + * @param variant the variant of the locale to retrieve. + * @return the locale. + */ + private static Locale getLocale(String language, String region, String variant) + { + return new Locale(language, region, variant); + } + + /** * Convert new iso639 codes to the old ones. * * @param language the language to check @@ -529,19 +570,36 @@ public final class Locale implements Serializable, Cloneable } /** - * Gets the language name suitable for display to the user, formatted - * for a specified locale. - * - * @param locale locale to use for formatting + * <p> + * Gets the name of the language specified by this locale, in a form suitable + * for display to the user. If possible, the display name will be localized + * to the specified locale. For example, if the locale instance is + * <code>Locale.GERMANY</code>, and the specified locale is <code>Locale.UK</code>, + * the result would be 'German'. Using the German locale would instead give + * 'Deutsch'. If the display name can not be localized to the supplied + * locale, it will fall back on other output in the following order: + * </p> + * <ul> + * <li>the display name in the default locale</li> + * <li>the display name in English</li> + * <li>the ISO code</li> + * </ul> + * <p> + * If the language is unspecified by this locale, then the empty string is + * returned. + * </p> + * + * @param inLocale the locale to use for formatting the display string. * @return the language name of this locale localized to the given locale, - * with the ISO code as backup + * with the default locale, English and the ISO code as backups. + * @throws NullPointerException if the supplied locale is null. */ - public String getDisplayLanguage(Locale locale) + public String getDisplayLanguage(Locale inLocale) { try { ResourceBundle bundle - = ResourceBundle.getBundle("gnu.java.locale.iso639", locale); + = ResourceBundle.getBundle("gnu.java.locale.iso639", inLocale); return bundle.getString(language); } catch (MissingResourceException ex) @@ -567,19 +625,36 @@ public final class Locale implements Serializable, Cloneable } /** - * Gets the country name suitable for display to the user, formatted - * for a specified locale. - * - * @param locale locale to use for formatting + * <p> + * Gets the name of the country specified by this locale, in a form suitable + * for display to the user. If possible, the display name will be localized + * to the specified locale. For example, if the locale instance is + * <code>Locale.GERMANY</code>, and the specified locale is <code>Locale.UK</code>, + * the result would be 'Germany'. Using the German locale would instead give + * 'Deutschland'. If the display name can not be localized to the supplied + * locale, it will fall back on other output in the following order: + * </p> + * <ul> + * <li>the display name in the default locale</li> + * <li>the display name in English</li> + * <li>the ISO code</li> + * </ul> + * <p> + * If the country is unspecified by this locale, then the empty string is + * returned. + * </p> + * + * @param inLocale the locale to use for formatting the display string. * @return the country name of this locale localized to the given locale, - * with the ISO code as backup + * with the default locale, English and the ISO code as backups. + * @throws NullPointerException if the supplied locale is null. */ - public String getDisplayCountry(Locale locale) + public String getDisplayCountry(Locale inLocale) { try { ResourceBundle bundle = - ResourceBundle.getBundle("gnu.java.locale.iso3166", locale); + ResourceBundle.getBundle("gnu.java.locale.iso3166", inLocale); return bundle.getString(country); } catch (MissingResourceException ex) @@ -605,15 +680,31 @@ public final class Locale implements Serializable, Cloneable } /** - * Returns the variant name of this locale localized to the - * given locale. If the localized is not found, the variant code - * itself is returned. - * - * @param locale locale to use for formatting - * @return the variant code of this locale localized to the given locale, - * with the ISO code as backup - */ - public String getDisplayVariant(Locale locale) + * <p> + * Gets the name of the variant specified by this locale, in a form suitable + * for display to the user. If possible, the display name will be localized + * to the specified locale. For example, if the locale instance is a revised + * variant, and the specified locale is <code>Locale.UK</code>, the result + * would be 'REVISED'. Using the German locale would instead give + * 'Revidiert'. If the display name can not be localized to the supplied + * locale, it will fall back on other output in the following order: + * </p> + * <ul> + * <li>the display name in the default locale</li> + * <li>the display name in English</li> + * <li>the ISO code</li> + * </ul> + * <p> + * If the variant is unspecified by this locale, then the empty string is + * returned. + * </p> + * + * @param inLocale the locale to use for formatting the display string. + * @return the variant name of this locale localized to the given locale, + * with the default locale, English and the ISO code as backups. + * @throws NullPointerException if the supplied locale is null. + */ + public String getDisplayVariant(Locale inLocale) { // XXX - load a bundle? return variant; |