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