ICU.java revision 162b0775772fa66b7eb634760a8159a60c1ddcea
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.Locale; 20 21/** 22 * Makes ICU data accessible to Java. 23 */ 24public final class ICU { 25 /** 26 * Cache for ISO language names. 27 */ 28 private static String[] isoLanguages; 29 30 /** 31 * Cache for ISO country names. 32 */ 33 private static String[] isoCountries; 34 35 /** 36 * Returns an array of ISO language names (two-letter codes), fetched either 37 * from ICU's database or from our memory cache. 38 * 39 * @return The array. 40 */ 41 public static String[] getISOLanguages() { 42 if (isoLanguages == null) { 43 isoLanguages = getISOLanguagesNative(); 44 } 45 return isoLanguages.clone(); 46 } 47 48 /** 49 * Returns an array of ISO country names (two-letter codes), fetched either 50 * from ICU's database or from our memory cache. 51 * 52 * @return The array. 53 */ 54 public static String[] getISOCountries() { 55 if (isoCountries == null) { 56 isoCountries = getISOCountriesNative(); 57 } 58 return isoCountries.clone(); 59 } 60 61 /** 62 * Returns the appropriate {@code Locale} given a {@code String} of the form returned 63 * by {@code toString}. This is very lenient, and doesn't care what's between the underscores: 64 * this method can parse strings that {@code Locale.toString} won't produce. 65 * Used to remove duplication. 66 */ 67 public static Locale localeFromString(String localeName) { 68 int first = localeName.indexOf('_'); 69 int second = localeName.indexOf('_', first + 1); 70 if (first == -1) { 71 // Language only ("ja"). 72 return new Locale(localeName); 73 } else if (second == -1) { 74 // Language and country ("ja_JP"). 75 return new Locale(localeName.substring(0, first), localeName.substring(first + 1)); 76 } else { 77 // Language and country and variant ("ja_JP_TRADITIONAL"). 78 return new Locale(localeName.substring(0, first), localeName.substring(first + 1, second), localeName.substring(second + 1)); 79 } 80 } 81 82 public static Locale[] localesFromStrings(String[] localeNames) { 83 Locale[] result = new Locale[localeNames.length]; 84 for (int i = 0; i < result.length; ++i) { 85 result[i] = localeFromString(localeNames[i]); 86 } 87 return result; 88 } 89 90 private static Locale[] availableLocalesCache; 91 public static Locale[] getAvailableLocales() { 92 if (availableLocalesCache == null) { 93 availableLocalesCache = localesFromStrings(getAvailableLocalesNative()); 94 } 95 return availableLocalesCache.clone(); 96 } 97 98 public static Locale[] getAvailableBreakIteratorLocales() { 99 return localesFromStrings(getAvailableBreakIteratorLocalesNative()); 100 } 101 102 public static Locale[] getAvailableCalendarLocales() { 103 return localesFromStrings(getAvailableCalendarLocalesNative()); 104 } 105 106 public static Locale[] getAvailableCollatorLocales() { 107 return localesFromStrings(getAvailableCollatorLocalesNative()); 108 } 109 110 public static Locale[] getAvailableDateFormatLocales() { 111 return localesFromStrings(getAvailableDateFormatLocalesNative()); 112 } 113 114 public static Locale[] getAvailableDateFormatSymbolsLocales() { 115 return getAvailableDateFormatLocales(); 116 } 117 118 public static Locale[] getAvailableDecimalFormatSymbolsLocales() { 119 return getAvailableNumberFormatLocales(); 120 } 121 122 public static Locale[] getAvailableNumberFormatLocales() { 123 return localesFromStrings(getAvailableNumberFormatLocalesNative()); 124 } 125 126 // --- Case mapping. 127 128 public static native String toLowerCase(String s, String localeName); 129 public static native String toUpperCase(String s, String localeName); 130 131 // --- Native methods accessing ICU's database. 132 133 private static native String[] getAvailableBreakIteratorLocalesNative(); 134 private static native String[] getAvailableCalendarLocalesNative(); 135 private static native String[] getAvailableCollatorLocalesNative(); 136 private static native String[] getAvailableDateFormatLocalesNative(); 137 private static native String[] getAvailableLocalesNative(); 138 private static native String[] getAvailableNumberFormatLocalesNative(); 139 140 public static native String getCurrencyCodeNative(String locale); 141 public static native int getCurrencyFractionDigitsNative(String currencyCode); 142 public static native String getCurrencySymbolNative(String locale, String currencyCode); 143 144 public static native String getDisplayCountryNative(String countryCode, String locale); 145 public static native String getDisplayLanguageNative(String languageCode, String locale); 146 public static native String getDisplayVariantNative(String variantCode, String locale); 147 148 public static native String getISO3CountryNative(String locale); 149 public static native String getISO3LanguageNative(String locale); 150 151 private static native String[] getISOLanguagesNative(); 152 private static native String[] getISOCountriesNative(); 153 154 static native boolean initLocaleDataImpl(String locale, LocaleData result); 155} 156