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);
Locale.getDefault()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     public String timeFormat_hm;
92     public String timeFormat_Hm;
93     public String timeFormat_hms;
94     public String timeFormat_Hms;
95 
96     // Used by android.text.format.DateFormat.getTimeFormat.
97     public String timeFormat12; // "hh:mm a"
98     public String timeFormat24; // "HH:mm"
99 
100     // Used by DecimalFormatSymbols.
101     public char zeroDigit;
102     public char decimalSeparator;
103     public char groupingSeparator;
104     public char patternSeparator;
105     public String percent;
106     public char perMill;
107     public char monetarySeparator;
108     public String minusSign;
109     public String exponentSeparator;
110     public String infinity;
111     public String NaN;
112     // Also used by Currency.
113     public String currencySymbol;
114     public String internationalCurrencySymbol;
115 
116     // Used by DecimalFormat and NumberFormat.
117     public String numberPattern;
118     public String integerPattern;
119     public String currencyPattern;
120     public String percentPattern;
121 
LocaleData()122     private LocaleData() {
123     }
124 
mapInvalidAndNullLocales(Locale locale)125     public static Locale mapInvalidAndNullLocales(Locale locale) {
126         if (locale == null) {
127             return Locale.getDefault();
128         }
129 
130         if ("und".equals(locale.toLanguageTag())) {
131             return Locale.ROOT;
132         }
133 
134         return locale;
135     }
136 
137     /**
138      * Returns a shared LocaleData for the given locale.
139      */
get(Locale locale)140     public static LocaleData get(Locale locale) {
141         if (locale == null) {
142             throw new NullPointerException("locale == null");
143         }
144 
145         final String languageTag = locale.toLanguageTag();
146         synchronized (localeDataCache) {
147             LocaleData localeData = localeDataCache.get(languageTag);
148             if (localeData != null) {
149                 return localeData;
150             }
151         }
152         LocaleData newLocaleData = initLocaleData(locale);
153         synchronized (localeDataCache) {
154             LocaleData localeData = localeDataCache.get(languageTag);
155             if (localeData != null) {
156                 return localeData;
157             }
158             localeDataCache.put(languageTag, newLocaleData);
159             return newLocaleData;
160         }
161     }
162 
toString()163     @Override public String toString() {
164         return Objects.toString(this);
165     }
166 
getDateFormat(int style)167     public String getDateFormat(int style) {
168         switch (style) {
169         case DateFormat.SHORT:
170             return shortDateFormat;
171         case DateFormat.MEDIUM:
172             return mediumDateFormat;
173         case DateFormat.LONG:
174             return longDateFormat;
175         case DateFormat.FULL:
176             return fullDateFormat;
177         }
178         throw new AssertionError();
179     }
180 
getTimeFormat(int style)181     public String getTimeFormat(int style) {
182         switch (style) {
183         case DateFormat.SHORT:
184             if (DateFormat.is24Hour == null) {
185                 return shortTimeFormat;
186             } else {
187                 return DateFormat.is24Hour ? timeFormat_Hm : timeFormat_hm;
188             }
189         case DateFormat.MEDIUM:
190             if (DateFormat.is24Hour == null) {
191                 return mediumTimeFormat;
192             } else {
193                 return DateFormat.is24Hour ? timeFormat_Hms : timeFormat_hms;
194             }
195         case DateFormat.LONG:
196             // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference.
197             return longTimeFormat;
198         case DateFormat.FULL:
199             // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference.
200             return fullTimeFormat;
201         }
202         throw new AssertionError();
203     }
204 
initLocaleData(Locale locale)205     private static LocaleData initLocaleData(Locale locale) {
206         LocaleData localeData = new LocaleData();
207         if (!ICU.initLocaleDataNative(locale.toLanguageTag(), localeData)) {
208             throw new AssertionError("couldn't initialize LocaleData for locale " + locale);
209         }
210 
211         // Get the SHORT and MEDIUM 12- and 24-hour time format strings.
212         localeData.timeFormat_hm = ICU.getBestDateTimePattern("hm", locale);
213         localeData.timeFormat_Hm = ICU.getBestDateTimePattern("Hm", locale);
214         localeData.timeFormat_hms = ICU.getBestDateTimePattern("hms", locale);
215         localeData.timeFormat_Hms = ICU.getBestDateTimePattern("Hms", locale);
216         // We could move callers over to the other fields, but these seem simpler and discourage
217         // people from shooting themselves in the foot by learning about patterns and skeletons.
218         // TODO: the right fix here is probably to move callers over to java.text.DateFormat,
219         // so nothing outside libcore references these any more.
220         localeData.timeFormat12 = localeData.timeFormat_hm;
221         localeData.timeFormat24 = localeData.timeFormat_Hm;
222 
223         // Fix up a couple of patterns.
224         if (localeData.fullTimeFormat != null) {
225             // There are some full time format patterns in ICU that use the pattern character 'v'.
226             // Java doesn't accept this, so we replace it with 'z' which has about the same result
227             // as 'v', the timezone name.
228             // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz
229             // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time"
230             localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z');
231         }
232         if (localeData.numberPattern != null) {
233             // The number pattern might contain positive and negative subpatterns. Arabic, for
234             // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be
235             // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)".
236             // (The negative subpattern is optional, though, and not present in most locales.)
237             // By only swallowing '#'es and ','s after the '.', we ensure that we don't
238             // accidentally eat too much.
239             localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", "");
240         }
241         localeData.shortDateFormat4 = localeData.shortDateFormat.replaceAll("\\byy\\b", "y");
242         return localeData;
243     }
244 }
245