1 |
769 |
jeremybenn |
/* LocaleHelper.java -- helper routines for localization
|
2 |
|
|
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.java.locale;
|
40 |
|
|
|
41 |
|
|
import java.text.Collator;
|
42 |
|
|
import java.util.Locale;
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* This class provides common helper methods
|
46 |
|
|
* for handling localized data.
|
47 |
|
|
*
|
48 |
|
|
* @author Andrew John Hughes (gnu_andrew@member.fsf.org)
|
49 |
|
|
*
|
50 |
|
|
* @see java.util.Locale
|
51 |
|
|
* @see java.util.ResourceBundle
|
52 |
|
|
*/
|
53 |
|
|
public class LocaleHelper
|
54 |
|
|
{
|
55 |
|
|
/**
|
56 |
|
|
* <p>
|
57 |
|
|
* This method is used by the localized name lookup methods to
|
58 |
|
|
* retrieve the next locale to try. The next locale is derived
|
59 |
|
|
* from the supplied locale by applying the first applicable
|
60 |
|
|
* rule from the following:
|
61 |
|
|
* </p>
|
62 |
|
|
* <ol>
|
63 |
|
|
* <li>If the variant contains a <code>'_'</code>, then
|
64 |
|
|
* this and everything following it is trimmed.</li>
|
65 |
|
|
* <li>If the variant is non-empty, it is converted to
|
66 |
|
|
* an empty string.</li>
|
67 |
|
|
* <li>If the country is non-empty, it is converted to
|
68 |
|
|
* an empty string.</li>
|
69 |
|
|
* <li>If the language is non-empty, it is converted to
|
70 |
|
|
* an empty string (forming {@link java.util.Locale#ROOT})</li>
|
71 |
|
|
* </ol>
|
72 |
|
|
* <p>
|
73 |
|
|
* The base fallback locale is {@link java.util.Locale#ROOT}.
|
74 |
|
|
* </p>
|
75 |
|
|
*
|
76 |
|
|
* @param locale the locale for which a localized piece of
|
77 |
|
|
* data could not be obtained.
|
78 |
|
|
* @return the next fallback locale to try.
|
79 |
|
|
*/
|
80 |
|
|
public static Locale getFallbackLocale(Locale locale)
|
81 |
|
|
{
|
82 |
|
|
String language = locale.getLanguage();
|
83 |
|
|
String country = locale.getCountry();
|
84 |
|
|
String variant = locale.getVariant();
|
85 |
|
|
int uscore = variant.indexOf('_');
|
86 |
|
|
if (uscore != -1)
|
87 |
|
|
return new Locale(language, country,
|
88 |
|
|
variant.substring(0, uscore));
|
89 |
|
|
if (!variant.isEmpty())
|
90 |
|
|
return new Locale(language, country, "");
|
91 |
|
|
if (!country.isEmpty())
|
92 |
|
|
return new Locale(language, "", "");
|
93 |
|
|
return Locale.ROOT;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* Return an array of all the locales for which there is a
|
98 |
|
|
* {@link Collator} instance. A new array is returned each time.
|
99 |
|
|
*/
|
100 |
|
|
public static Locale[] getCollatorLocales()
|
101 |
|
|
{
|
102 |
|
|
// For now we don't bother caching. This is probably
|
103 |
|
|
// not called very frequently. And, we would have to
|
104 |
|
|
// clone the array anyway.
|
105 |
|
|
if (LocaleData.collatorLocaleNames.length == 0)
|
106 |
|
|
return new Locale[] { Locale.US };
|
107 |
|
|
Locale[] result = new Locale[LocaleData.collatorLocaleNames.length];
|
108 |
|
|
for (int i = 0; i < result.length; ++i)
|
109 |
|
|
{
|
110 |
|
|
String language;
|
111 |
|
|
String region = "";
|
112 |
|
|
String variant = "";
|
113 |
|
|
String name = LocaleData.collatorLocaleNames[i];
|
114 |
|
|
|
115 |
|
|
language = name.substring(0, 2);
|
116 |
|
|
|
117 |
|
|
if (name.length() > 2)
|
118 |
|
|
region = name.substring(3);
|
119 |
|
|
|
120 |
|
|
int index = region.indexOf("_");
|
121 |
|
|
if (index > 0)
|
122 |
|
|
{
|
123 |
|
|
variant = region.substring(index + 1);
|
124 |
|
|
region = region.substring(0, index - 1);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
result[i] = new Locale(language, region, variant);
|
128 |
|
|
}
|
129 |
|
|
return result;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/**
|
133 |
|
|
* Return the number of locales we know of.
|
134 |
|
|
*/
|
135 |
|
|
public static int getLocaleCount()
|
136 |
|
|
{
|
137 |
|
|
return LocaleData.localeNames.length;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Return the Nth locale name.
|
142 |
|
|
*/
|
143 |
|
|
public static String getLocaleName(int n)
|
144 |
|
|
{
|
145 |
|
|
return LocaleData.localeNames[n];
|
146 |
|
|
}
|
147 |
|
|
}
|