예제 #1
0
 private static RootType GetRootType(string baseName, Assembly root)
 {
     return(ROOT_CACHE.GetOrAdd(baseName, (key) =>
     {
         string rootLocale = (baseName.IndexOf('.') == -1) ? "root" : "";
         try
         {
             ICUResourceBundle.GetBundleInstance(baseName, rootLocale, root, true);
             return RootType.ICU;
         }
         catch (MissingManifestResourceException)
         {
             try
             {
                 ResourceBundleWrapper.GetBundleInstance(baseName, rootLocale, root, true);
                 return RootType.DotNet;
             }
             catch (MissingManifestResourceException)
             {
                 //throw away the exception
                 return RootType.Missing;
             }
         }
     }));
 }
예제 #2
0
        /// <summary>
        /// <icu/> Loads a new resource bundle for the given base name, locale and assembly.
        /// Optionally will disable loading of fallback bundles.
        /// </summary>
        /// <param name="baseName">String containing the name of the data package.
        /// If null the default ICU package name is used.</param>
        /// <param name="localeName">The locale for which a resource bundle is desired.</param>
        /// <param name="root">The class object from which to load the resource bundle.</param>
        /// <param name="disableFallback">Disables loading of fallback lookup chain.</param>
        /// <exception cref="MissingManifestResourceException">If no resource bundle for the specified base name can be found.</exception>
        /// <returns>A resource bundle for the given base name and locale.</returns>
        /// <stable>ICU 3.0</stable>
        protected static UResourceBundle InstantiateBundle(string baseName, string localeName,
                                                           Assembly root, bool disableFallback)
        {
            RootType rootType = GetRootType(baseName, root);

            switch (rootType)
            {
            case RootType.ICU:
                return(ICUResourceBundle.GetBundleInstance(baseName, localeName, root, disableFallback));

            case RootType.DotNet:
                return(ResourceBundleWrapper.GetBundleInstance(baseName, localeName, root,
                                                               disableFallback));

            case RootType.Missing:
            default:
                UResourceBundle b;
                try
                {
                    b = ICUResourceBundle.GetBundleInstance(baseName, localeName, root,
                                                            disableFallback);
                    SetRootType(baseName, RootType.ICU);
                }
                catch (MissingManifestResourceException)
                {
                    b = ResourceBundleWrapper.GetBundleInstance(baseName, localeName, root,
                                                                disableFallback);
                    SetRootType(baseName, RootType.DotNet);
                }
                return(b);
            }
        }
예제 #3
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);
        }
예제 #4
0
        private static RootType GetRootType(string baseName, Assembly root)
        {
            RootType rootType;

            if (!ROOT_CACHE.TryGetValue(baseName, out rootType))
            {
                string rootLocale = (baseName.IndexOf('.') == -1) ? "root" : "";
                try
                {
                    ICUResourceBundle.GetBundleInstance(baseName, rootLocale, root, true);
                    rootType = RootType.ICU;
                }
                catch (MissingManifestResourceException ex)
                {
                    try
                    {
                        ResourceBundleWrapper.GetBundleInstance(baseName, rootLocale, root, true);
                        rootType = RootType.JAVA;
                    }
                    catch (MissingManifestResourceException e)
                    {
                        //throw away the exception
                        rootType = RootType.MISSING;
                    }
                }

                ROOT_CACHE[baseName] = rootType;
            }

            return(rootType);
        }
        public virtual CurrencyDisplayInfo GetInstance(ULocale locale, bool withFallback)
        {
            // Make sure the locale is non-null (this can happen during deserialization):
            if (locale == null)
            {
                locale = ULocale.ROOT;
            }
            ICUCurrencyDisplayInfo instance = currencyDisplayInfoCache;

            if (instance == null || !instance.locale.Equals(locale) || instance.fallback != withFallback)
            {
                ICUResourceBundle rb;
                if (withFallback)
                {
                    rb = ICUResourceBundle.GetBundleInstance(
                        ICUData.ICU_CURR_BASE_NAME, locale, typeof(ICUCurrencyDisplayInfoProvider).GetTypeInfo().Assembly, OpenType.LOCALE_DEFAULT_ROOT);
                }
                else
                {
                    try
                    {
                        rb = ICUResourceBundle.GetBundleInstance(
                            ICUData.ICU_CURR_BASE_NAME, locale, typeof(ICUCurrencyDisplayInfoProvider).GetTypeInfo().Assembly, OpenType.LOCALE_ONLY);
                    }
                    catch (MissingManifestResourceException e)
                    {
                        return(null);
                    }
                }
                instance = new ICUCurrencyDisplayInfo(locale, rb, withFallback);
                currencyDisplayInfoCache = instance;
            }
            return(instance);
        }
