示例#1
0
 public string Format(params char[][] values)
 {
     return(SimpleFormatterImpl.FormatCompiledPattern(compiledPattern, values));
 }
示例#2
0
 /// <summary>
 /// Formats the given values.
 /// </summary>
 /// <stable>ICU 57</stable>
 internal string Format(params ICharSequence[] values)
 {
     return(SimpleFormatterImpl.FormatCompiledPattern(compiledPattern, values));
 }
示例#3
0
        // TODO: implement use of capitalization
        private string GetLocaleDisplayNameInternal(UCultureInfo locale)
        {
            // lang
            // lang (script, country, variant, keyword=value, ...)
            // script, country, variant, keyword=value, ...

            string resultName = null;

            string lang = locale.Language;

            // 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.Name.Length == 0)
            {
                lang = "root";
            }
            string script  = locale.Script;
            string country = locale.Country;
            string variant = locale.Variant;

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

            // always have a value for lang
            if (displayContextOptions.DialectHandling == DialectHandling.DialectNames)
            {
                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 = GetScriptDisplayNameInContext(script, true);
                if (result == null)
                {
                    return(null);
                }
                buf.Append(result
                           .Replace(formatOpenParen, formatReplaceOpenParen)
                           .Replace(formatCloseParen, formatReplaceCloseParen));
            }
            if (hasCountry)
            {
                string result = GetRegionDisplayName(country, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }
            if (hasVariant)
            {
                string result = GetVariantDisplayName(variant, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }

            using (var pairs = locale.Keywords.GetEnumerator())
            {
                if (pairs != null)
                {
                    while (pairs.MoveNext())
                    {
                        string key            = pairs.Current.Key;
                        string value          = pairs.Current.Value; // locale.GetKeywordValue(key);
                        string keyDisplayName = GetKeyDisplayName(key, true);
                        if (keyDisplayName == null)
                        {
                            return(null);
                        }
                        keyDisplayName = keyDisplayName
                                         .Replace(formatOpenParen, formatReplaceOpenParen)
                                         .Replace(formatCloseParen, formatReplaceCloseParen);
                        string valueDisplayName = GetKeyValueDisplayName(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));
        }