Example #1
0
        public virtual CurrencyDisplayInfo GetInstance(UCultureInfo culture, bool withFallback)
        {
            // Make sure the locale is non-null (this can happen during deserialization):
            if (culture == null)
            {
                culture = UCultureInfo.InvariantCulture;
            }
            ICUCurrencyDisplayInfo instance = currencyDisplayInfoCache;

            if (instance == null || !culture.Equals(instance.culture) || instance.fallback != withFallback)
            {
                ICUResourceBundle rb;
                if (withFallback)
                {
                    rb = ICUResourceBundle.GetBundleInstance(ICUData.IcuCurrencyBaseName, culture, ICUResourceBundle.IcuDataAssembly, OpenType.LocaleDefaultRoot);
                }
                else
                {
                    try
                    {
                        rb = ICUResourceBundle.GetBundleInstance(ICUData.IcuCurrencyBaseName, culture, ICUResourceBundle.IcuDataAssembly, OpenType.LocaleOnly);
                    }
                    catch (MissingManifestResourceException)
                    {
                        return(null);
                    }
                }
                instance = new ICUCurrencyDisplayInfo(culture, rb, withFallback);
                currencyDisplayInfoCache = instance;
            }
            return(instance);
        }
Example #2
0
        private void getFunctionalEquivalentTestCases(String path, Assembly cl, String resName, String keyword,
                                                      bool truncate, String[] testCases)
        {
            //String F_STR = "f";
            String T_STR = "t";

            bool[] isAvail = new bool[1];

            Logln("Testing functional equivalents...");
            for (int i = 0; i < testCases.Length; i += 3)
            {
                bool         expectAvail  = T_STR.Equals(testCases[i + 0]);
                UCultureInfo inLocale     = new UCultureInfo(testCases[i + 1]);
                UCultureInfo expectLocale = new UCultureInfo(testCases[i + 2]);

                Logln(((int)(i / 3)).ToString(CultureInfo.InvariantCulture) + ": " + expectAvail.ToString() + "\t\t" +
                      inLocale.ToString() + "\t\t" + expectLocale.ToString());

                UCultureInfo equivLocale = ICUResourceBundle.GetFunctionalEquivalent(path, cl, resName, keyword, inLocale, isAvail, truncate);
                bool         gotAvail    = isAvail[0];

                if ((gotAvail != expectAvail) || !equivLocale.Equals(expectLocale))
                {
                    Errln(((int)(i / 3)).ToString(CultureInfo.InvariantCulture) + ":  Error, expected  Equiv=" + expectAvail.ToString() + "\t\t" +
                          inLocale.ToString() + "\t\t--> " + expectLocale.ToString() + ",  but got " + gotAvail.ToString() + " " +
                          equivLocale.ToString());
                }
            }
        }
Example #3
0
        public void TestGetFunctionalEquivalent()
        {
            var kw = Collator.Keywords;

            String[] DATA =
            {
                "sv",                          "sv",                    "t",
                "sv@collation=direct",         "sv",                    "t",
                "sv@collation=traditional",    "sv",                    "t",
                "sv@collation=gb2312han",      "sv",                    "t",
                "sv@collation=stroke",         "sv",                    "t",
                "sv@collation=pinyin",         "sv",                    "t",
                "sv@collation=standard",       "sv@collation=standard", "t",
                "sv@collation=reformed",       "sv",                    "t",
                "sv@collation=big5han",        "sv",                    "t",
                "sv_FI",                       "sv",                    "f",
                "sv_FI@collation=direct",      "sv",                    "f",
                "sv_FI@collation=traditional", "sv",                    "f",
                "sv_FI@collation=gb2312han",   "sv",                    "f",
                "sv_FI@collation=stroke",      "sv",                    "f",
                "sv_FI@collation=pinyin",      "sv",                    "f",
                "sv_FI@collation=standard",    "sv@collation=standard", "f",
                "sv_FI@collation=reformed",    "sv",                    "f",
                "sv_FI@collation=big5han",     "sv",                    "f",
                "nl",                          "root",                  "t",
                "nl@collation=direct",         "root",                  "t",
                "nl_BE",                       "root",                  "f",
                "nl_BE@collation=direct",      "root",                  "f",
                "nl_BE@collation=traditional", "root",                  "f",
                "nl_BE@collation=gb2312han",   "root",                  "f",
                "nl_BE@collation=stroke",      "root",                  "f",
                "nl_BE@collation=pinyin",      "root",                  "f",
                "nl_BE@collation=big5han",     "root",                  "f",
                "nl_BE@collation=phonebook",   "root",                  "f",
                "en_US_VALLEYGIRL",            "root",                  "f"
            };
            int DATA_COUNT = (DATA.Length / 3);

            for (int i = 0; i < DATA_COUNT; i++)
            {
                bool[]       isAvailable     = new bool[1];
                UCultureInfo input           = new UCultureInfo(DATA[(i * 3) + 0]);
                UCultureInfo expect          = new UCultureInfo(DATA[(i * 3) + 1]);
                bool         expectAvailable = DATA[(i * 3) + 2].Equals("t");
                UCultureInfo actual          = Collator.GetFunctionalEquivalent(kw[0], input, isAvailable);
                if (!actual.Equals(expect) || (expectAvailable != isAvailable[0]))
                {
                    Errln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + isAvailable[0] + ", " +
                          "expected " + expect + " avail " + expectAvailable);
                }
                else
                {
                    Logln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + isAvailable[0]);
                }
            }
        }
