LocaleData.java revision 4f3bca749bdc8be8abf7555b315f308a2c8c14a7
133aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes/* 233aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * Copyright (C) 2009 The Android Open Source Project 333aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * 433aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 533aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * you may not use this file except in compliance with the License. 633aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * You may obtain a copy of the License at 733aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * 833aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 933aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * 1033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1133aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1233aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1333aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * See the License for the specific language governing permissions and 1433aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * limitations under the License. 1533aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes */ 1633aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes 17162b0775772fa66b7eb634760a8159a60c1ddceaElliott Hughespackage libcore.icu; 1833aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes 196ca85c49efc0f02d69933f60b207b964a999061fElliott Hughesimport java.text.DateFormat; 20deacd761e85ee4d75a6adbdd63225fc4a6d3088dElliott Hughesimport java.util.Arrays; 21757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughesimport java.util.HashMap; 22757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughesimport java.util.Locale; 2366e8297c70daaf001ca199e4e4ad3ba5283396d2Elliott Hughesimport libcore.util.Objects; 246ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes 2533aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes/** 2633aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * Passes locale-specific from ICU native code to Java. 2733aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * <p> 2833aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * Note that you share these; you must not alter any of the fields, nor their array elements 2933aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * in the case of arrays. If you ever expose any of these things to user code, you must give 3033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes * them a clone rather than the original. 3133aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes */ 329672b4887f2972c1b7c5f3d1a6cf882deccf857fElliott Hughespublic final class LocaleData { 33757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // A cache for the locale-specific data. 34757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes private static final HashMap<String, LocaleData> localeDataCache = new HashMap<String, LocaleData>(); 35e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes static { 36e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes // Ensure that we pull in the locale data for the root locale, en_US, and the 37e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes // user's default locale. (All devices must support the root locale and en_US, 38e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes // and they're used for various system things like HTTP headers.) Pre-populating 39e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes // the cache is especially useful on Android because we'll share this via the Zygote. 40e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes get(Locale.ROOT); 41e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes get(Locale.US); 42e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes get(Locale.getDefault()); 43e2377cdd707b830d07a5708216834f7ac76ee3e1Elliott Hughes } 44757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 45143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes // Used by Calendar. 4633aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public Integer firstDayOfWeek; 4733aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public Integer minimalDaysInFirstWeek; 48757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 49143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes // Used by DateFormatSymbols. 50ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] amPm; // "AM", "PM". 51ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] eras; // "BC", "AD". 52757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 53ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] longMonthNames; // "January", ... 54ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] shortMonthNames; // "Jan", ... 55ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] tinyMonthNames; // "J", ... 56ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] longStandAloneMonthNames; // "January", ... 57ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] shortStandAloneMonthNames; // "Jan", ... 58ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] tinyStandAloneMonthNames; // "J", ... 59757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 60ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] longWeekdayNames; // "Sunday", ... 61ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] shortWeekdayNames; // "Sun", ... 62ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] tinyWeekdayNames; // "S", ... 63ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] longStandAloneWeekdayNames; // "Sunday", ... 64ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] shortStandAloneWeekdayNames; // "Sun", ... 65ad66a888b2e48b1a185de1b3c73fd01383a1fd04Elliott Hughes public String[] tinyStandAloneWeekdayNames; // "S", ... 66757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 67c4e0797a4dd028d23e788da15c3055f83f6f37d5Elliott Hughes // Used by frameworks/base DateSorter and DateUtils. 68c4e0797a4dd028d23e788da15c3055f83f6f37d5Elliott Hughes public String yesterday; // "Yesterday". 69c4e0797a4dd028d23e788da15c3055f83f6f37d5Elliott Hughes public String today; // "Today". 70c4e0797a4dd028d23e788da15c3055f83f6f37d5Elliott Hughes public String tomorrow; // "Tomorrow". 71c4e0797a4dd028d23e788da15c3055f83f6f37d5Elliott Hughes 7233aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String fullTimeFormat; 7333aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String longTimeFormat; 7433aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String mediumTimeFormat; 7533aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String shortTimeFormat; 76757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 7733aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String fullDateFormat; 7833aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String longDateFormat; 7933aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String mediumDateFormat; 8033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String shortDateFormat; 81757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 822f5b39ddcdfc4ab7edd0a5a02801eb3dc96e0266Elliott Hughes // shortDateFormat, but guaranteed to have 4-digit years. 832f5b39ddcdfc4ab7edd0a5a02801eb3dc96e0266Elliott Hughes // Used by android.text.format.DateFormat.getDateFormatStringForSetting. 842f5b39ddcdfc4ab7edd0a5a02801eb3dc96e0266Elliott Hughes public String shortDateFormat4; 852f5b39ddcdfc4ab7edd0a5a02801eb3dc96e0266Elliott Hughes 864f32b7933626e100fca505484f8b4636f3342e5eElliott Hughes // Used by android.text.format.DateFormat.getTimeFormat. 874f32b7933626e100fca505484f8b4636f3342e5eElliott Hughes public String timeFormat12; // "hh:mm a" 884f32b7933626e100fca505484f8b4636f3342e5eElliott Hughes public String timeFormat24; // "HH:mm" 894f32b7933626e100fca505484f8b4636f3342e5eElliott Hughes 90143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes // Used by DecimalFormatSymbols. 91b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char zeroDigit; 92b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char decimalSeparator; 93b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char groupingSeparator; 94b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char patternSeparator; 95b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char percent; 96b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char perMill; 97b7e820b92c7345cdc0cd4fea50954289ae66eb67Elliott Hughes public char monetarySeparator; 984f3bca749bdc8be8abf7555b315f308a2c8c14a7Narayan Kamath public String minusSign; 9990aa512eb7b126deb8d752b7474c30d3f73507b2Elliott Hughes public String exponentSeparator; 10033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String infinity; 10133aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String NaN; 102143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes // Also used by Currency. 10333aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String currencySymbol; 10433aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String internationalCurrencySymbol; 105757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 106143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes // Used by DecimalFormat and NumberFormat. 10733aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String numberPattern; 10833aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String integerPattern; 10933aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String currencyPattern; 11033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes public String percentPattern; 111757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 112757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes private LocaleData() { 113757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 114757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 115757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes /** 116757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes * Returns a shared LocaleData for the given locale. 117757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes */ 118757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes public static LocaleData get(Locale locale) { 119757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes if (locale == null) { 120757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes locale = Locale.getDefault(); 121757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 122757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes String localeName = locale.toString(); 123757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes synchronized (localeDataCache) { 124757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes LocaleData localeData = localeDataCache.get(localeName); 125757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes if (localeData != null) { 126757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return localeData; 127757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 128757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 1295b7b7fe6a817fdf058eefd9a716cc58a3283eb05claireho LocaleData newLocaleData = initLocaleData(locale); 130757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes synchronized (localeDataCache) { 131757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes LocaleData localeData = localeDataCache.get(localeName); 132757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes if (localeData != null) { 133757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return localeData; 134757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 135757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes localeDataCache.put(localeName, newLocaleData); 136757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return newLocaleData; 137757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 138757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 139757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 14033aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes @Override public String toString() { 14166e8297c70daaf001ca199e4e4ad3ba5283396d2Elliott Hughes return Objects.toString(this); 14233aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes } 143757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 1446ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes public String getDateFormat(int style) { 1456ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes switch (style) { 1466ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.SHORT: 1476ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return shortDateFormat; 1486ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.MEDIUM: 1496ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return mediumDateFormat; 1506ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.LONG: 1516ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return longDateFormat; 1526ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.FULL: 1536ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return fullDateFormat; 1546ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes } 1556ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes throw new AssertionError(); 1566ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes } 157757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 1586ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes public String getTimeFormat(int style) { 1596ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes switch (style) { 1606ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.SHORT: 1616ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return shortTimeFormat; 1626ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.MEDIUM: 1636ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return mediumTimeFormat; 1646ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.LONG: 1656ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return longTimeFormat; 1666ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes case DateFormat.FULL: 1676ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes return fullTimeFormat; 1686ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes } 1696ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes throw new AssertionError(); 1706ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes } 171757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 172757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes private static LocaleData initLocaleData(Locale locale) { 173757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes LocaleData localeData = new LocaleData(); 1740fd776f4dbf7c1940c080003f0c7a02d35374ab9Elliott Hughes if (!ICU.initLocaleDataNative(locale.toString(), localeData)) { 175757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes throw new AssertionError("couldn't initialize LocaleData for locale " + locale); 176757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 177783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes 178783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes // Get the "h:mm a" and "HH:mm" 12- and 24-hour time format strings. 179783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes localeData.timeFormat12 = ICU.getBestDateTimePattern("hm", locale.toString()); 180783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes localeData.timeFormat24 = ICU.getBestDateTimePattern("Hm", locale.toString()); 181783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes 182783112d47e2236b6a7e2315c2e50166d755422a7Elliott Hughes // Fix up a couple of patterns. 183757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes if (localeData.fullTimeFormat != null) { 184757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // There are some full time format patterns in ICU that use the pattern character 'v'. 185757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // Java doesn't accept this, so we replace it with 'z' which has about the same result 186757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // as 'v', the timezone name. 187757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // 'v' -> "PT", 'z' -> "PST", v is the generic timezone and z the standard tz 188757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // "vvvv" -> "Pacific Time", "zzzz" -> "Pacific Standard Time" 189757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes localeData.fullTimeFormat = localeData.fullTimeFormat.replace('v', 'z'); 190757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 191757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes if (localeData.numberPattern != null) { 192757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // The number pattern might contain positive and negative subpatterns. Arabic, for 193757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // example, might look like "#,##0.###;#,##0.###-" because the minus sign should be 194757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // written last. Macedonian supposedly looks something like "#,##0.###;(#,##0.###)". 195757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // (The negative subpattern is optional, though, and not present in most locales.) 196757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // By only swallowing '#'es and ','s after the '.', we ensure that we don't 197757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes // accidentally eat too much. 198757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes localeData.integerPattern = localeData.numberPattern.replaceAll("\\.[#,]*", ""); 199757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 2002f5b39ddcdfc4ab7edd0a5a02801eb3dc96e0266Elliott Hughes localeData.shortDateFormat4 = localeData.shortDateFormat.replaceAll("\\byy\\b", "y"); 201757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return localeData; 202757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes } 20333aa6eb602478e7f51ac16f30c88db3566022886Elliott Hughes} 204