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     // Used by TimePicker. Not currently used by UTS#35.
     83     public String narrowAm; // "a".
     84     public String narrowPm; // "p".
     85 
     86     // shortDateFormat, but guaranteed to have 4-digit years.
     87     // Used by android.text.format.DateFormat.getDateFormatStringForSetting.
     88     public String shortDateFormat4;
     89 
     90     // Used by android.text.format.DateFormat.getTimeFormat.
     91     public String timeFormat12; // "hh:mm a"
     92     public String timeFormat24; // "HH:mm"
     93 
     94     // Used by DecimalFormatSymbols.
     95     public char zeroDigit;
     96     public char decimalSeparator;
     97     public char groupingSeparator;
     98     public char patternSeparator;
     99     public char percent;
    100     public char perMill;
    101     public char monetarySeparator;
    102     public String minusSign;
    103     public String exponentSeparator;
    104     public String infinity;
    105     public String NaN;
    106     // Also used by Currency.
    107     public String currencySymbol;
    108     public String internationalCurrencySymbol;
    109 
    110     // Used by DecimalFormat and NumberFormat.
    111     public String numberPattern;
    112     public String integerPattern;
    113     public String currencyPattern;
    114     public String percentPattern;
    115 
    116     private LocaleData() {
    117     }
    118 
    119     public static Locale mapInvalidAndNullLocales(Locale locale) {
    120         if (locale == null) {
    121             return Locale.getDefault();
    122         }
    123 
    124         if ("und".equals(locale.toLanguageTag())) {
    125             return Locale.ROOT;
    126         }
    127 
    128         return locale;
    129     }
    130 
    131     /**
    132      * Returns a shared LocaleData for the given locale.
    133      */
    134     public static LocaleData get(Locale locale) {
    135         if (locale == null) {
    136             throw new NullPointerException("locale == null");
    137         }
    138 
    139         final String languageTag = locale.toLanguageTag();
    140         synchronized (localeDataCache) {
    141             LocaleData localeData = localeDataCache.get(languageTag);
    142             if (localeData != null) {
    143                 return localeData;
    144             }
    145         }
    146         LocaleData newLocaleData = initLocaleData(locale);
    147         synchronized (localeDataCache) {
    148             LocaleData localeData = localeDataCache.get(languageTag);
    149             if (localeData != null) {
    150                 return localeData;
    151             }
    152             localeDataCache.put(languageTag, newLocaleData);
    153             return newLocaleData;
    154         }
    155     }
    156 
    157     @Override public String toString() {
    158         return Objects.toString(this);
    159     }
    160 
    161     public String getDateFormat(int style) {
    162         switch (style) {
    163         case DateFormat.SHORT:
    164             return shortDateFormat;
    165         case DateFormat.MEDIUM:
    166             return mediumDateFormat;
    167         case DateFormat.LONG:
    168             return longDateFormat;
    169         case DateFormat.FULL:
    170             return fullDateFormat;
    171         }
    172         throw new AssertionError();
    173     }
    174 
    175     public String getTimeFormat(int style) {
    176         switch (style) {
    177         case DateFormat.SHORT:
    178             return shortTimeFormat;
    179         case DateFormat.MEDIUM:
    180             return mediumTimeFormat;
    181         case DateFormat.LONG:
    182             return longTimeFormat;
    183         case DateFormat.FULL:
    184             return fullTimeFormat;
    185         }
    186         throw new AssertionError();
    187     }
    188 
    189     private static LocaleData initLocaleData(Locale locale) {
    190         LocaleData localeData = new LocaleData();
    191         if (!ICU.initLocaleDataNative(locale.toLanguageTag(), localeData)) {
    192             throw new AssertionError("couldn't initialize LocaleData for locale " + locale);
    193         }
    194 
    195         // Get the "h:mm a" and "HH:mm" 12- and 24-hour time format strings.
    196         localeData.timeFormat12 = ICU.getBestDateTimePattern("hm", locale);
    197         localeData.timeFormat24 = ICU.getBestDateTimePattern("Hm", locale);
    198 
    199         // Fix up a couple of patterns.
    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         localeData.shortDateFormat4 = localeData.shortDateFormat.replaceAll("\\byy\\b", "y");
    218         return localeData;
    219     }
    220 }
    221