/*
*******************************************************************************
- * Copyright (C) 2009, International Business Machines Corporation and *
+ * Copyright (C) 2009,2016 International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
return calType;
}
- String baseLoc = loc.getBaseName();
-
- // Check the cache
- calType = CALTYPE_CACHE.get(baseLoc);
+ // Canonicalize, so grandfathered variant will be transformed to keywords
+ ULocale canonical = ULocale.createCanonical(loc.toString());
+ calType = canonical.getKeywordValue(CALKEY);
if (calType != null) {
return calType;
}
- // Canonicalize, so grandfathered variant will be transformed to keywords
- ULocale canonical = ULocale.createCanonical(loc.toString());
- calType = canonical.getKeywordValue("calendar");
+ // When calendar keyword is not available, use the locale's
+ // region to get the default calendar type
+ String region = ULocale.getRegionForSupplementalData(canonical, true);
- if (calType == null) {
- // When calendar keyword is not available, use the locale's
- // region to get the default calendar type
- String region = canonical.getCountry();
- if (region.length() == 0) {
- ULocale fullLoc = ULocale.addLikelySubtags(canonical);
- region = fullLoc.getCountry();
- }
+ // Check the cache (now we cache by region, not base locale)
+ calType = CALTYPE_CACHE.get(region);
+ if (calType != null) {
+ return calType;
+ }
- // Read supplementalData to get the default calendar type for
- // the locale's region
+ // Read supplementalData to get the default calendar type for
+ // the locale's region
+ try {
+ UResourceBundle rb = UResourceBundle.getBundleInstance(
+ ICUResourceBundle.ICU_BASE_NAME,
+ "supplementalData",
+ ICUResourceBundle.ICU_DATA_CLASS_LOADER);
+ UResourceBundle calPref = rb.get("calendarPreferenceData");
+ UResourceBundle order = null;
try {
- UResourceBundle rb = UResourceBundle.getBundleInstance(
- ICUResourceBundle.ICU_BASE_NAME,
- "supplementalData",
- ICUResourceBundle.ICU_DATA_CLASS_LOADER);
- UResourceBundle calPref = rb.get("calendarPreferenceData");
- UResourceBundle order = null;
- try {
- order = calPref.get(region);
- } catch (MissingResourceException mre) {
- // use "001" as fallback
- order = calPref.get("001");
- }
- // the first calendar type is the default for the region
- calType = order.getString(0);
+ order = calPref.get(region);
} catch (MissingResourceException mre) {
- // fall through
+ // use "001" as fallback
+ order = calPref.get("001");
}
+ // the first calendar type is the default for the region
+ calType = order.getString(0);
+ } catch (MissingResourceException mre) {
+ // fall through
+ }
- if (calType == null) {
- // Use "gregorian" as the last resort fallback.
- calType = DEFCAL;
- }
+ if (calType == null) {
+ // Use "gregorian" as the last resort fallback.
+ calType = DEFCAL;
}
// Cache the resolved value for the next time
- CALTYPE_CACHE.put(baseLoc, calType);
+ CALTYPE_CACHE.put(region, calType);
return calType;
}
}
}
+ private static class RgKeyValueTypeHandler extends SpecialTypeHandler {
+ private static final Pattern pat = Pattern.compile("[a-zA-Z]{2}[zZ]{4}");
+ boolean isValid(String value) {
+ return pat.matcher(value).matches();
+ }
+ }
+
private enum SpecialType {
CODEPOINTS(new CodepointsTypeHandler()),
- REORDER_CODE(new ReorderCodeTypeHandler());
+ REORDER_CODE(new ReorderCodeTypeHandler()),
+ RG_KEY_VALUE(new RgKeyValueTypeHandler());
SpecialTypeHandler handler;
SpecialType(SpecialTypeHandler handler) {
}
private static String getRegionForCalendar(ULocale loc) {
- String region = loc.getCountry();
+ String region = ULocale.getRegionForSupplementalData(loc, true);
if (region.length() == 0) {
- ULocale maxLocale = ULocale.addLikelySubtags(loc);
- region = maxLocale.getCountry();
- if (region.length() == 0) {
- region = "001";
- }
+ region = "001";
}
return region;
}
public static final String[] getKeywordValuesForLocale(String key, ULocale locale,
boolean commonlyUsed) {
// Resolve region
- String prefRegion = locale.getCountry();
- if (prefRegion.length() == 0){
- ULocale loc = ULocale.addLikelySubtags(locale);
- prefRegion = loc.getCountry();
- }
+ String prefRegion = ULocale.getRegionForSupplementalData(locale, true);
// Read preferred calendar values from supplementalData calendarPreferences
ArrayList<String> values = new ArrayList<String>();
/**
*******************************************************************************
- * Copyright (C) 2001-2015, International Business Machines Corporation and
+ * Copyright (C) 2001-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
* @stable ICU 4.0
*/
public static String[] getAvailableCurrencyCodes(ULocale loc, Date d) {
- CurrencyFilter filter = CurrencyFilter.onDate(d).withRegion(loc.getCountry());
+ String region = ULocale.getRegionForSupplementalData(loc, false);
+ CurrencyFilter filter = CurrencyFilter.onDate(d).withRegion(region);
List<String> list = getTenderCurrencies(filter);
// Note: Prior to 4.4 the spec didn't say that we return null if there are no results, but
// the test assumed it did. Kept the behavior and amended the spec.
String code = currencyCodeCache.get(loc);
if (code == null) {
- String country = loc.getCountry();
+ String country = ULocale.getRegionForSupplementalData(loc, false);
CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
List<String> list = info.currencies(CurrencyFilter.onRegion(country));
// Don't resolve region if the requested locale is 'und', it will resolve to US
// which we don't want.
- String prefRegion = locale.getCountry();
- if (prefRegion.length() == 0) {
- if (UND.equals(locale)) {
- return EMPTY_STRING_ARRAY;
- }
- ULocale loc = ULocale.addLikelySubtags(locale);
- prefRegion = loc.getCountry();
- }
+ if (UND.equals(locale)) {
+ return EMPTY_STRING_ARRAY;
+ }
+ String prefRegion = ULocale.getRegionForSupplementalData(locale, true);
CurrencyFilter filter = CurrencyFilter.now().withRegion(prefRegion);
/*
**************************************************************************************
- * Copyright (C) 2009-2014, Google, Inc.; International Business Machines Corporation *
- * and others. All Rights Reserved. *
+ * Copyright (C) 2009-2016, International Business Machines Corporation, *
+ * Google, Inc. and others. All Rights Reserved. *
**************************************************************************************
*/
package com.ibm.icu.util;
private static UResourceBundle measurementTypeBundleForLocale(ULocale locale, String measurementType){
// Much of this is taken from getCalendarType in impl/CalendarUtil.java
UResourceBundle measTypeBundle = null;
- ULocale fullLoc = ULocale.addLikelySubtags(locale);
- String region = fullLoc.getCountry();
+ String region = ULocale.getRegionForSupplementalData(locale, true);
try {
UResourceBundle rb = UResourceBundle.getBundleInstance(
ICUResourceBundle.ICU_BASE_NAME,
/*
******************************************************************************
- * Copyright (C) 2003-2015, International Business Machines Corporation and
+ * Copyright (C) 2003-2016, International Business Machines Corporation and
* others. All Rights Reserved.
******************************************************************************
*/
return new LocaleIDParser(localeID).getCountry();
}
+ /**
+ * {@icu} Get the region to use for supplemental data lookup.
+ * Uses
+ * (1) any region specified by locale tag "rg"; if none then
+ * (2) any unicode_region_tag in the locale ID; if none then
+ * (3) if inferRegion is TRUE, the region suggested by
+ * getLikelySubtags on the localeID.
+ * If no region is found, returns empty string ""
+ *
+ * @param locale
+ * The locale (includes any keywords) from which
+ * to get the region to use for supplemental data.
+ * @param inferRegion
+ * If TRUE, will try to infer region from other
+ * locale elements if not found any other way.
+ * @return
+ * String with region to use ("" if none found).
+ * @internal ICU 57
+ * @deprecated This API is ICU internal only.
+ */
+ @Deprecated
+ public static String getRegionForSupplementalData(
+ ULocale locale, boolean inferRegion) {
+ String region = locale.getKeywordValue("rg");
+ if (region != null && region.length() == 6) {
+ String regionUpper = AsciiUtil.toUpperString(region);
+ if (regionUpper.endsWith("ZZZZ")) {
+ return regionUpper.substring(0,2);
+ }
+ }
+ region = locale.getCountry();
+ if (region.length() == 0 && inferRegion) {
+ ULocale maximized = addLikelySubtags(locale);
+ region = maximized.getCountry();
+ }
+ return region;
+ }
+
/**
* Returns the variant code for this locale, which might be the empty string.
* @see #getDisplayVariant()
/**
*******************************************************************************
- * Copyright (C) 2000-2014, International Business Machines Corporation and *
+ * Copyright (C) 2000-2016, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
{"en@calendar=islamic", "gregorian"},
{"zh_TW", "gregorian", "roc", "chinese"},
{"ar_IR", "persian", "gregorian", "islamic", "islamic-civil", "islamic-tbla"},
+ {"th@rg=SAZZZZ", "islamic-umalqura", "gregorian", "islamic", "islamic-rgsa"},
};
String[] ALL = Calendar.getKeywordValuesForLocale("calendar", ULocale.getDefault(), false);
Calendar aCalendar = Calendar.getInstance(Locale.US);
assertEquals("US", usWeekData, aCalendar.getWeekData());
+ Calendar rgusCalendar = Calendar.getInstance(new ULocale("hi_IN@rg=USzzzz"));
+ assertEquals("IN@rg=US", usWeekData, rgusCalendar.getWeekData());
aCalendar.setWeekData(testWeekData);
assertEquals("Custom", testWeekData, aCalendar.getWeekData());
/*
*******************************************************************************
- * Copyright (C) 2000-2014, International Business Machines Corporation and
+ * Copyright (C) 2000-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
"th", // th's default region is TH and buddhist is used as default for TH
"en_TH", // Default calendar for any locales with region TH is buddhist
"th_TH@calendar=iso8601", // iso8601 calendar type
+ "fr_CH",
+ "fr_SA",
+ "fr_CH@rg=sazzzz",
+ "fr_CH@calendar=japanese;rg=sazzzz",
+ "fr_TH@rg=SA", // ignore malformed rg tag, use buddhist
+ "th@rg=SA", // ignore malformed rg tag, use buddhist
};
String[] types = {
"buddhist",
"buddhist",
"buddhist",
- "gregorian", // iso8601 is a gregiran sub type
+ "gregorian", // iso8601 is a gregorian sub type
+ "gregorian",
+ "islamic-umalqura",
+ "islamic-umalqura",
+ "japanese",
+ "buddhist",
+ "buddhist",
};
for (int i = 0; i < locs.length; i++) {
/*
**********************************************************************
- * Copyright (c) 2002-2015, International Business Machines
+ * Copyright (c) 2002-2016, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
{"en@currency=CAD", "USD", "USN"},
{"fr@currency=ZZZ", "EUR"},
{"de_DE@currency=DEM", "EUR"},
+ {"en_US@rg=THZZZZ", "THB"},
+ {"de@rg=USZZZZ", "USD", "USN"},
+ {"en_US@currency=CAD;rg=THZZZZ", "THB"},
};
String[] ALL = Currency.getKeywordValuesForLocale("currency", ULocale.getDefault(), false);
/*
*******************************************************************************
- * Copyright (C) 2003-2015, International Business Machines Corporation and *
+ * Copyright (C) 2003-2016, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
}
}
+ public void TestMeasurementSysForSpecificLocales(){
+ class TestMeasurementSysItem {
+ public String localeID;
+ public LocaleData.MeasurementSystem measureSys;
+ public TestMeasurementSysItem(String locID, LocaleData.MeasurementSystem ms) {
+ localeID = locID;
+ measureSys = ms;
+ }
+ };
+ final TestMeasurementSysItem[] items = {
+ new TestMeasurementSysItem("fr_FR", LocaleData.MeasurementSystem.SI),
+ new TestMeasurementSysItem("en", LocaleData.MeasurementSystem.US),
+ new TestMeasurementSysItem("en_GB", LocaleData.MeasurementSystem.UK),
+ new TestMeasurementSysItem("fr_FR@rg=GBZZZZ", LocaleData.MeasurementSystem.UK),
+ new TestMeasurementSysItem("en@rg=frzzzz", LocaleData.MeasurementSystem.SI),
+ new TestMeasurementSysItem("en_GB@rg=USZZZZ", LocaleData.MeasurementSystem.US),
+ };
+ for (TestMeasurementSysItem item: items) {
+ LocaleData.MeasurementSystem ms = LocaleData.getMeasurementSystem(new ULocale(item.localeID));
+ if (ms != item.measureSys) {
+ errln("For locale " + item.localeID + ", expected " + item.measureSys + ", got " + ms);
+ }
+ }
+ }
+
// Simple test case for checking exemplar character type coverage
public void TestEnglishExemplarCharacters() {
final char[] testChars = {