예제 #6
0
        private NamePrepTransform()
        {
            // load the resource bundle
            //ICUResourceBundle bundle = (ICUResourceBundle)ICUResourceBundle.GetBundleInstance("com/ibm/icu/dev/data/testdata", "idna_rules", typeof(NamePrepTransform).GetTypeInfo().Assembly, true);
            ICUResourceBundle bundle   = (ICUResourceBundle)ICUResourceBundle.GetBundleInstance("Dev/Data/TestData", "idna_rules", typeof(NamePrepTransform).GetTypeInfo().Assembly, true);
            String            mapRules = bundle.GetString("MapNoNormalization");

            mapRules += bundle.GetString("MapNFKC");
            // disable
            mapTransform      = new MapTransform("CaseMap", mapRules, 0 /*Transliterator.FORWARD*/);
            labelSeparatorSet = new UnicodeSet(bundle.GetString("LabelSeparatorSet"));
            prohibitedSet     = new UnicodeSet(bundle.GetString("ProhibitedSet"));
            unassignedSet     = new UnicodeSet(bundle.GetString("UnassignedSet"));
        }
            /// <summary>
            /// Create <see cref="SimpleFilteredSentenceBreakIterator.Builder"/> using given locale.
            /// </summary>
            /// <param name="loc">The locale to get filtered iterators.</param>
            public Builder(ULocale loc)
#pragma warning disable 612, 618
                : base()
#pragma warning restore 612, 618
            {
                ICUResourceBundle rb = ICUResourceBundle.GetBundleInstance(
                    ICUData.ICU_BRKITR_BASE_NAME, loc, OpenType.LOCALE_ROOT);

                ICUResourceBundle breaks = rb.FindWithFallback("exceptions/SentenceBreak");

                if (breaks != null)
                {
                    for (int index = 0, size = breaks.Length; index < size; ++index)
                    {
                        ICUResourceBundle b  = (ICUResourceBundle)breaks.Get(index);
                        string            br = b.GetString();
                        filterSet.Add(br.ToCharSequence());
                    }
                }
            }
예제 #8
0
        /// <summary>
        /// Create <see cref="SimpleFilteredSentenceBreakIteratorBuilder"/> using given locale.
        /// </summary>
        /// <param name="loc">The locale to get filtered iterators.</param>
        public SimpleFilteredSentenceBreakIteratorBuilder(UCultureInfo loc)
#pragma warning disable 612, 618
            : base()
#pragma warning restore 612, 618
        {
            ICUResourceBundle rb = ICUResourceBundle.GetBundleInstance(
                ICUData.IcuBreakIteratorBaseName, loc, OpenType.LocaleRoot);

            ICUResourceBundle breaks = rb.FindWithFallback("exceptions/SentenceBreak");

            if (breaks != null)
            {
                for (int index = 0, size = breaks.Length; index < size; ++index)
                {
                    ICUResourceBundle b  = (ICUResourceBundle)breaks.Get(index);
                    string            br = b.GetString();
                    filterSet.Add(br.AsCharSequence());
                }
            }
        }
예제 #9
0
        public virtual CurrencyDisplayInfo GetInstance(ULocale locale, bool withFallback)
        {
            // Make sure the locale is non-null (this can happen during deserialization):
            if (locale == null)
            {
                locale = ULocale.ROOT;
            }
            ICUCurrencyDisplayInfo instance = currencyDisplayInfoCache;

            if (instance == null || !instance.locale.Equals(locale) || instance.fallback != withFallback)
            {
                ICUResourceBundle rb;
#if FEATURE_TYPEEXTENSIONS_GETTYPEINFO
                Assembly assembly = typeof(ICUCurrencyDisplayInfoProvider).GetTypeInfo().Assembly;
#else
                Assembly assembly = typeof(ICUCurrencyDisplayInfoProvider).Assembly;
#endif
                if (withFallback)
                {
                    rb = ICUResourceBundle.GetBundleInstance(ICUData.IcuCurrencyBaseName, locale, assembly, OpenType.LocaleDefaultRoot);
                }
                else
                {
                    try
                    {
                        rb = ICUResourceBundle.GetBundleInstance(ICUData.IcuCurrencyBaseName, locale, assembly, OpenType.LocaleOnly);
                    }
                    catch (MissingManifestResourceException)
                    {
                        return(null);
                    }
                }
                instance = new ICUCurrencyDisplayInfo(locale, rb, withFallback);
                currencyDisplayInfoCache = instance;
            }
            return(instance);
        }
