LocaleData.java revision 66e8297c70daaf001ca199e4e4ad3ba5283396d2
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 17package libcore.icu; 18 19import java.text.DateFormat; 20import java.util.Arrays; 21import java.util.HashMap; 22import java.util.Locale; 23import 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 */ 32public 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 DecimalFormatSymbols. 83 public char zeroDigit; 84 public char decimalSeparator; 85 public char groupingSeparator; 86 public char patternSeparator; 87 public char percent; 88 public char perMill; 89 public char monetarySeparator; 90 public char minusSign; 91 public String exponentSeparator; 92 public String infinity; 93 public String NaN; 94 // Also used by Currency. 95 public String currencySymbol; 96 public String internationalCurrencySymbol; 97 98 // Used by DecimalFormat and NumberFormat. 99 public String numberPattern; 100 public String integerPattern; 101 public String currencyPattern; 102 public String percentPattern; 103 104 private LocaleData() { 105 } 106 107 /** 108 * Returns a shared LocaleData for the given locale. 109 */ 110 public static LocaleData get(Locale locale) { 111 if (locale == null) { 112 locale = Locale.getDefault(); 113 } 114 String localeName = locale.toString(); 115 synchronized (localeDataCache) { 116 LocaleData localeData = localeDataCache.get(localeName); 117 if (localeData != null) { 118 return localeData; 119 } 120 } 121 LocaleData newLocaleData = initLocaleData(locale); 122 synchronized (localeDataCache) { 123 LocaleData localeData = localeDataCache.get(localeName); 124 if (localeData != null) { 125 return localeData; 126 } 127 localeDataCache.put(localeName, newLocaleData); 128 return newLocaleData; 129 } 130 } 131 132 @Override public String toString() { 133 return Objects.toString(this); 134 } 135 136 public String getDateFormat(int style) { 137 switch (style) { 138 case DateFormat.SHORT: 139 return shortDateFormat; 140 case DateFormat.MEDIUM: 141 return mediumDateFormat; 142 case DateFormat.LONG: 143 return longDateFormat; 144 case DateFormat.FULL: 145 return fullDateFormat; 146 } 147 throw new AssertionError(); 148 } 149 150 public String getTimeFormat(int style) { 151 switch (style) { 152 case DateFormat.SHORT: 153 return shortTimeFormat; 154 case DateFormat.MEDIUM: 155 return mediumTimeFormat; 156 case DateFormat.LONG: 157 return longTimeFormat; 158 case DateFormat.FULL: 159 return fullTimeFormat; 160 } 161 throw new AssertionError(); 162 } 163 164 private static LocaleData initLocaleData(Locale locale) { 165 LocaleData localeData = new LocaleData(); 166 if (!ICU.initLocaleDataImpl(locale.toString(), localeData)) { 167 throw new AssertionError("couldn't initialize LocaleData for locale " + locale); 168 } 169 if (localeData.fullTimeFormat != null) { 170 // There are some full time format patterns in ICU that use the pattern character 'v'. 171 // Java doesn't accept this, so we replace it with 'z' which has about the same result 172 // as 'v', the timezone name. 173 // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz 174 // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time" 175 localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z'); 176 } 177 if (localeData.numberPattern != null) { 178 // The number pattern might contain positive and negative subpatterns. Arabic, for 179 // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be 180 // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)". 181 // (The negative subpattern is optional, though, and not present in most locales.) 182 // By only swallowing '#'es and ','s after the '.', we ensure that we don't 183 // accidentally eat too much. 184 localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", ""); 185 } 186 return localeData; 187 } 188} 189