Example #1
0
 private StringBuilder AppendWithSep(string s, StringBuilder b)
 {
     if (b.Length == 0)
     {
         b.Append(s);
     }
     else
     {
         // ICU4N TODO: Does it make sense to call this twice, once for StringBuilder and once for String?
         SimpleFormatterImpl.FormatAndReplace(separatorFormat, b, null, b.ToCharSequence(), s.ToCharSequence());
     }
     return(b);
 }
Example #2
0
        // TODO: implement use of capitalization
        private string LocaleDisplayNameInternal(ULocale locale)
        {
            // lang
            // lang (script, country, variant, keyword=value, ...)
            // script, country, variant, keyword=value, ...

            string resultName = null;

            string lang = locale.GetLanguage();

            // Empty basename indicates root locale (keywords are ignored for this).
            // Our data uses 'root' to access display names for the root locale in the
            // "Languages" table.
            if (locale.GetBaseName().Length == 0)
            {
                lang = "root";
            }
            string script  = locale.GetScript();
            string country = locale.GetCountry();
            string variant = locale.GetVariant();

            bool hasScript  = script.Length > 0;
            bool hasCountry = country.Length > 0;
            bool hasVariant = variant.Length > 0;

            // always have a value for lang
            if (dialectHandling == DialectHandling.DIALECT_NAMES)
            {
                do
                { // loop construct is so we can break early out of search
                    if (hasScript && hasCountry)
                    {
                        string langScriptCountry = lang + '_' + script + '_' + country;
                        string result            = LocaleIdName(langScriptCountry);
                        if (result != null && !result.Equals(langScriptCountry))
                        {
                            resultName = result;
                            hasScript  = false;
                            hasCountry = false;
                            break;
                        }
                    }
                    if (hasScript)
                    {
                        string langScript = lang + '_' + script;
                        string result     = LocaleIdName(langScript);
                        if (result != null && !result.Equals(langScript))
                        {
                            resultName = result;
                            hasScript  = false;
                            break;
                        }
                    }
                    if (hasCountry)
                    {
                        string langCountry = lang + '_' + country;
                        string result      = LocaleIdName(langCountry);
                        if (result != null && !result.Equals(langCountry))
                        {
                            resultName = result;
                            hasCountry = false;
                            break;
                        }
                    }
                } while (false);
            }

            if (resultName == null)
            {
                string result = LocaleIdName(lang);
                if (result == null)
                {
                    return(null);
                }
                resultName = result
                             .Replace(formatOpenParen, formatReplaceOpenParen)
                             .Replace(formatCloseParen, formatReplaceCloseParen);
            }

            StringBuilder buf = new StringBuilder();

            if (hasScript)
            {
                // first element, don't need appendWithSep
                string result = ScriptDisplayNameInContext(script, true);
                if (result == null)
                {
                    return(null);
                }
                buf.Append(result
                           .Replace(formatOpenParen, formatReplaceOpenParen)
                           .Replace(formatCloseParen, formatReplaceCloseParen));
            }
            if (hasCountry)
            {
                string result = RegionDisplayName(country, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }
            if (hasVariant)
            {
                string result = VariantDisplayName(variant, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }

            using (IEnumerator <string> keys = locale.GetKeywords())
            {
                if (keys != null)
                {
                    while (keys.MoveNext())
                    {
                        string key            = keys.Current;
                        string value          = locale.GetKeywordValue(key);
                        string keyDisplayName = KeyDisplayName(key, true);
                        if (keyDisplayName == null)
                        {
                            return(null);
                        }
                        keyDisplayName = keyDisplayName
                                         .Replace(formatOpenParen, formatReplaceOpenParen)
                                         .Replace(formatCloseParen, formatReplaceCloseParen);
                        string valueDisplayName = KeyValueDisplayName(key, value, true);
                        if (valueDisplayName == null)
                        {
                            return(null);
                        }
                        valueDisplayName = valueDisplayName
                                           .Replace(formatOpenParen, formatReplaceOpenParen)
                                           .Replace(formatCloseParen, formatReplaceCloseParen);
                        if (!valueDisplayName.Equals(value))
                        {
                            AppendWithSep(valueDisplayName, buf);
                        }
                        else if (!key.Equals(keyDisplayName))
                        {
                            string keyValue = SimpleFormatterImpl.FormatCompiledPattern(
                                keyTypeFormat, keyDisplayName, valueDisplayName);
                            AppendWithSep(keyValue, buf);
                        }
                        else
                        {
                            AppendWithSep(keyDisplayName, buf)
                            .Append("=")
                            .Append(valueDisplayName);
                        }
                    }
                }
            }
            string resultRemainder = null;

            if (buf.Length > 0)
            {
                resultRemainder = buf.ToString();
            }

            if (resultRemainder != null)
            {
                resultName = SimpleFormatterImpl.FormatCompiledPattern(
                    format, resultName, resultRemainder);
            }

            return(AdjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, resultName));
        }
Example #3
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);
        }