예제 #10
0
        public static CollationTailoring LoadTailoring(ULocale locale, out ULocale 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.GetName();

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

            UResourceBundle bundle = null;

            try
            {
                bundle = ICUResourceBundle.GetBundleInstance(
                    ICUData.ICU_COLLATION_BASE_NAME, locale,
                    // ICU4N specific - need to pass in this assembly
                    // name for the resources to be resolved here.
                    CollationData.ICU_DATA_CLASS_LOADER,
                    ICUResourceBundle.OpenType.LOCALE_ROOT);
            }
            catch (MissingManifestResourceException e)
            {
                outValidLocale = ULocale.ROOT;
                return(root);
            }

            ULocale validLocale = bundle.GetULocale();
            // Normalize the root locale. See
            // http://bugs.icu-project.org/trac/ticket/10715
            string validLocaleName = validLocale.GetName();

            if (validLocaleName.Length == 0 || validLocaleName.Equals("root"))
            {
                validLocale = ULocale.ROOT;
            }
            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 ignored)
            {
                return(root);
            }

            // Fetch the collation type from the locale ID and the default type from the data.
            string type        = locale.GetKeywordValue("collation");
            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.
            ULocale actualLocale = data.GetULocale();
            // http://bugs.icu-project.org/trac/ticket/10715 ICUResourceBundle(root).getULocale() != ULocale.ROOT
            // Therefore not just if (actualLocale.equals(ULocale.ROOT) && type.equals("standard")) {
            string actualLocaleName = actualLocale.GetName();

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

            CollationTailoring t = new CollationTailoring(root.Settings);

            t.ActualLocale = 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 ignored)
            {
            }

            // 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.ICU_COLLATION_BASE_NAME, actualLocale);
                defT = ((ICUResourceBundle)actualBundle).FindStringWithFallback("collations/default");
                if (defT != null)
                {
                    defaultType = defT;
                }
            }

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

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

            return(t);
        }
예제 #11
0
 /// <summary>
 /// Create the service.  The default implementation returns the resource bundle
 /// for the locale, ignoring kind, and service.
 /// </summary>
 protected override object HandleCreate(UCultureInfo loc, int kind, ICUService service)
 {
     return(ICUResourceBundle.GetBundleInstance(bundleName, loc, Assembly));
 }
예제 #12
0
 /// <summary>
 /// Return the plurals resource. Note <see cref="MissingManifestResourceException"/> is unchecked,
 /// listed here for clarity. Callers should handle this exception.
 /// </summary>
 public virtual UResourceBundle GetPluralBundle()
 {
     return(ICUResourceBundle.GetBundleInstance(
                ICUData.ICU_BASE_NAME, "plurals",
                ICUResourceBundle.ICU_DATA_CLASS_LOADER, true));
 }
예제 #13
0
 /// <summary>
 /// Return the plurals resource. Note <see cref="MissingManifestResourceException"/> is unchecked,
 /// listed here for clarity. Callers should handle this exception.
 /// </summary>
 public virtual UResourceBundle GetPluralBundle()
 {
     return(ICUResourceBundle.GetBundleInstance(
                ICUData.IcuBaseName, "plurals",
                ICUResourceBundle.IcuDataAssembly, true));
 }
예제 #14
0
        private static BreakIterator CreateBreakInstance(ULocale locale, int kind)
        {
            RuleBasedBreakIterator iter = null;
            ICUResourceBundle      rb   = ICUResourceBundle.
                                          GetBundleInstance(ICUData.ICU_BRKITR_BASE_NAME, locale,
                                                            ICUResourceBundle.OpenType.LOCALE_ROOT);

            //
            //  Get the binary rules.
            //
            ByteBuffer bytes      = null;
            string     typeKeyExt = null;

            if (kind == BreakIterator.KIND_LINE)
            {
                string lbKeyValue = locale.GetKeywordValue("lb");
                if (lbKeyValue != null && (lbKeyValue.Equals("strict") || lbKeyValue.Equals("normal") || lbKeyValue.Equals("loose")))
                {
                    typeKeyExt = "_" + lbKeyValue;
                }
            }

            try
            {
                string typeKey       = (typeKeyExt == null) ? KIND_NAMES[kind] : KIND_NAMES[kind] + typeKeyExt;
                string brkfname      = rb.GetStringWithFallback("boundaries/" + typeKey);
                string rulesFileName = ICUData.ICU_BRKITR_NAME + '/' + brkfname;
                bytes = ICUBinary.GetData(rulesFileName);
            }
            catch (Exception e)
            {
                throw new MissingManifestResourceException(e.ToString(), e /*, "", ""*/);
            }

            //
            // Create a normal RuleBasedBreakIterator.
            //
            try
            {
#pragma warning disable 612, 618
                iter = RuleBasedBreakIterator.GetInstanceFromCompiledRules(bytes);
#pragma warning restore 612, 618
            }
            catch (IOException e)
            {
                // Shouldn't be possible to get here.
                // If it happens, the compiled rules are probably corrupted in some way.
                Assert.Fail(e);
            }
            // TODO: Determine valid and actual locale correctly.
            ULocale uloc = ULocale.ForLocale(rb.GetLocale());
            iter.SetLocale(uloc, uloc);
            iter.BreakType = kind;

            // filtered break
            if (kind == BreakIterator.KIND_SENTENCE)
            {
                string ssKeyword = locale.GetKeywordValue("ss");
                if (ssKeyword != null && ssKeyword.Equals("standard"))
                {
                    ULocale @base = new ULocale(locale.GetBaseName());
                    return(FilteredBreakIteratorBuilder.GetInstance(@base).WrapIteratorWithFilter(iter));
                }
            }

            return(iter);
        }