LocaleData.java revision ad66a888b2e48b1a185de1b3c73fd01383a1fd04
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    public String fullTimeFormat;
67    public String longTimeFormat;
68    public String mediumTimeFormat;
69    public String shortTimeFormat;
70
71    public String fullDateFormat;
72    public String longDateFormat;
73    public String mediumDateFormat;
74    public String shortDateFormat;
75
76    // Used by DecimalFormatSymbols.
77    public char zeroDigit;
78    public char decimalSeparator;
79    public char groupingSeparator;
80    public char patternSeparator;
81    public char percent;
82    public char perMill;
83    public char monetarySeparator;
84    public char minusSign;
85    public String exponentSeparator;
86    public String infinity;
87    public String NaN;
88    // Also used by Currency.
89    public String currencySymbol;
90    public String internationalCurrencySymbol;
91
92    // Used by DecimalFormat and NumberFormat.
93    public String numberPattern;
94    public String integerPattern;
95    public String currencyPattern;
96    public String percentPattern;
97
98    private LocaleData() {
99    }
100
101    /**
102     * Returns a shared LocaleData for the given locale.
103     */
104    public static LocaleData get(Locale locale) {
105        if (locale == null) {
106            locale = Locale.getDefault();
107        }
108        String localeName = locale.toString();
109        synchronized (localeDataCache) {
110            LocaleData localeData = localeDataCache.get(localeName);
111            if (localeData != null) {
112                return localeData;
113            }
114        }
115        LocaleData newLocaleData = initLocaleData(locale);
116        synchronized (localeDataCache) {
117            LocaleData localeData = localeDataCache.get(localeName);
118            if (localeData != null) {
119                return localeData;
120            }
121            localeDataCache.put(localeName, newLocaleData);
122            return newLocaleData;
123        }
124    }
125
126    @Override public String toString() {
127        return "LocaleData[" +
128                "firstDayOfWeek=" + firstDayOfWeek + "," +
129                "minimalDaysInFirstWeek=" + minimalDaysInFirstWeek + "," +
130                "amPm=" + Arrays.toString(amPm) + "," +
131                "eras=" + Arrays.toString(eras) + "," +
132                "longMonthNames=" + Arrays.toString(longMonthNames) + "," +
133                "shortMonthNames=" + Arrays.toString(shortMonthNames) + "," +
134                "longStandAloneMonthNames=" + Arrays.toString(longStandAloneMonthNames) + "," +
135                "shortStandAloneMonthNames=" + Arrays.toString(shortStandAloneMonthNames) + "," +
136                "longWeekdayNames=" + Arrays.toString(longWeekdayNames) + "," +
137                "shortWeekdayNames=" + Arrays.toString(shortWeekdayNames) + "," +
138                "longStandAloneWeekdayNames=" + Arrays.toString(longStandAloneWeekdayNames) + "," +
139                "shortStandAloneWeekdayNames=" + Arrays.toString(shortStandAloneWeekdayNames) + "," +
140                "fullTimeFormat=" + fullTimeFormat + "," +
141                "longTimeFormat=" + longTimeFormat + "," +
142                "mediumTimeFormat=" + mediumTimeFormat + "," +
143                "shortTimeFormat=" + shortTimeFormat + "," +
144                "fullDateFormat=" + fullDateFormat + "," +
145                "longDateFormat=" + longDateFormat + "," +
146                "mediumDateFormat=" + mediumDateFormat + "," +
147                "shortDateFormat=" + shortDateFormat + "," +
148                "zeroDigit=" + zeroDigit + "," +
149                "decimalSeparator=" + decimalSeparator + "," +
150                "groupingSeparator=" + groupingSeparator + "," +
151                "patternSeparator=" + patternSeparator + "," +
152                "percent=" + percent + "," +
153                "perMill=" + perMill + "," +
154                "monetarySeparator=" + monetarySeparator + "," +
155                "minusSign=" + minusSign + "," +
156                "exponentSeparator=" + exponentSeparator + "," +
157                "infinity=" + infinity + "," +
158                "NaN=" + NaN + "," +
159                "currencySymbol=" + currencySymbol + "," +
160                "internationalCurrencySymbol=" + internationalCurrencySymbol + "," +
161                "numberPattern=" + numberPattern + "," +
162                "integerPattern=" + integerPattern + "," +
163                "currencyPattern=" + currencyPattern + "," +
164                "percentPattern=" + percentPattern + "]";
165    }
166
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
181    public String getTimeFormat(int style) {
182        switch (style) {
183        case DateFormat.SHORT:
184            return shortTimeFormat;
185        case DateFormat.MEDIUM:
186            return mediumTimeFormat;
187        case DateFormat.LONG:
188            return longTimeFormat;
189        case DateFormat.FULL:
190            return fullTimeFormat;
191        }
192        throw new AssertionError();
193    }
194
195    private static LocaleData initLocaleData(Locale locale) {
196        LocaleData localeData = new LocaleData();
197        if (!ICU.initLocaleDataImpl(locale.toString(), localeData)) {
198            throw new AssertionError("couldn't initialize LocaleData for locale " + locale);
199        }
200        if (localeData.fullTimeFormat != null) {
201            // There are some full time format patterns in ICU that use the pattern character 'v'.
202            // Java doesn't accept this, so we replace it with 'z' which has about the same result
203            // as 'v', the timezone name.
204            // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz
205            // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time"
206            localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z');
207        }
208        if (localeData.numberPattern != null) {
209            // The number pattern might contain positive and negative subpatterns. Arabic, for
210            // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be
211            // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)".
212            // (The negative subpattern is optional, though, and not present in most locales.)
213            // By only swallowing '#'es and ','s after the '.', we ensure that we don't
214            // accidentally eat too much.
215            localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", "");
216        }
217        return localeData;
218    }
219}
220