Example #4
0
        public static CollationTailoring LoadTailoring(UCultureInfo locale, out UCultureInfo outValidLocale)
        {
            // Java porting note: ICU4J getWithFallback/getStringWithFallback currently does not
            // work well when alias table is involved in a resource path, unless full path is specified.
            // For now, collation resources does not contain such data, so the code below should work fine.

            CollationTailoring root       = CollationRoot.Root;
            string             localeName = locale.FullName;

            if (localeName.Length == 0 || localeName.Equals("root"))
            {
                outValidLocale = UCultureInfo.InvariantCulture;
                return(root);
            }

            UResourceBundle bundle = null;

            try
            {
                bundle = ICUResourceBundle.GetBundleInstance(
                    ICUData.IcuCollationBaseName, locale,
                    // ICU4N specific - need to pass in this assembly
                    // name for the resources to be resolved here.
                    ICUResourceBundle.IcuDataAssembly,
                    OpenType.LocaleRoot);
            }
            catch (MissingManifestResourceException)
            {
                outValidLocale = UCultureInfo.InvariantCulture;
                return(root);
            }

            UCultureInfo validLocale = bundle.UCulture;
            // Normalize the root locale. See
            // http://bugs.icu-project.org/trac/ticket/10715
            string validLocaleName = validLocale.FullName;

            if (validLocaleName.Length == 0 || validLocaleName.Equals("root"))
            {
                validLocale = UCultureInfo.InvariantCulture;
            }
            outValidLocale = validLocale;

            // There are zero or more tailorings in the collations table.
            UResourceBundle collations;

            try
            {
                collations = bundle.Get("collations");
                if (collations == null)
                {
                    return(root);
                }
            }
            catch (MissingManifestResourceException)
            {
                return(root);
            }

            // Fetch the collation type from the locale ID and the default type from the data.
            locale.Keywords.TryGetValue("collation", out string type);
            string defaultType = "standard";

            string defT = ((ICUResourceBundle)collations).FindStringWithFallback("default");

            if (defT != null)
            {
                defaultType = defT;
            }

            if (type == null || type.Equals("default"))
            {
                type = defaultType;
            }
            else
            {
                type = ASCII.ToLower(type);
            }

            // Load the collations/type tailoring, with type fallback.

            // Java porting note: typeFallback is used for setting U_USING_DEFAULT_WARNING in
            // ICU4C, but not used by ICU4J

            // boolean typeFallback = false;
            UResourceBundle data = FindWithFallback(collations, type);

            if (data == null &&
                type.Length > 6 && type.StartsWith("search", StringComparison.Ordinal))
            {
                // fall back from something like "searchjl" to "search"
                // typeFallback = true;
                type = "search";
                data = FindWithFallback(collations, type);
            }

            if (data == null && !type.Equals(defaultType))
            {
                // fall back to the default type
                // typeFallback = true;
                type = defaultType;
                data = FindWithFallback(collations, type);
            }

            if (data == null && !type.Equals("standard"))
            {
                // fall back to the "standard" type
                // typeFallback = true;
                type = "standard";
                data = FindWithFallback(collations, type);
            }

            if (data == null)
            {
                return(root);
            }

            // Is this the same as the root collator? If so, then use that instead.
            UCultureInfo actualLocale = data.UCulture;
            // http://bugs.icu-project.org/trac/ticket/10715 ICUResourceBundle(root).UCulture != UCultureInfo.InvariantCulture
            // Therefore not just if (actualLocale.Equals(UCultureInfo.InvariantCulture) && type.Equals("standard")) {
            string actualLocaleName = actualLocale.FullName;

            if (actualLocaleName.Length == 0 || actualLocaleName.Equals("root"))
            {
                actualLocale = UCultureInfo.InvariantCulture;
                if (type.Equals("standard"))
                {
                    return(root);
                }
            }

            CollationTailoring t = new CollationTailoring(root.Settings)
            {
                ActualCulture = actualLocale
            };

            // deserialize
            UResourceBundle binary  = data.Get("%%CollationBin");
            ByteBuffer      inBytes = binary.GetBinary();

            try
            {
                CollationDataReader.Read(root, inBytes, t);
            }
            catch (IOException e)
            {
                throw new ICUUncheckedIOException("Failed to load collation tailoring data for locale:"
                                                  + actualLocale + " type:" + type, e);
            }

            // Try to fetch the optional rules string.
            try
            {
                t.SetRulesResource(data.Get("Sequence"));
            }
            catch (MissingManifestResourceException)
            {
            }

            // Set the collation types on the informational locales,
            // except when they match the default types (for brevity and backwards compatibility).
            // For the valid locale, suppress the default type.
            if (!type.Equals(defaultType))
            {
                outValidLocale = validLocale.SetKeywordValue("collation", type);
            }

            // For the actual locale, suppress the default type *according to the actual locale*.
            // For example, zh has default=pinyin and contains all of the Chinese tailorings.
            // zh_Hant has default=stroke but has no other data.
            // For the valid locale "zh_Hant" we need to suppress stroke.
            // For the actual locale "zh" we need to suppress pinyin instead.
            if (!actualLocale.Equals(validLocale))
            {
                // Opening a bundle for the actual locale should always succeed.
                UResourceBundle actualBundle = UResourceBundle.GetBundleInstance(
                    ICUData.IcuCollationBaseName, actualLocale);
                defT = ((ICUResourceBundle)actualBundle).FindStringWithFallback("collations/default");
                if (defT != null)
                {
                    defaultType = defT;
                }
            }

            if (!type.Equals(defaultType))
            {
                t.ActualCulture = t.ActualCulture.SetKeywordValue("collation", type);
            }

            // if (typeFallback) {
            //     ICU4C implementation sets U_USING_DEFAULT_WARNING here
            // }

            return(t);
        }