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