Exemplo n.º 1
0
        public LocaleDisplayNamesImpl(ULocale locale, params DisplayContext[] contexts)
#pragma warning disable 612, 618
            : base()
#pragma warning restore 612, 618
        {
            DialectHandling dialectHandling    = DialectHandling.STANDARD_NAMES;
            DisplayContext  capitalization     = DisplayContext.CapitalizationNone;
            DisplayContext  nameLength         = DisplayContext.LengthFull;
            DisplayContext  substituteHandling = DisplayContext.Substitute;

            foreach (DisplayContext contextItem in contexts)
            {
                switch (contextItem.Type())
                {
                case DisplayContextType.DialectHandling:
                    dialectHandling = (contextItem.Value() == DisplayContext.StandardNames.Value()) ?
                                      DialectHandling.STANDARD_NAMES : DialectHandling.DIALECT_NAMES;
                    break;

                case DisplayContextType.Capitalization:
                    capitalization = contextItem;
                    break;

                case DisplayContextType.DisplayLength:
                    nameLength = contextItem;
                    break;

                case DisplayContextType.SubstituteHandling:
                    substituteHandling = contextItem;
                    break;

                default:
                    break;
                }
            }

            this.dialectHandling    = dialectHandling;
            this.capitalization     = capitalization;
            this.nameLength         = nameLength;
            this.substituteHandling = substituteHandling;
            this.langData           = LangDataTables.impl.Get(locale, substituteHandling == DisplayContext.NoSubstitute);
            this.regionData         = RegionDataTables.impl.Get(locale, substituteHandling == DisplayContext.NoSubstitute);
            this.locale             = ULocale.ROOT.Equals(langData.GetLocale()) ? regionData.GetLocale() :
                                      langData.GetLocale();

            // Note, by going through DataTable, this uses table lookup rather than straight lookup.
            // That should get us the same data, I think.  This way we don't have to explicitly
            // load the bundle again.  Using direct lookup didn't seem to make an appreciable
            // difference in performance.
            string sep = langData.Get("localeDisplayPattern", "separator");

            if (sep == null || "separator".Equals(sep))
            {
                sep = "{0}, {1}";
            }
            StringBuilder sb = new StringBuilder();

            this.separatorFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments(sep, sb, 2, 2);

            string pattern = langData.Get("localeDisplayPattern", "pattern");

            if (pattern == null || "pattern".Equals(pattern))
            {
                pattern = "{0} ({1})";
            }
            this.format = SimpleFormatterImpl.CompileToStringMinMaxArguments(pattern, sb, 2, 2);
            if (pattern.Contains("("))
            {
                formatOpenParen         = '(';
                formatCloseParen        = ')';
                formatReplaceOpenParen  = '[';
                formatReplaceCloseParen = ']';
            }
            else
            {
                formatOpenParen         = '(';
                formatCloseParen        = ')';
                formatReplaceOpenParen  = '[';
                formatReplaceCloseParen = ']';
            }

            string keyTypePattern = langData.Get("localeDisplayPattern", "keyTypePattern");

            if (keyTypePattern == null || "keyTypePattern".Equals(keyTypePattern))
            {
                keyTypePattern = "{0}={1}";
            }
            this.keyTypeFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments(
                keyTypePattern, sb, 2, 2);

            // Get values from the contextTransforms data if we need them
            // Also check whether we will need a break iterator (depends on the data)
            bool needBrkIter = false;

            if (capitalization == DisplayContext.CapitalizationForUIListOrMenu ||
                capitalization == DisplayContext.CapitalizationForStandalone)
            {
                capitalizationUsage = new bool[Enum.GetValues(typeof(CapitalizationContextUsage)).Length]; // initialized to all false
                ICUResourceBundle         rb   = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.ICU_BASE_NAME, locale);
                CapitalizationContextSink sink = new CapitalizationContextSink(this);
                try
                {
                    rb.GetAllItemsWithFallback("contextTransforms", sink);
                }
                catch (MissingManifestResourceException e)
                {
                    // Silently ignore.  Not every locale has contextTransforms.
                }
                needBrkIter = sink.hasCapitalizationUsage;
            }
            // Get a sentence break iterator if we will need it
            if (needBrkIter || capitalization == DisplayContext.CapitalizationForBeginningOfSentence)
            {
                capitalizationBrkIter = BreakIterator.GetSentenceInstance(locale);
            }

            this.currencyDisplayInfo = CurrencyData.Provider.GetInstance(locale, false);
        }
