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