Index: gnu/java/locale/LocaleHelper.java =================================================================== RCS file: gnu/java/locale/LocaleHelper.java diff -N gnu/java/locale/LocaleHelper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ gnu/java/locale/LocaleHelper.java 31 Jan 2005 01:17:23 -0000 @@ -0,0 +1,161 @@ +/* LocaleHelper.java -- helper routines for localization + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +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 gnu.java.locale; + +import java.util.Locale; +import java.util.Hashtable; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * This class provides common helper methods + * for handling localized data. + * + * @author Andrew John Hughes (address@hidden) + * @see java.util.Locale + * @see java.util.ResourceBundle + */ +public class LocaleHelper +{ + + /** + * This method is used by the localized name lookup methods to retrieve + * the localized name of a particular piece of locale data. + * If the display name can not be localized to the supplied + * locale, it will fall back on other output in the following order: + *
+ *+ * If the supplied key is merely the empty string, then the empty string is + * returned. + *
+ * + * @param inLocale the locale to use for formatting the display string. + * @param key the locale data used as a key to the localized lookup tables. + * @param name the name of the hashtable containing the localized data. + * @param checkEnglish true if the method should fall back on data + * from the English locale. + * @param checkRoot true if the method should fall back on data from the + * unlocalized root locale. + * @return aString
, hopefully containing the localized
+ * variant of the input data.
+ * @throws NullPointerException if inLocale
is null.
+ */
+ public static String getLocalizedString(Locale inLocale, String key,
+ String name, boolean checkEnglish,
+ boolean checkRoot)
+ {
+ String localizedString;
+ Hashtable table;
+
+ if (key.equals(""))
+ return "";
+ /* Localize to inLocale */
+ try
+ {
+ table = (Hashtable)
+ ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
+ inLocale).getObject(name);
+ localizedString = (String) table.get(key);
+ }
+ catch (MissingResourceException exception)
+ {
+ localizedString = null;
+ }
+ /* Localize to default locale */
+ if (localizedString == null)
+ {
+ try
+ {
+ ResourceBundle bundle;
+
+ bundle =
+ ResourceBundle.getBundle("gnu.java.locale.LocaleInformation");
+ table = (Hashtable) bundle.getObject(name);
+ localizedString = (String) table.get(key);
+ }
+ catch (MissingResourceException exception)
+ {
+ localizedString = null;
+ }
+ }
+ /* Localize to English */
+ if (localizedString == null && checkEnglish)
+ {
+ try
+ {
+ table = (Hashtable)
+ ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
+ Locale.ENGLISH).getObject(name);
+ localizedString= (String) table.get(key);
+ }
+ catch (MissingResourceException exception)
+ {
+ localizedString = null;
+ }
+ }
+ /* Return unlocalized version */
+ if (localizedString == null && checkRoot)
+ {
+ try
+ {
+ table = (Hashtable)
+ ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
+ new Locale("","","")).getObject(name);
+ localizedString= (String) table.get(key);
+ }
+ catch (MissingResourceException exception)
+ {
+ localizedString = null;
+ }
+ }
+ /* Return original input string */
+ if (localizedString == null)
+ {
+ localizedString = key;
+ }
+ return localizedString;
+ }
+}
+
Index: java/util/Currency.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Currency.java,v
retrieving revision 1.13
diff -u -3 -p -u -r1.13 Currency.java
--- java/util/Currency.java 20 Jan 2005 14:39:27 -0000 1.13
+++ java/util/Currency.java 31 Jan 2005 01:17:40 -0000
@@ -38,7 +38,7 @@ exception statement from your version. *
package java.util;
-import gnu.java.locale.LocaleInformation;
+import gnu.java.locale.LocaleHelper;
import java.io.IOException;
import java.io.ObjectStreamException;
@@ -93,17 +93,6 @@ public final class Currency
* @serial the number of fraction digits
*/
private transient int fractionDigits;
-
- /**
- * The currency symbol used when formatting currency strings.
- * When this field is null
the currencyCode
- * has to be used instead.
- *
- * @see #getSymbol()
- * @see #getSymbol(java.util.Locale)
- * @serial the currency symbol, or null
- */
- private transient String currencySymbol;
/**
* A cached map of country codes
@@ -207,9 +196,6 @@ public final class Currency
}
/* Retrieve the fraction digits */
fractionDigits = Integer.parseInt(properties.getProperty(fractionDigitsKey));
- /* Get currency symbol */
- currencySymbol =
- (String) LocaleInformation.getCurrencySymbols().get(currencyCode);
}
/**
@@ -360,16 +346,16 @@ public final class Currency
/**
* This method returns the symbol which precedes or follows a
- * value in this particular currency. In cases where there is no
- * such symbol for the currency, the ISO 4217 currency
- * code is returned.
+ * value in this particular currency in the default locale.
+ * In cases where there is no such symbol for the currency,
+ * the ISO 4217 currency code is returned.
*
* @return the currency symbol, or the ISO 4217 currency code if
* one doesn't exist.
*/
public String getSymbol()
{
- return currencySymbol == null ? getCurrencyCode() : currencySymbol;
+ return getSymbol(Locale.getDefault());
}
/**
@@ -399,11 +385,8 @@ public final class Currency
*/
public String getSymbol(Locale locale)
{
- /*
- We don't currently have the currency symbols, so we always
- return the currency code.
- */
- return getCurrencyCode();
+ return LocaleHelper.getLocalizedString(locale, currencyCode,
+ "currenciesSymbol", false, true);
}
/**
Index: java/util/Locale.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Locale.java,v
retrieving revision 1.25
diff -u -3 -p -u -r1.25 Locale.java
--- java/util/Locale.java 30 Dec 2004 19:26:49 -0000 1.25
+++ java/util/Locale.java 31 Jan 2005 01:17:40 -0000
@@ -39,6 +39,7 @@ exception statement from your version. *
package java.util;
import gnu.classpath.SystemProperties;
+import gnu.java.locale.LocaleHelper;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -617,7 +618,8 @@ public final class Locale implements Ser
*/
public String getDisplayLanguage(Locale inLocale)
{
- return getDisplayString(inLocale, language, "languages");
+ return LocaleHelper.getLocalizedString(inLocale, language,
+ "languages", true, false);
}
/**
@@ -663,7 +665,8 @@ public final class Locale implements Ser
*/
public String getDisplayCountry(Locale inLocale)
{
- return getDisplayString(inLocale, country, "territories");
+ return LocaleHelper.getLocalizedString(inLocale, country,
+ "territories", true, false);
}
/**
@@ -688,10 +691,10 @@ public final class Locale implements Ser
* 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 Locale.UK
, 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:
+ * variant, and the specified locale is Locale.UK
, 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:
*
* - * If the language is unspecified by this locale, then the empty string is - * returned. - *
- * - * @param inLocale the locale to use for formatting the display string. - * @param key the locale data used as a key to the localized lookup tables. - * @param tableName the name of the hashtable containing the localized data. - */ - private String getDisplayString(Locale inLocale, String key, String tableName) - { - String displayString; - Hashtable table; - - if (key.equals("")) - { - return ""; - } - /* Display in inLocale */ - try - { - table = (Hashtable) - ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", - inLocale).getObject(tableName); - displayString = (String) table.get(key); - } - catch (MissingResourceException exception) - { - displayString = null; - } - /* Display in default locale */ - if (displayString == null) - { - try - { - ResourceBundle bundle; - - bundle = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation"); - table = (Hashtable) bundle.getObject(tableName); - displayString = (String) table.get(key); - } - catch (MissingResourceException exception) - { - displayString = null; - } - } - /* Display in English */ - if (displayString == null) - { - try - { - table = (Hashtable) - ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", - Locale.ENGLISH).getObject(tableName); - displayString= (String) table.get(key); - } - catch (MissingResourceException exception) - { - displayString = null; - } - } - /* Display ISO code */ - if (displayString == null) - { - displayString = key; - } - return displayString; + return LocaleHelper.getLocalizedString(inLocale, variant, "variants", + true, false); } /**