/// <summary> /// Return the ResourceBundle for this spec, at the current level of /// iteration. The level of iteration goes from aa_BB_CCC to aa_BB to aa. /// If the bundle does not correspond to the current level of iteration, /// return null. If isLocale() is false, always return null. /// </summary> /// public ResourceBundle GetBundle() { if (res != null && res.GetULocale().ToString().Equals(spec)) { return(res); } return(null); }
public Spec(string theSpec) { top = theSpec; spec = null; scriptName = null; try { // Canonicalize script name. If top is a script name then // script != UScript.INVALID_CODE. int script = UScript.GetCodeFromName(top); // Canonicalize script name -or- do locale->script mapping int[] s = UScript.GetCode(top); if (s != null) { scriptName = UScript.GetName(s[0]); // If the script name is the same as top then it's redundant if (scriptName.Equals(top, StringComparison.OrdinalIgnoreCase)) { scriptName = null; } } isSpecLocale = false; res = null; // If 'top' is not a script name, try a locale lookup if (script == UScript.InvalidCode) { // ICU4N specific - CultureInfo doesn't support IANA culture names, so we use ULocale instead. ULocale toploc = new ULocale(top); //CultureInfo toploc = LocaleUtility.GetLocaleFromName(top); res = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuTransliteratorBaseName, toploc, Transliterator.ICU_DATA_CLASS_LOADER); // Make sure we got the bundle we wanted; otherwise, don't use it if (res != null && LocaleUtility.IsFallbackOf(res.GetULocale().ToString(), top)) { isSpecLocale = true; } } } catch (MissingManifestResourceException) { ////CLOVER:OFF // The constructor is called from multiple private methods // that protects an invalid scriptName scriptName = null; ////CLOVER:ON } // assert(spec != top); Reset(); }
public Spec(String theSpec) { top = theSpec; spec = null; scriptName = null; try { // Canonicalize script name. If top is a script name then // script != UScript.INVALID_CODE. int script = IBM.ICU.Lang.UScript.GetCodeFromName(top); // Canonicalize script name -or- do locale->script mapping int[] s = IBM.ICU.Lang.UScript.GetCode(top); if (s != null) { scriptName = IBM.ICU.Lang.UScript.GetName(s[0]); // If the script name is the same as top then it's redundant if (scriptName.Equals(top, StringComparison.InvariantCultureIgnoreCase)) { scriptName = null; } } isSpecLocale = false; res = null; // If 'top' is not a script name, try a locale lookup if (script == IBM.ICU.Lang.UScript.INVALID_CODE) { Locale toploc = IBM.ICU.Impl.LocaleUtility.GetLocaleFromName(top); res = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle .GetBundleInstance( IBM.ICU.Impl.ICUResourceBundle.ICU_TRANSLIT_BASE_NAME, toploc); // Make sure we got the bundle we wanted; otherwise, don't // use it if (res != null && IBM.ICU.Impl.LocaleUtility.IsFallbackOf(res.GetULocale() .ToString(), top)) { isSpecLocale = true; } } } catch (MissingManifestResourceException e) { scriptName = null; } // assert(spec != top); Reset(); }
public void TestGetWithFallback() { /* * UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN"); * String key = bundle.getStringWithFallback("Keys/collation"); * if(!key.equals("COLLATION")){ * Errln("Did not get the expected result from getStringWithFallback method."); * } * String type = bundle.getStringWithFallback("Types/collation/direct"); * if(!type.equals("DIRECT")){ * Errln("Did not get the expected result form getStringWithFallback method."); * } */ ICUResourceBundle bundle = null; String key = null; try { bundle = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuCollationBaseName, ULocale.Canonicalize("de__PHONEBOOK")); if (!bundle.GetULocale().GetName().Equals("de")) { Errln("did not get the expected bundle"); } key = bundle.GetStringWithFallback("collations/collation/default"); if (!key.Equals("phonebook")) { Errln("Did not get the expected result from getStringWithFallback method."); } } catch (MissingManifestResourceException ex) { Logln("got the expected exception"); } bundle = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuCollationBaseName, "fr_FR"); key = bundle.GetStringWithFallback("collations/default"); if (!key.Equals("standard")) { Errln("Did not get the expected result from getStringWithFallback method."); } }
/// <summary> /// Utility to fetch locale display data from resource bundle tables. Uses fallback /// through the "Fallback" resource if available. /// </summary> public static string GetTableString(ICUResourceBundle bundle, string tableName, string subtableName, string item, string defaultValue) { string result = null; try { for (; ;) { ICUResourceBundle table = bundle.FindWithFallback(tableName); if (table == null) { return(defaultValue); } ICUResourceBundle stable = table; if (subtableName != null) { stable = table.FindWithFallback(subtableName); } if (stable != null) { result = stable.FindStringWithFallback(item); if (result != null) { break; // possible real exception } } // if we get here, stable was null, or there was no string for the item if (subtableName == null) { // may be a deprecated code string currentName = null; if (tableName.Equals("Countries")) { currentName = LocaleIDs.GetCurrentCountryID(item); } else if (tableName.Equals("Languages")) { currentName = LocaleIDs.GetCurrentLanguageID(item); } if (currentName != null) { result = table.FindStringWithFallback(currentName); if (result != null) { break; // possible real exception } } } // still can't figure it out? try the fallback mechanism string fallbackLocale = table.FindStringWithFallback("Fallback"); // again, possible exception if (fallbackLocale == null) { return(defaultValue); } if (fallbackLocale.Length == 0) { fallbackLocale = "root"; } if (fallbackLocale.Equals(table.GetULocale().GetName())) { return(defaultValue); } bundle = (ICUResourceBundle)UResourceBundle.GetBundleInstance( bundle.GetBaseName(), fallbackLocale); } } catch (Exception) { // If something is seriously wrong, we might call getString on a resource that is // not a string. That will throw an exception, which we catch and ignore here. } // If the result is empty return item instead return((result != null && result.Length > 0) ? result : defaultValue); }
public override ULocale GetLocale() { return(bundle.GetULocale()); }
/// <summary> /// Initializes the symbols from the LocaleElements resource bundle. Note: /// The organization of LocaleElements badly needs to be cleaned up. /// </summary> /// private void Initialize(ULocale locale) { this.requestedLocale = locale.ToLocale(); this.ulocale = locale; /* try the cache first */ String[][] data = (String[][])cachedLocaleData[locale]; String[] numberElements; if (data == null) /* cache miss */ { data = new String[1][]; ICUResourceBundle rb = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale); data[0] = rb.GetStringArray("NumberElements"); /* update cache */ ILOG.J2CsMapping.Collections.Collections.Put(cachedLocaleData, locale, data); } numberElements = data[0]; ICUResourceBundle r = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale); // TODO: Determine actual and valid locale correctly. ULocale uloc = r.GetULocale(); SetLocale(uloc, uloc); // {dlf} clean up below now that we have our own resource data decimalSeparator = numberElements[0][0]; groupingSeparator = numberElements[1][0]; // Temporary hack to support old JDK 1.1 resources // patternSeparator = numberElements[2].length() > 0 ? // numberElements[2].charAt(0) : ';'; patternSeparator = numberElements[2][0]; percent = numberElements[3][0]; zeroDigit = numberElements[4][0]; // different for Arabic,etc. digit = numberElements[5][0]; minusSign = numberElements[6][0]; // Temporary hack to support JDK versions before 1.1.6 (?) // exponentSeparator = numberElements.length >= 9 ? // numberElements[7] : DecimalFormat.PATTERN_EXPONENT; // perMill = numberElements.length >= 9 ? // numberElements[8].charAt(0) : '\u2030'; // infinity = numberElements.length >= 10 ? // numberElements[9] : "\u221e"; // NaN = numberElements.length >= 11 ? // numberElements[10] : "\ufffd"; exponentSeparator = numberElements[7]; perMill = numberElements[8][0]; infinity = numberElements[9]; NaN = numberElements[10]; plusSign = numberElements[11][0]; padEscape = IBM.ICU.Text.DecimalFormat.PATTERN_PAD_ESCAPE; sigDigit = IBM.ICU.Text.DecimalFormat.PATTERN_SIGNIFICANT_DIGIT; // Obtain currency data from the currency API. This is strictly // for backward compatibility; we don't use DecimalFormatSymbols // for currency data anymore. String currname = null; currency = IBM.ICU.Util.Currency.GetInstance(locale); if (currency != null) { intlCurrencySymbol = currency.GetCurrencyCode(); bool[] isChoiceFormat = new bool[1]; currname = currency.GetName(locale, IBM.ICU.Util.Currency.SYMBOL_NAME, isChoiceFormat); // If this is a ChoiceFormat currency, then format an // arbitrary value; pick something != 1; more common. currencySymbol = (isChoiceFormat[0]) ? new ChoiceFormat(currname) .Format(2.0d) : currname; } else { intlCurrencySymbol = "XXX"; currencySymbol = "\u00A4"; // 'OX' currency symbol } // If there is a currency decimal, use it. monetarySeparator = decimalSeparator; monetaryGroupingSeparator = groupingSeparator; Currency curr = IBM.ICU.Util.Currency.GetInstance(locale); if (curr != null) { String currencyCode = curr.GetCurrencyCode(); if (currencyCode != null) { /* An explicit currency was requested */ ICUResourceBundle resource = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale); ICUResourceBundle currencyRes = resource .GetWithFallback("Currencies"); try { currencyRes = currencyRes.GetWithFallback(currencyCode); if (currencyRes.GetSize() > 2) { currencyRes = (ICUResourceBundle)currencyRes.Get(2); currencyPattern = currencyRes.GetString(0); monetarySeparator = currencyRes.GetString(1)[0]; monetaryGroupingSeparator = currencyRes.GetString(2)[0]; } } catch (MissingManifestResourceException ex) { /* * else An explicit currency was requested and is unknown or * locale data is malformed. */ /* decimal format API will get the correct value later on. */ } } /* else no currency keyword used. */ } // monetarySeparator = numberElements[11].charAt(0); }