Exemplo n.º 2
0
        public DataTableCultureDisplayNames(UCultureInfo culture, DisplayContextOptions options)
#pragma warning disable 612, 618
            : base()
#pragma warning restore 612, 618
        {
            this.displayContextOptions = options.Freeze();
            this.langData   = languageDataTableProvider.GetDataTable(culture, options.SubstituteHandling == SubstituteHandling.NoSubstitute);
            this.regionData = regionDataTableProvider.GetDataTable(culture, options.SubstituteHandling == SubstituteHandling.NoSubstitute);
            this.locale     = langData.CultureInfo != null && langData.CultureInfo.Equals(CultureInfo.InvariantCulture)
                ? regionData.CultureInfo.ToUCultureInfo()
                : langData.CultureInfo.ToUCultureInfo();

            // Note, by going through DataTable, this uses table lookup rather than straight lookup.
            // That should get us the same data, I think.  This way we don't have to explicitly
            // load the bundle again.  Using direct lookup didn't seem to make an appreciable
            // difference in performance.
            string sep = langData.Get("localeDisplayPattern", "separator");

            if (sep == null || "separator".Equals(sep))
            {
                sep = "{0}, {1}";
            }
            StringBuilder sb = new StringBuilder();

            this.separatorFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments(sep, sb, 2, 2);

            string pattern = langData.Get("localeDisplayPattern", "pattern");

            if (pattern == null || "pattern".Equals(pattern))
            {
                pattern = "{0} ({1})";
            }
            this.format = SimpleFormatterImpl.CompileToStringMinMaxArguments(pattern, sb, 2, 2);
            if (pattern.Contains("("))
            {
                formatOpenParen         = '(';
                formatCloseParen        = ')';
                formatReplaceOpenParen  = '[';
                formatReplaceCloseParen = ']';
            }
            else
            {
                formatOpenParen         = '(';
                formatCloseParen        = ')';
                formatReplaceOpenParen  = '[';
                formatReplaceCloseParen = ']';
            }

            string keyTypePattern = langData.Get("localeDisplayPattern", "keyTypePattern");

            if (keyTypePattern == null || "keyTypePattern".Equals(keyTypePattern))
            {
                keyTypePattern = "{0}={1}";
            }
            this.keyTypeFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments(
                keyTypePattern, sb, 2, 2);

            // Get values from the contextTransforms data if we need them
            // Also check whether we will need a break iterator (depends on the data)
            bool needBrkIter = false;

            if (options.Capitalization == Capitalization.UIListOrMenu ||
                options.Capitalization == Capitalization.Standalone)
            {
                capitalizationUsage = new bool[Enum.GetValues(typeof(CapitalizationContextUsage)).Length]; // initialized to all false
                ICUResourceBundle         rb   = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuBaseName, locale);
                CapitalizationContextSink sink = new CapitalizationContextSink(this);
                try
                {
                    rb.GetAllItemsWithFallback("contextTransforms", sink);
                }
                catch (MissingManifestResourceException)
                {
                    // Silently ignore.  Not every locale has contextTransforms.
                }
                needBrkIter = sink.hasCapitalizationUsage;
            }
            // Get a sentence break iterator if we will need it
            if (needBrkIter || options.Capitalization == Capitalization.BeginningOfSentence)
            {
                capitalizationBrkIter = BreakIterator.GetSentenceInstance(locale);
            }

            this.currencyDisplayInfo = CurrencyData.Provider.GetInstance(locale, false);
        }