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