Merge from mainline.
[official-gcc.git] / libjava / classpath / gnu / java / locale / LocaleHelper.java
blobcec0147179f95aad7136de1ba612413fb33e2ac2
1 /* LocaleHelper.java -- helper routines for localization
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.locale;
41 import java.text.Collator;
42 import java.util.Locale;
43 import java.util.MissingResourceException;
44 import java.util.ResourceBundle;
46 /**
47 * This class provides common helper methods
48 * for handling localized data.
50 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
52 * @see java.util.Locale
53 * @see java.util.ResourceBundle
55 public class LocaleHelper
57 /**
58 * This method is used by the localized name lookup methods to retrieve
59 * the localized name of a particular piece of locale data.
60 * If the display name can not be localized to the supplied
61 * locale, it will fall back on other output in the following order:
63 * <ul>
64 * <li>the localized name in the default locale</li>
65 * <li>the localized name in English (optional)</li>
66 * <li>the localized name in the root locale bundle (optional)</li>
67 * <li>the localized input string</li>
68 * </ul>
69 * <p>
70 * If the supplied key is merely the empty string, then the empty string is
71 * returned.
72 * </p>
74 * @param inLocale the locale to use for formatting the display string.
75 * @param key the locale data used as a key to the localized lookup tables.
76 * @param name the name of the hashtable containing the localized data.
77 * @param checkEnglish true if the method should fall back on data
78 * from the English locale.
79 * @param checkRoot true if the method should fall back on data from the
80 * unlocalized root locale.
81 * @return a <code>String</code>, hopefully containing the localized
82 * variant of the input data.
83 * @throws NullPointerException if <code>inLocale</code> is null.
85 public static String getLocalizedString(Locale inLocale, String key,
86 String name, boolean checkEnglish,
87 boolean checkRoot)
89 String localizedString;
90 String property;
92 if (key.equals(""))
93 return "";
94 property = name + "." + key;
95 /* Localize to inLocale */
96 try
98 localizedString =
99 ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
100 inLocale).getString(property);
102 catch (MissingResourceException exception)
104 localizedString = null;
106 /* Localize to default locale */
107 if (localizedString == null)
109 try
111 ResourceBundle bundle;
113 bundle =
114 ResourceBundle.getBundle("gnu.java.locale.LocaleInformation");
115 localizedString = bundle.getString(property);
117 catch (MissingResourceException exception)
119 localizedString = null;
122 /* Localize to English */
123 if (localizedString == null && checkEnglish)
127 localizedString =
128 ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
129 Locale.ENGLISH).getString(property);
131 catch (MissingResourceException exception)
133 localizedString = null;
136 /* Return unlocalized version */
137 if (localizedString == null && checkRoot)
141 localizedString =
142 ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
143 new Locale("","","")
144 ).getString(property);
146 catch (MissingResourceException exception)
148 localizedString = null;
151 /* Return original input string */
152 if (localizedString == null)
154 localizedString = key;
156 return localizedString;
160 * Return an array of all the locales for which there is a
161 * {@link Collator} instance. A new array is returned each time.
163 public static Locale[] getCollatorLocales()
165 // For now we don't bother caching. This is probably
166 // not called very frequently. And, we would have to
167 // clone the array anyway.
168 if (LocaleData.collatorLocaleNames.length == 0)
169 return new Locale[] { Locale.US };
170 Locale[] result = new Locale[LocaleData.collatorLocaleNames.length];
171 for (int i = 0; i < result.length; ++i)
173 String language;
174 String region = "";
175 String variant = "";
176 String name = LocaleData.collatorLocaleNames[i];
178 language = name.substring(0, 2);
180 if (name.length() > 2)
181 region = name.substring(3);
183 int index = region.indexOf("_");
184 if (index > 0)
186 variant = region.substring(index + 1);
187 region = region.substring(0, index - 1);
190 result[i] = new Locale(language, region, variant);
192 return result;
196 * Return the number of locales we know of.
198 public static int getLocaleCount()
200 return LocaleData.localeNames.length;
204 * Return the Nth locale name.
206 public static String getLocaleName(int n)
208 return LocaleData.localeNames[n];