ICU.java revision 4c94a6128d1fcfaa75539b36c9f50d0c75df6144
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.icu;
18
19import java.util.LinkedHashSet;
20import java.util.Locale;
21
22/**
23 * Makes ICU data accessible to Java.
24 */
25public final class ICU {
26    /**
27     * Cache for ISO language names.
28     */
29    private static String[] isoLanguages;
30
31    /**
32     * Cache for ISO country names.
33     */
34    private static String[] isoCountries;
35
36    /**
37     * Returns an array of ISO language names (two-letter codes), fetched either
38     * from ICU's database or from our memory cache.
39     *
40     * @return The array.
41     */
42    public static String[] getISOLanguages() {
43        if (isoLanguages == null) {
44            isoLanguages = getISOLanguagesNative();
45        }
46        return isoLanguages.clone();
47    }
48
49    /**
50     * Returns an array of ISO country names (two-letter codes), fetched either
51     * from ICU's database or from our memory cache.
52     *
53     * @return The array.
54     */
55    public static String[] getISOCountries() {
56        if (isoCountries == null) {
57            isoCountries = getISOCountriesNative();
58        }
59        return isoCountries.clone();
60    }
61
62    /**
63     * Returns the appropriate {@code Locale} given a {@code String} of the form returned
64     * by {@code toString}. This is very lenient, and doesn't care what's between the underscores:
65     * this method can parse strings that {@code Locale.toString} won't produce.
66     * Used to remove duplication.
67     */
68    public static Locale localeFromString(String localeName) {
69        int first = localeName.indexOf('_');
70        int second = localeName.indexOf('_', first + 1);
71        if (first == -1) {
72            // Language only ("ja").
73            return new Locale(localeName);
74        } else if (second == -1) {
75            // Language and country ("ja_JP").
76            return new Locale(localeName.substring(0, first), localeName.substring(first + 1));
77        } else {
78            // Language and country and variant ("ja_JP_TRADITIONAL").
79            return new Locale(localeName.substring(0, first), localeName.substring(first + 1, second), localeName.substring(second + 1));
80        }
81    }
82
83    public static Locale[] localesFromStrings(String[] localeNames) {
84        // We need to remove duplicates caused by the conversion of "he" to "iw", et cetera.
85        // Java needs the obsolete code, ICU needs the modern code, but we let ICU know about
86        // both so that we never need to convert back when talking to it.
87        LinkedHashSet<Locale> set = new LinkedHashSet<Locale>();
88        for (String localeName : localeNames) {
89            set.add(localeFromString(localeName));
90        }
91        return set.toArray(new Locale[set.size()]);
92    }
93
94    private static Locale[] availableLocalesCache;
95    public static Locale[] getAvailableLocales() {
96        if (availableLocalesCache == null) {
97            availableLocalesCache = localesFromStrings(getAvailableLocalesNative());
98        }
99        return availableLocalesCache.clone();
100    }
101
102    public static Locale[] getAvailableBreakIteratorLocales() {
103        return localesFromStrings(getAvailableBreakIteratorLocalesNative());
104    }
105
106    public static Locale[] getAvailableCalendarLocales() {
107        return localesFromStrings(getAvailableCalendarLocalesNative());
108    }
109
110    public static Locale[] getAvailableCollatorLocales() {
111        return localesFromStrings(getAvailableCollatorLocalesNative());
112    }
113
114    public static Locale[] getAvailableDateFormatLocales() {
115        return localesFromStrings(getAvailableDateFormatLocalesNative());
116    }
117
118    public static Locale[] getAvailableDateFormatSymbolsLocales() {
119        return getAvailableDateFormatLocales();
120    }
121
122    public static Locale[] getAvailableDecimalFormatSymbolsLocales() {
123        return getAvailableNumberFormatLocales();
124    }
125
126    public static Locale[] getAvailableNumberFormatLocales() {
127        return localesFromStrings(getAvailableNumberFormatLocalesNative());
128    }
129
130    /**
131     * Returns the ICU version in use. This is "4.4" for gingerbread, for example.
132     */
133    public static native String getIcuVersion();
134
135    /**
136     * Returns the Unicode version our ICU supports. This is "5.2" for gingerbread, for example.
137     */
138    public static native String getUnicodeVersion();
139
140    // --- Case mapping.
141
142    public static native String toLowerCase(String s, String localeName);
143    public static native String toUpperCase(String s, String localeName);
144
145    // --- Native methods accessing ICU's database.
146
147    private static native String[] getAvailableBreakIteratorLocalesNative();
148    private static native String[] getAvailableCalendarLocalesNative();
149    private static native String[] getAvailableCollatorLocalesNative();
150    private static native String[] getAvailableDateFormatLocalesNative();
151    private static native String[] getAvailableLocalesNative();
152    private static native String[] getAvailableNumberFormatLocalesNative();
153
154    public static native String[] getAvailableCurrencyCodes();
155    public static native String getCurrencyCode(String countryCode);
156    public static native String getCurrencyDisplayName(String locale, String currencyCode);
157    public static native int getCurrencyFractionDigits(String currencyCode);
158    public static native String getCurrencySymbol(String locale, String currencyCode);
159
160    public static native String getDisplayCountryNative(String countryCode, String locale);
161    public static native String getDisplayLanguageNative(String languageCode, String locale);
162    public static native String getDisplayVariantNative(String variantCode, String locale);
163
164    public static native String getISO3CountryNative(String locale);
165    public static native String getISO3LanguageNative(String locale);
166
167    public static native String addLikelySubtags(String locale);
168    public static native String getScript(String locale);
169
170    private static native String[] getISOLanguagesNative();
171    private static native String[] getISOCountriesNative();
172
173    static native boolean initLocaleDataImpl(String locale, LocaleData result);
174}
175