• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package libcore.icu;
18 
19 import java.util.LinkedHashSet;
20 import java.util.Locale;
21 
22 /**
23  * Makes ICU data accessible to Java.
24  */
25 public 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      */
getISOLanguages()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      */
getISOCountries()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      */
localeFromString(String localeName)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 
localesFromStrings(String[] localeNames)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;
getAvailableLocales()95     public static Locale[] getAvailableLocales() {
96         if (availableLocalesCache == null) {
97             availableLocalesCache = localesFromStrings(getAvailableLocalesNative());
98         }
99         return availableLocalesCache.clone();
100     }
101 
getAvailableBreakIteratorLocales()102     public static Locale[] getAvailableBreakIteratorLocales() {
103         return localesFromStrings(getAvailableBreakIteratorLocalesNative());
104     }
105 
getAvailableCalendarLocales()106     public static Locale[] getAvailableCalendarLocales() {
107         return localesFromStrings(getAvailableCalendarLocalesNative());
108     }
109 
getAvailableCollatorLocales()110     public static Locale[] getAvailableCollatorLocales() {
111         return localesFromStrings(getAvailableCollatorLocalesNative());
112     }
113 
getAvailableDateFormatLocales()114     public static Locale[] getAvailableDateFormatLocales() {
115         return localesFromStrings(getAvailableDateFormatLocalesNative());
116     }
117 
getAvailableDateFormatSymbolsLocales()118     public static Locale[] getAvailableDateFormatSymbolsLocales() {
119         return getAvailableDateFormatLocales();
120     }
121 
getAvailableDecimalFormatSymbolsLocales()122     public static Locale[] getAvailableDecimalFormatSymbolsLocales() {
123         return getAvailableNumberFormatLocales();
124     }
125 
getAvailableNumberFormatLocales()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      */
getIcuVersion()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      */
getUnicodeVersion()138     public static native String getUnicodeVersion();
139 
140     // --- Case mapping.
141 
toLowerCase(String s, String localeName)142     public static native String toLowerCase(String s, String localeName);
toUpperCase(String s, String localeName)143     public static native String toUpperCase(String s, String localeName);
144 
145     // --- Native methods accessing ICU's database.
146 
getAvailableBreakIteratorLocalesNative()147     private static native String[] getAvailableBreakIteratorLocalesNative();
getAvailableCalendarLocalesNative()148     private static native String[] getAvailableCalendarLocalesNative();
getAvailableCollatorLocalesNative()149     private static native String[] getAvailableCollatorLocalesNative();
getAvailableDateFormatLocalesNative()150     private static native String[] getAvailableDateFormatLocalesNative();
getAvailableLocalesNative()151     private static native String[] getAvailableLocalesNative();
getAvailableNumberFormatLocalesNative()152     private static native String[] getAvailableNumberFormatLocalesNative();
153 
getAvailableCurrencyCodes()154     public static native String[] getAvailableCurrencyCodes();
getCurrencyCode(String countryCode)155     public static native String getCurrencyCode(String countryCode);
getCurrencyDisplayName(String locale, String currencyCode)156     public static native String getCurrencyDisplayName(String locale, String currencyCode);
getCurrencyFractionDigits(String currencyCode)157     public static native int getCurrencyFractionDigits(String currencyCode);
getCurrencySymbol(String locale, String currencyCode)158     public static native String getCurrencySymbol(String locale, String currencyCode);
159 
getDisplayCountryNative(String countryCode, String locale)160     public static native String getDisplayCountryNative(String countryCode, String locale);
getDisplayLanguageNative(String languageCode, String locale)161     public static native String getDisplayLanguageNative(String languageCode, String locale);
getDisplayVariantNative(String variantCode, String locale)162     public static native String getDisplayVariantNative(String variantCode, String locale);
163 
getISO3CountryNative(String locale)164     public static native String getISO3CountryNative(String locale);
getISO3LanguageNative(String locale)165     public static native String getISO3LanguageNative(String locale);
166 
addLikelySubtags(String locale)167     public static native String addLikelySubtags(String locale);
getScript(String locale)168     public static native String getScript(String locale);
169 
getISOLanguagesNative()170     private static native String[] getISOLanguagesNative();
getISOCountriesNative()171     private static native String[] getISOCountriesNative();
172 
initLocaleDataImpl(String locale, LocaleData result)173     static native boolean initLocaleDataImpl(String locale, LocaleData result);
174 }
175