ICU.java revision 15d82808bab24f399a034d086f587d5fab32522e
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 public static native String getBestDateTimePattern(String skeleton, String localeName); 131 132 /** 133 * Returns the version of the CLDR data in use, such as "22.1.1". 134 */ 135 public static native String getCldrVersion(); 136 137 /** 138 * Returns the icu4c version in use, such as "50.1.1". 139 */ 140 public static native String getIcuVersion(); 141 142 /** 143 * Returns the Unicode version our ICU supports, such as "6.2". 144 */ 145 public static native String getUnicodeVersion(); 146 147 // --- Case mapping. 148 149 public static native String toLowerCase(String s, String localeName); 150 public static native String toUpperCase(String s, String localeName); 151 152 // --- Errors. 153 154 // Just the subset of error codes needed by CharsetDecoderICU/CharsetEncoderICU. 155 public static final int U_ZERO_ERROR = 0; 156 public static final int U_INVALID_CHAR_FOUND = 10; 157 public static final int U_TRUNCATED_CHAR_FOUND = 11; 158 public static final int U_ILLEGAL_CHAR_FOUND = 12; 159 public static final int U_BUFFER_OVERFLOW_ERROR = 15; 160 161 public static boolean U_FAILURE(int error) { 162 return error > U_ZERO_ERROR; 163 } 164 165 // --- Native methods accessing ICU's database. 166 167 private static native String[] getAvailableBreakIteratorLocalesNative(); 168 private static native String[] getAvailableCalendarLocalesNative(); 169 private static native String[] getAvailableCollatorLocalesNative(); 170 private static native String[] getAvailableDateFormatLocalesNative(); 171 private static native String[] getAvailableLocalesNative(); 172 private static native String[] getAvailableNumberFormatLocalesNative(); 173 174 public static native String[] getAvailableCurrencyCodes(); 175 public static native String getCurrencyCode(String countryCode); 176 public static native String getCurrencyDisplayName(String locale, String currencyCode); 177 public static native int getCurrencyFractionDigits(String currencyCode); 178 public static native String getCurrencySymbol(String locale, String currencyCode); 179 180 public static native String getDisplayCountryNative(String countryCode, String locale); 181 public static native String getDisplayLanguageNative(String languageCode, String locale); 182 public static native String getDisplayVariantNative(String variantCode, String locale); 183 184 public static native String getISO3CountryNative(String locale); 185 public static native String getISO3LanguageNative(String locale); 186 187 public static native String addLikelySubtags(String locale); 188 public static native String getScript(String locale); 189 190 private static native String[] getISOLanguagesNative(); 191 private static native String[] getISOCountriesNative(); 192 193 static native boolean initLocaleDataImpl(String locale, LocaleData result); 194} 195