1 /* 2 * Copyright (C) 2009 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.text.DateFormat; 20 import java.util.Arrays; 21 import java.util.HashMap; 22 import java.util.Locale; 23 import libcore.util.Objects; 24 25 /** 26 * Passes locale-specific from ICU native code to Java. 27 * <p> 28 * Note that you share these; you must not alter any of the fields, nor their array elements 29 * in the case of arrays. If you ever expose any of these things to user code, you must give 30 * them a clone rather than the original. 31 */ 32 public final class LocaleData { 33 // A cache for the locale-specific data. 34 private static final HashMap<String, LocaleData> localeDataCache = new HashMap<String, LocaleData>(); 35 static { 36 // Ensure that we pull in the locale data for the root locale, en_US, and the 37 // user's default locale. (All devices must support the root locale and en_US, 38 // and they're used for various system things like HTTP headers.) Pre-populating 39 // the cache is especially useful on Android because we'll share this via the Zygote. 40 get(Locale.ROOT); 41 get(Locale.US); 42 get(Locale.getDefault()); 43 } 44 45 // Used by Calendar. 46 public Integer firstDayOfWeek; 47 public Integer minimalDaysInFirstWeek; 48 49 // Used by DateFormatSymbols. 50 public String[] amPm; // "AM", "PM". 51 public String[] eras; // "BC", "AD". 52 53 public String[] longMonthNames; // "January", ... 54 public String[] shortMonthNames; // "Jan", ... 55 public String[] tinyMonthNames; // "J", ... 56 public String[] longStandAloneMonthNames; // "January", ... 57 public String[] shortStandAloneMonthNames; // "Jan", ... 58 public String[] tinyStandAloneMonthNames; // "J", ... 59 60 public String[] longWeekdayNames; // "Sunday", ... 61 public String[] shortWeekdayNames; // "Sun", ... 62 public String[] tinyWeekdayNames; // "S", ... 63 public String[] longStandAloneWeekdayNames; // "Sunday", ... 64 public String[] shortStandAloneWeekdayNames; // "Sun", ... 65 public String[] tinyStandAloneWeekdayNames; // "S", ... 66 67 // Used by frameworks/base DateSorter and DateUtils. 68 public String yesterday; // "Yesterday". 69 public String today; // "Today". 70 public String tomorrow; // "Tomorrow". 71 72 public String fullTimeFormat; 73 public String longTimeFormat; 74 public String mediumTimeFormat; 75 public String shortTimeFormat; 76 77 public String fullDateFormat; 78 public String longDateFormat; 79 public String mediumDateFormat; 80 public String shortDateFormat; 81 82 // Used by TimePicker. Not currently used by UTS#35. 83 public String narrowAm; // "a". 84 public String narrowPm; // "p". 85 86 // shortDateFormat, but guaranteed to have 4-digit years. 87 // Used by android.text.format.DateFormat.getDateFormatStringForSetting. 88 public String shortDateFormat4; 89 90 // Used by DateFormat to implement 12- and 24-hour SHORT and MEDIUM. 91 // The first two are also used directly by frameworks code. 92 public String timeFormat_hm; 93 public String timeFormat_Hm; 94 public String timeFormat_hms; 95 public String timeFormat_Hms; 96 97 // Used by DecimalFormatSymbols. 98 public char zeroDigit; 99 public char decimalSeparator; 100 public char groupingSeparator; 101 public char patternSeparator; 102 public String percent; 103 public char perMill; 104 public char monetarySeparator; 105 public String minusSign; 106 public String exponentSeparator; 107 public String infinity; 108 public String NaN; 109 // Also used by Currency. 110 public String currencySymbol; 111 public String internationalCurrencySymbol; 112 113 // Used by DecimalFormat and NumberFormat. 114 public String numberPattern; 115 public String integerPattern; 116 public String currencyPattern; 117 public String percentPattern; 118 119 private LocaleData() { 120 } 121 122 public static Locale mapInvalidAndNullLocales(Locale locale) { 123 if (locale == null) { 124 return Locale.getDefault(); 125 } 126 127 if ("und".equals(locale.toLanguageTag())) { 128 return Locale.ROOT; 129 } 130 131 return locale; 132 } 133 134 /** 135 * Returns a shared LocaleData for the given locale. 136 */ 137 public static LocaleData get(Locale locale) { 138 if (locale == null) { 139 throw new NullPointerException("locale == null"); 140 } 141 142 final String languageTag = locale.toLanguageTag(); 143 synchronized (localeDataCache) { 144 LocaleData localeData = localeDataCache.get(languageTag); 145 if (localeData != null) { 146 return localeData; 147 } 148 } 149 LocaleData newLocaleData = initLocaleData(locale); 150 synchronized (localeDataCache) { 151 LocaleData localeData = localeDataCache.get(languageTag); 152 if (localeData != null) { 153 return localeData; 154 } 155 localeDataCache.put(languageTag, newLocaleData); 156 return newLocaleData; 157 } 158 } 159 160 @Override public String toString() { 161 return Objects.toString(this); 162 } 163 164 public String getDateFormat(int style) { 165 switch (style) { 166 case DateFormat.SHORT: 167 return shortDateFormat; 168 case DateFormat.MEDIUM: 169 return mediumDateFormat; 170 case DateFormat.LONG: 171 return longDateFormat; 172 case DateFormat.FULL: 173 return fullDateFormat; 174 } 175 throw new AssertionError(); 176 } 177 178 public String getTimeFormat(int style) { 179 switch (style) { 180 case DateFormat.SHORT: 181 if (DateFormat.is24Hour == null) { 182 return shortTimeFormat; 183 } else { 184 return DateFormat.is24Hour ? timeFormat_Hm : timeFormat_hm; 185 } 186 case DateFormat.MEDIUM: 187 if (DateFormat.is24Hour == null) { 188 return mediumTimeFormat; 189 } else { 190 return DateFormat.is24Hour ? timeFormat_Hms : timeFormat_hms; 191 } 192 case DateFormat.LONG: 193 // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference. 194 return longTimeFormat; 195 case DateFormat.FULL: 196 // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference. 197 return fullTimeFormat; 198 } 199 throw new AssertionError(); 200 } 201 202 private static LocaleData initLocaleData(Locale locale) { 203 LocaleData localeData = new LocaleData(); 204 if (!ICU.initLocaleDataNative(locale.toLanguageTag(), localeData)) { 205 throw new AssertionError("couldn't initialize LocaleData for locale " + locale); 206 } 207 208 // Get the SHORT and MEDIUM 12- and 24-hour time format strings. 209 localeData.timeFormat_hm = ICU.getBestDateTimePattern("hm", locale); 210 localeData.timeFormat_Hm = ICU.getBestDateTimePattern("Hm", locale); 211 localeData.timeFormat_hms = ICU.getBestDateTimePattern("hms", locale); 212 localeData.timeFormat_Hms = ICU.getBestDateTimePattern("Hms", locale); 213 214 // Fix up a couple of patterns. 215 if (localeData.fullTimeFormat != null) { 216 // There are some full time format patterns in ICU that use the pattern character 'v'. 217 // Java doesn't accept this, so we replace it with 'z' which has about the same result 218 // as 'v', the timezone name. 219 // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz 220 // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time" 221 localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z'); 222 } 223 if (localeData.numberPattern != null) { 224 // The number pattern might contain positive and negative subpatterns. Arabic, for 225 // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be 226 // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)". 227 // (The negative subpattern is optional, though, and not present in most locales.) 228 // By only swallowing '#'es and ','s after the '.', we ensure that we don't 229 // accidentally eat too much. 230 localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", ""); 231 } 232 localeData.shortDateFormat4 = localeData.shortDateFormat.replaceAll("\\byy\\b", "y"); 233 return localeData; 234 } 235 } 236