Home | History | Annotate | Download | only in icu
      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);
     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 char 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     /**
    116      * Returns a shared LocaleData for the given locale.
    117      */
    118     public static LocaleData get(Locale locale) {
    119         if (locale == null) {
    120             locale = Locale.getDefault();
    121         }
    122         String localeName = locale.toString();
    123         synchronized (localeDataCache) {
    124             LocaleData localeData = localeDataCache.get(localeName);
    125             if (localeData != null) {
    126                 return localeData;
    127             }
    128         }
    129         LocaleData newLocaleData = initLocaleData(locale);
    130         synchronized (localeDataCache) {
    131             LocaleData localeData = localeDataCache.get(localeName);
    132             if (localeData != null) {
    133                 return localeData;
    134             }
    135             localeDataCache.put(localeName, newLocaleData);
    136             return newLocaleData;
    137         }
    138     }
    139 
    140     @Override public String toString() {
    141         return Objects.toString(this);
    142     }
    143 
    144     public String getDateFormat(int style) {
    145         switch (style) {
    146         case DateFormat.SHORT:
    147             return shortDateFormat;
    148         case DateFormat.MEDIUM:
    149             return mediumDateFormat;
    150         case DateFormat.LONG:
    151             return longDateFormat;
    152         case DateFormat.FULL:
    153             return fullDateFormat;
    154         }
    155         throw new AssertionError();
    156     }
    157 
    158     public String getTimeFormat(int style) {
    159         switch (style) {
    160         case DateFormat.SHORT:
    161             return shortTimeFormat;
    162         case DateFormat.MEDIUM:
    163             return mediumTimeFormat;
    164         case DateFormat.LONG:
    165             return longTimeFormat;
    166         case DateFormat.FULL:
    167             return fullTimeFormat;
    168         }
    169         throw new AssertionError();
    170     }
    171 
    172     private static LocaleData initLocaleData(Locale locale) {
    173         LocaleData localeData = new LocaleData();
    174         if (!ICU.initLocaleDataNative(locale.toString(), localeData)) {
    175             throw new AssertionError("couldn't initialize LocaleData for locale " + locale);
    176         }
    177 
    178         // Get the "h:mm a" and "HH:mm" 12- and 24-hour time format strings.
    179         localeData.timeFormat12 = ICU.getBestDateTimePattern("hm", locale.toString());
    180         localeData.timeFormat24 = ICU.getBestDateTimePattern("Hm", locale.toString());
    181 
    182         // Fix up a couple of patterns.
    183         if (localeData.fullTimeFormat != null) {
    184             // There are some full time format patterns in ICU that use the pattern character 'v'.
    185             // Java doesn't accept this, so we replace it with 'z' which has about the same result
    186             // as 'v', the timezone name.
    187             // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz
    188             // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time"
    189             localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z');
    190         }
    191         if (localeData.numberPattern != null) {
    192             // The number pattern might contain positive and negative subpatterns. Arabic, for
    193             // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be
    194             // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)".
    195             // (The negative subpattern is optional, though, and not present in most locales.)
    196             // By only swallowing '#'es and ','s after the '.', we ensure that we don't
    197             // accidentally eat too much.
    198             localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", "");
    199         }
    200         localeData.shortDateFormat4 = localeData.shortDateFormat.replaceAll("\\byy\\b", "y");
    201         return localeData;
    202     }
    203 }
    204